推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
tg11
V2EX  ›  Python

一个 web 能连接但 Python 不能访问的 websocket 链接,是什么原理?

  •  
  •   tg11 · Jun 10, 2023 · 5299 views
    This topic created in 1243 days ago, the information mentioned may be changed or developed.

    链接地址如下:

    wss://api.cotrans.touhou.ai/task/clipekg954dosmc01ll5xpyh1/event/v1

    用 web 的在线调试工具,可以连接->接收参数->断开。

    用 python 代码或 postman ,加上同样的请求头,连接超时。

    是哪里做了验证?

    Supplement 1  ·  Jun 10, 2023
    上面这个 wss 地址是可以访问的,有兴趣的可以试一下看看
    37 replies  •  2023-06-15 17:49:57 +08:00
    so1n
    so1n
    Jun 10, 2023
    1
    你是不是用了 socketio
    tg11
    tg11
    OP
    Jun 10, 2023
    2
    @so1n 不清楚,服务端是一个网站上的。我只是尝试客户端连接。
    Nazz
    Nazz
    Jun 10, 2023 via Android
    3
    可能是 header 差异造成的,服务端有 check origin
    tg11
    tg11
    OP
    Jun 10, 2023
    4
    @Nazz header 应该不会有差异吧,我完全复制的浏览器使用的 header 。以 cURL 复制也试过了
    momocraft
    momocraft
    Jun 10, 2023
    5
    2 个不好判断 完全有可能是 py 代码写错了
    试试别的 cliient?
    Nazz
    Nazz
    Jun 10, 2023 via Android
    6
    连接超时是因为没使用代理
    tg11
    tg11
    OP
    Jun 10, 2023
    7
    @Nazz 使用 clash 代理了,没用。
    tg11
    tg11
    OP
    Jun 10, 2023
    8
    @momocraft postman 也不行啊
    marshmallow
    marshmallow
    Jun 10, 2023
    9
    python 用了 http2 ,而浏览器降级到 http/1.1 了吧?
    marshmallow
    marshmallow
    Jun 10, 2023
    10
    你看一下浏览器请求头,如果有 Connection: upgrade 和 Upgrade: websocket ,那就是用 http1.1 握手的。http2 版的 websocket 我还没有测过,只是我确定没有 Connection 和 Upgrade 头。
    Nazz
    Nazz
    Jun 10, 2023 via Android
    11
    @tg11 开了 clash 不意味着 websocket 能通过代理建立连接,关掉 clash 你会发现浏览器里面也会超时
    Nazz
    Nazz
    Jun 10, 2023 via Android
    12
    @marshmallow 好好看看 rfc6455 吧
    Nazz
    Nazz
    Jun 10, 2023
    13
    @tg11 使用 Clash X Pro 开启增强模式, 或许可行
    tg11
    tg11
    OP
    Jun 10, 2023
    14
    @Nazz 我直接用的
    websocket.enableTrace(True)
    ws = websocket.WebSocket( http_proxy_host="127.0.0.1", http_proxy_port=7890)
    ws.connect('wss://api.cotrans.touhou.ai/task/clipekg954dosmc01ll5xpyh1/event/v1')
    所以肯定是走了代理的
    tg11
    tg11
    OP
    Jun 10, 2023
    15
    @marshmallow python 的 websocket-client 是基于 http1.1 握手的。请求头确实有 Connection: upgrade 和 Upgrade: websocket 。
    我在连接 ws 的时候手动添加这两个 header 也不管用。
    对了,我在主楼留的链接,是可以直接访问的,你可以试试
    tg11
    tg11
    OP
    Jun 10, 2023
    16
    @Nazz 我在主楼留下的链接地址是可以访问的,你可以尝试下
    centralpark
    centralpark
    Jun 10, 2023❤️1
    17
    可能是 tls 指纹
    tg11
    tg11
    OP
    Jun 10, 2023
    18
    @centralpark 随便手撸一个 js 的 websocket 客户端,就不受 tls 指纹影响吗?
    marshmallow
    marshmallow
    Jun 10, 2023
    19
    @Nazz 你好好看看 rfc8441 ,再来回复我吧
    Nazz
    Nazz
    Jun 10, 2023 via Android
    20
    @marshmallow 是我孤陋寡闻了,没见过 RFC8441 的 server 实现
    Nazz
    Nazz
    Jun 10, 2023
    21
    @tg11 你设置的是 http_proxy, wss 对应的是 https_proxy.
    我在浏览器试过, 开启 SwitchOmega 代理能正常连接, 关闭就会超时
    centralpark
    centralpark
    Jun 10, 2023
    22
    @Nazz #21 那他这个应该就是被墙了……
    Nazz
    Nazz
    Jun 10, 2023
    23
    @centralpark 我用 golang 写的客户端已经连上去了, 输出:
    {"type":"result","result":{"translation_mask":"https://cotrans-r2.moe.ci/mask/clipekg954dosmc01ll5xpyh1.png"}}

    然后过几秒会发生错误断开连接
    marshmallow
    marshmallow
    Jun 10, 2023
    24
    @tg11 我刚测了一下,这个 http 代理配置应该不生效。
    我本机没开 clash ,走的路由器透明代理,浏览器和 python 都是直接连得上。
    marshmallow
    marshmallow
    Jun 10, 2023❤️1
    25
    @Nazz haproxy 2.4+就支持啊,你 docker 运行下看看效果
    tg11
    tg11
    OP
    Jun 10, 2023
    26
    @Nazz 断开连接是正常的,服务端主动断开的。要的就是这段 json 参数。你用 golang 写的时候挂代理了吗?
    Nazz
    Nazz
    Jun 10, 2023 via Android
    27
    @tg11 开了 clash ,然后设置 socks5://127.0.0.1:1080
    est
    est
    Jun 10, 2023
    28
    这不是 py 的 ws 的锅,是 py 的 ws 不支持代理吧?
    haoxue
    haoxue
    Jun 11, 2023 via Android
    29
    可以抓包看一下,对比请求内容之间的差异,再进一步分析
    ethusdt
    ethusdt
    Jun 11, 2023
    30
    用 Proxyman 抓包看看,设置代理为 Proxyman 的,Proxyman 上级代理是 clash, 或者直接用 ClashX Pro 增强模式。

    websocket.WebSocket( http_proxy_host="127.0.0.1", http_proxy_port=PROXYMANPORT)

    https://docs.proxyman.io/advanced-features/websocket
    Nazz
    Nazz
    Jun 11, 2023
    31
    @marshmallow 知道有这个东西就行了. 我感觉 H2 升级 WebSocket 意义不大, 传输层都是 TCP, 而且 1.1 和 2 header 差异较大握手部分代码不能复用进一步导致开发者不愿意支持.
    marshmallow
    marshmallow
    Jun 12, 2023
    32
    @Nazz 不能复用应该不是不愿意支持的主要原因吧。我感觉不如原来的实用才是更主要的原因。
    我想用 h2 websocket ,主要原因是想用来替代 v2ray 的 mux 。平时看视频感觉很卡,不知道是不是 v2ray 的 mux 做的太差了。
    Nazz
    Nazz
    Jun 12, 2023
    33
    @marshmallow 实用性差不多, 握手之后都是 TCP Socket. HTTP/1.1 报文更简单, 而且已经得到了广泛应用.
    握手的报文不变, 把传输层换成 KCP/QUIC 更有意义.
    Nazz
    Nazz
    Jun 12, 2023
    34
    @marshmallow 看视频卡可能是你的梯子延迟高. Mux 功能是在一条 TCP 连接上分发多个 TCP 连接的数据, 对降低延迟没有帮助.
    marshmallow
    marshmallow
    Jun 12, 2023
    35
    http2 就算没有实现 rfc8441 用起来差别也不大。如果 http3 没有实现 rfc9220 ,就感觉这个 http3 功能不完备。
    marshmallow
    marshmallow
    Jun 12, 2023
    36
    延迟高和看视频关系应该不大吧,延迟只影响浏览体验,想通过代理玩游戏可能玩不了。我出现的情况是就访问视频那个流地址会 20 秒,然后超时,访问其他网站都正常,不知道具体什么原因。
    v2ray 介绍里说的 Mux 功能就是为了降低延迟而设计的吧。

    这两天把代理客户端用上 haproxy 了,感觉再把服务器端也配置好 haproxy 应该延迟能降不少。我现在配置的东西有点多杂,第一次访问一个网站可能要连 4 ,5 秒。
    wingor2015
    wingor2015
    Jun 15, 2023
    37
    网上超了一段,
    from websocket import create_connection

    ws = create_connection("wss://api.cotrans.touhou.ai/task/clipekg954dosmc01ll5xpyh1/event/v1", timeout=5)
    if ws.connected:
    ws.send('8')
    print(ws.recv())
    # ws.close()

    打印了这个{"type":"result","result":{"translation_mask":"https://cotrans-r2.moe.ci/mask/clipekg954dosmc01ll5xpyh1.png"}}
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3054 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 11:06 · PVG 19:06 · LAX 03:06 · JFK 06:06
    ♥ Do have faith in what you're doing.