python-socketio通信与ssl相关的问题

2024-04-26 03:00:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pythonsocketio进行通信,它对http很好。在升级它以使用SSL时出现问题。在

我制作了一个自签名的根证书(CA),并颁发了服务器证书以及服务器.key. 我告诉计算机信任CA。之后,我添加了服务器证书以及服务器.key在flask socketio服务器端。代码如下:

from flask import Flask, render_template
from flask_socketio import SocketIO
from flask_socketio import Namespace

app = Flask(__name__, template_folder="templates")

app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

# Create a URL route in our application for "/"
@app.route('/')
def home():
    """
    This function loads the homepage
    """
    return render_template('index.html')


class MyCustomNamespace(Namespace):
    def on_connect(self):
        print("Client just connected")

    def on_disconnect(self):
        print("Client just left")

    def on_messages(self, data):                     
        print(f"\nReceived data from client: \n {data}\n")
        return data

socketio.on_namespace(MyCustomNamespace('/channel_A'))

if __name__ == "__main__":
    socketio.run(app, host="192.168.0.28", port=2000, certfile="server.crt", keyfile="server.key", server_side=True, debug=True)    
    #socketio.run(app, host="192.168.0.28", port=2000, debug=True)

客户端连接的代码很简单:

^{pr2}$

我使用pythonsocketio(https://python-socketio.readthedocs.io/en/latest/client.html#disconnecting-from-the-server)作为客户端。在

服务器启动后,网站运行良好,连接也很安全。命令行如下所示:

 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 142-782-563
(3484) wsgi starting up on https://192.168.0.28:2000

当SocketIO client尝试与服务器连接时,连接被拒绝,在服务器端,最终会抛出如下错误:

ssl.SSLError: [SSL: TLSV1_ALERT_UNKNOWN_CA] tlsv1 alert unknown ca (_ssl.c:2488)

我想我可能错过了什么。有什么想法吗?提前谢谢!在


Tags: keyfromimport服务器appflaskdataserver