如何通过HTTPS运行python-socketio(eventlet WSGI服务器)

时间:2016-09-09 22:09:54

标签: python socket.io

我想通过HTTPS运行以下eventlet WSGI服务器。我试图在启用HTTPS的Web服务器上从JavaScript连接到python服务器。

我希望得到答案来描述如何更改以下代码以使用HTTPS。

import socketio
import eventlet
import eventlet.wsgi
from flask import Flask, render_template

sio = socketio.Server()
app = Flask(__name__)

@app.route('/')
def index():
    """Serve the client-side application."""
    return render_template('index.html')

@sio.on('connect', namespace='/chat')
    def connect(sid, environ):
    print("connect ", sid)

@sio.on('chat message', namespace='/chat')
def message(sid, data):
    print("message ", data)
    sio.emit('reply', room=sid)

@sio.on('disconnect', namespace='/chat')
def disconnect(sid):
    print('disconnect ', sid)

if __name__ == '__main__':
    # wrap Flask application with engineio's middleware
    app = socketio.Middleware(sio, app)

    # deploy as an eventlet WSGI server
    eventlet.wsgi.server(eventlet.listen(('', 8000)), app)

此代码来自here

1 个答案:

答案 0 :(得分:0)

要通过HTTPS运行Evenlet WSGI服务器,只需将SSL包装的套接字传递给server()方法,如下所示:

wsgi.server(eventlet.wrap_ssl(eventlet.listen(('', 8000)),
                          certfile='cert.crt',
                          keyfile='private.key',
                          server_side=True),
        app)