j撅客 2021-06-09 21:32 采纳率: 0%
浏览 47

哪位高手能帮忙看下这个python websocket连接该怎么写?

import asyncio
import websockets
import json


async def subscribe_without_login_index(url, channels):
    while True:
            async with websockets.connect(url) as ws:
                sub_param = {"method": "SUBSCRIBE","params":channels,"id": 1}
                sub_str = json.dumps(sub_param)
                await ws.send(sub_param)
                while True:
                    try:
                            res_b = await asyncio.wait_for(ws.recv(), timeout=55)
                    except (asyncio.TimeoutError, websockets.exceptions.ConnectionClosed) as e:
                            try:
                                await ws.send('pong')
                                res_b = await ws.recv()
                                print(res_b)
                            except Exception as e:
                                print("连接关闭,正在重连……")
                                break
                            await asyncio.sleep(1)
                    if 'event' in res_b:
                        continue


url = 'wss://fstream.binance.com/ws/<!markPrice@arr>'
channels = ["!miniTicker@arr"]
loop = asyncio.get_event_loop()
loop.run_until_complete(subscribe_without_login_index(url, channels))
loop.close()

运行后报错: TimeoutError: [Errno 10060] Connect call failed ('104.244.46.185', 443)。

其中wss接口url: wss://fstream.binance.com/ws/<!markPrice@arr>

订阅一个信息流: 

  • 请求

    {
    "method": "SUBSCRIBE",
    "params":["!markPrice@arr],
    "id": 1
    }

 

我用websocket 网站测试是OK的,如下图所示:

测试网站: https://www.websocket.org/echo.html

 

但是用python弄不出来,有高手能指点下吗?

  • 写回答

1条回答 默认 最新

  • 有问必答小助手 2021-06-10 16:11
    关注

    你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答

    本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。

    ​​​​因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条