def websocket(url, headers_dict=None, params_dict=None, result: list = None): """ websocket请求 :param url: :param headers_dict: :param params_dict: :param result: 请求结果列表 """ print('websocket请求url:{}'.format(url)) # step1 创建连接 while True: try: if headers_dict is not None: ws = create_connection(url, header=headers_dict) else: ws = create_connection(url) break except Exception as e: print('连接错误:{}'.format(e)) time.sleep(5) print('请求是否成功:{}'.format('成功' if ws.status == 101 else '失败')) # step2:请求/获取 响应 while True: if params_dict is not None: params = params_dict ws.send(str(params)) response = ws.recv() if result is not None: result.append(response) print(response) ws.close()