如果这个问题太长,请原谅我.我正在开发一种物联网设备,它通过WebSockets与网站交谈.我的物联网设备现在支持使用套接字的TCP通信. 在转到IOT设备中的WebSockets之前,我想在我的覆盆子pi 4上try WebSockets.所以我做了一个简单的设置,在render.com上运行了一个wesocket服务器,我的链接格式如下:

<wss://web-socket-xxx.onrender.com>

我在raspberry pi上使用了以下Python脚本连接到Web服务器:

import websockets
import asyncio

async def ws_client():
    print("WebSocket: Client Connection Initiated.")
    url = "wss://web-socket-9wz7.onrender.com"
    # Connect to the server
    async with websockets.connect(url) as ws:
        print(ws.remote_address)
        print(ws.path)
        print("request headers")
        print(ws.request_headers)
        print("response headers")
        print(ws.response_headers)
        print(ws.subprotocol)
        name = input("Your Name (type 'exit' to quit): ")

        if name == 'exit':
            exit()

        age = input("Your Age: ")
        # Send values to the server
        await ws.send(f"{name}")
        await ws.send(f"{age}")

        # Stay alive forever, listen to incoming msgs
        while True:
            msg = await ws.recv()
            print(msg)

# Start the connection
asyncio.run(ws_client())

上面的脚本运行得非常好.WebSocket通信不会出现任何问题.

因为我不能在我的物联网设备上使用Python代码,所以我开始研究任何我可以在Raspberry pi上编译的WebSocket的开源C实现.为此,我 Select 了以下GitHub回购

https://github.com/jeremyhahn/cwebsocket

我在树莓pi上基于上面的repo编写了WebSocket客户端,并try 连接到WebSocket服务器.我遇到了以下错误:

cwebsocket: starting cwebsocket client
cwebsocket: cwebsocket_client_init: stack limit min=8388608, max=-1
cwebsocket: cwebsocket_client_connect: hostname=web-socket-xxxx.onrender.com, port=443, resource=/, querystring=, secure=1
cwebsocket: cwebsocket_client_connect: using secure (SSL) connection
4159651904:error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:../ssl/record/rec_layer_s3.c:1562:SSL alert number 40
cwebsocket: exiting cwebsocket

我判断了与本地WebSocket服务器相同的WebSocket客户端,它工作正常,但每次try 连接到render.com上的远程WebSocket服务器时,我都会遇到相同的错误. 谁能告诉我远程WebSocket服务器出了什么问题?

推荐答案

简单地看一下实现,它看起来不支持SNI -但服务器需要SNI.这可能是你看到的问题的原因,即服务器返回握手错误alert .

一个可能的解决办法是在calling SSL_connect in the code之前加上SSL_set_tlsext_host_name.

但我建议最好不要使用图书馆.它是7年来没有改变,看起来被遗弃-即使它的国家"cwebsocket is currently in a development state.".更严重的是,它似乎没有实现任何类型的证书验证.这应该被认为是一个关键而明显的安全问题,这在某种程度上表明,无论是谁写了这段代码,(在写这段代码的时候)都没有对代码安全性有正确的理解.因此,可能会出现比这些显而易见的问题更多的问题.

Python相关问答推荐

自定义新元未更新参数

inspect_asm不给出输出

在for循环中仅执行一次此操作

添加包含中具有任何值的其他列的计数的列

在函数内部使用eval(),将函数的输入作为字符串的一部分

如何计算两极打印机中 * 所有列 * 的出现次数?

numba jitClass,记录类型为字符串

需要计算60,000个坐标之间的距离

如何避免Chained when/then分配中的Mypy不兼容类型警告?

从numpy数组和参数创建收件箱

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

如何过滤包含2个指定子字符串的收件箱列名?

在Python argparse包中添加formatter_class MetavarTypeHelpFormatter时, - help不再工作""""

Geopandas未返回正确的缓冲区(单位:米)

Python避免mypy在相互引用中从另一个类重定义类时失败

替换现有列名中的字符,而不创建新列

Python类型提示:对于一个可以迭代的变量,我应该使用什么?

如何反转一个框架中列的值?

当输入是字典时,`pandas. concat`如何工作?

如何用FFT确定频变幅值