【Python】【websocket请求/ws请求】


 

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()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM