def client(data): while True: ws = yield from connect(HOST+':'+PORT) yield from ws.send(data) greeting = yield from ws.recv() print("< {}".format(greeting)) yield from ws.close()
async def tv_conn(): async with connect(url, ssl=CERT_NONE, close_timeout=2000, ping_interval=2000) as websocket: res = await websocket.recv() print(res)
async def _mock_listener(self, on_connect): async with connect(self._uri) as websocket: on_connect() await _mock_verify_handler( websocket.recv, websocket.send, self._interactions, self._errors, mock_direction=InteractionDirection.REQUEST, verify_direction=InteractionDirection.RESPONSE, )
def start_gecko(self, path='', **kwds): gecko = connect('ws://localhost:8765/worker', **kwds) self.gecko = self.loop.run_until_complete(gecko) self.loop.run_until_complete( self.gecko.send( json.dumps({ "messageType": "hello", "action": "worker-hello", "geckoId": "gecko-1243" }))) print("# Gecko started")
async def _wait_for_job_result_ws(service_url: str, ticket: str) -> str: # Note that a 'https' url will result in 'wss' after the string replace # which is what we want. ws_service_url = service_url.replace("http", "ws") artifacts_service_url = f"{ws_service_url}/job/artifacts?ticket={ticket}" async with connect(artifacts_service_url, ssl=True) as websocket: message = await websocket.recv() job_status = json.loads(message) artifact_url = _check_job_status(service_url, ticket, job_status) if artifact_url: return artifact_url
async def _publish_changes( ee_id: str, changes, ws_uri: str, ssl_context: ssl.SSLContext, headers: Mapping[str, str], ): events = deque([ JobQueue._translate_change_to_cloudevent(ee_id, real_id, status) for real_id, status in changes.items() ]) retries = 0 while True: try: async with connect(ws_uri, ssl=ssl_context, extra_headers=headers) as websocket: while events: await asyncio.wait_for( websocket.send(to_json(events[0])), 60) events.popleft() return except (ConnectionClosedError, asyncio.TimeoutError) as e: if retries >= 10: logger.exception( "Connection to websocket %s failed, unable to publish changes", ws_uri, ) raise # websockets for python > 3.6 comes with builtin backoff, implement a # crude one here retries += 1 backoff = max(3, min(60, 2**retries)) logger.info( "Connection to websocket %s was closed, retry in %d seconds.", ws_uri, backoff, exc_info=e, ) await asyncio.sleep(backoff)
def connect(self): return connect(self.ws_url, extra_headers={"Cookie": self.cookie}, origin=Origin(self.http_url))
def start_client(self, path='', **kwds): client = connect('ws://localhost:8765/' + path, **kwds) self.client = self.loop.run_until_complete(client) print("$ Client started")
# sample with pip3 install websockets # authentication works def connect(url): from websockets.client import connect, WebSocketClientProtocol from ssl import CERT_NONE import asyncio async def tv_conn(): async with connect(url, ssl=CERT_NONE, close_timeout=2000, ping_interval=2000) as websocket: res = await websocket.recv() print(res) asyncio.get_event_loop().run_until_complete(tv_conn()) if __name__ == "__main__": from tv import SSL_URL connect(SSL_URL)