网上搜了好久,没看到python写的webSocketClient
这里贴个示例供大家参考,测过了
async def webSocketClient(self,url,sendData,headers,breakTag): """ 调用实例: netWorkTools = WorkTools.NetworkServerTools() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) code, message, executeResultDict = loop.run_until_complete(netWorkTools.webSocketClient(url,sendData,headers,breakTag)) return code, message, executeResultDict """ code = None message = None executeResultDict = None sendDataDumps = json.dumps(sendData) try: async with ws.connect(url, extra_headers=headers) as websocket: await websocket.send(sendDataDumps) while True: resp = await websocket.recv() if breakTag not in resp: continue Logger(DEFAULT_EXECUTE_LOG_PATH).info("webSocketClient 发送wss请求:resp={}".format(resp)) executeResultDict = json.loads(resp) Logger(DEFAULT_EXECUTE_LOG_PATH).info("webSocketClient 发送wss请求成功:url={},sendData={},executeResultDict={}".format(url,sendData,executeResultDict)) code = "0" message = "webSocketClient执行成功" return code, message, executeResultDict except Exception as e: message = str(e) + "\n" + traceback.format_exc() Logger(DEFAULT_EXECUTE_LOG_PATH).error("webSocketClient 发送wss请求异常 message = {}".format(message)) code = "1" finally: return code, message, executeResultDict
标签:code,webSocket,webSocketClient,resp,实例,loop,message,python3,executeResultDict From: https://www.cnblogs.com/chenyuebai/p/16911741.html