python Flask SocketIO抛出带有Eventlet的SSL错误

时间:2018-09-13 03:38:21

标签: python flask flask-socketio eventlet

我正在运行以下python代码,试图在其中运行socketio flask应用程序并传递ssl证书文件:

from flask import Flask, render_template, request, session, Markup, current_app, jsonify
from flask_socketio import emit, SocketIO

import eventlet


from flask_babel import gettext
app = Flask(__name__)
app.config['SECRET_KEY'] = '123'
app.config['FILEDIR'] = 'static/_files/'
socketio = SocketIO(app)



if __name__ == '__main__':
    try:
        app_host = os.environ.get('APP_HOST')
        app_port = os.environ.get('APP_PORT')

        eventlet.wsgi.server(eventlet.wrap_ssl(eventlet.listen((app_host, int(app_port))),certfile ='selfsigned.crt', keyfile = 'selfsigned.key',server_side = True),app)
    except Exception as e:
        logger.error(e)

运行此代码时,它会引发以下SSL错误:

(21755) wsgi starting up on https://12.34.56.78:5000
(21755) accepted ('12.34.56.79', 50021)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 458, in fire_timers
    timer()
  File "/usr/local/lib/python3.6/site-packages/eventlet/hubs/timer.py", line 58, in __call__
    cb(*args, **kw)
  File "/usr/local/lib/python3.6/site-packages/eventlet/greenthread.py", line 218, in main
    result = function(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 781, in process_request
    proto.__init__(conn_state, self)
  File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 335, in __init__
    self.handle()
  File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 368, in handle
    self.handle_one_request()
  File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 397, in handle_one_request
    self.raw_requestline = self._read_request_line()
  File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 380, in _read_request_line
    return self.rfile.readline(self.server.url_length_limit)
  File "/usr/local/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/usr/local/lib/python3.6/site-packages/eventlet/green/ssl.py", line 204, in recv_into
    return self._base_recv(nbytes, flags, into=True, buffer_=buffer)
  File "/usr/local/lib/python3.6/site-packages/eventlet/green/ssl.py", line 225, in _base_recv
    read = self.read(nbytes, buffer_)
  File "/usr/local/lib/python3.6/site-packages/eventlet/green/ssl.py", line 139, in read
    super(GreenSSLSocket, self).read, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/eventlet/green/ssl.py", line 113, in _call_trampolining
    return func(*a, **kw)
  File "/usr/local/lib/python3.6/ssl.py", line 871, in read
    return self._sslobj.read(len, buffer)
  File "/usr/local/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: HTTP_REQUEST] http request (_ssl.c:2217)

我想要的是此应用应该通过https连接运行,但是此错误阻止了它的运行。以下是我的python和软件包版本详细信息:

python 3.6.3
eventlet==0.22.1
Flask==0.12.2
Flask-SocketIO==2.9.3

1 个答案:

答案 0 :(得分:0)

似乎已向服务器发出的请求使用的是http而不是https。您必须确保客户端也使用与服务器相同的https协议发出请求。例如,在客户端代码中,您必须向https://<server-ip>而不是http://<server-ip>

发送请求