为什么Flask + SocketIO + Gevent会给我SSL EOF错误?

时间:2016-10-11 02:35:15

标签: python-2.7 ssl flask gevent flask-socketio

这是一个简单的代码片段,一直重复我遇到的问题。我使用的是Python 2.7.12,Flask 0.11,Flask-SocketIO 2.7.1和gevent 1.1.2。我知道这可能是一个更好地提交到负责包的邮件列表的问题,但我无法弄清楚哪一个是负责任的。但是,我很确定这是gevent的问题,因为这是引发异常的原因。

from flask import Flask
from flask_socketio import SocketIO

from gevent import monkey
monkey.patch_all()

import ssl

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'

socketio = SocketIO(app, async_mode='gevent')

@app.route('/')
def index():
    return "Hello World!"

@socketio.on('connect')
def handle_connect_event():
    print('Client connected')

if __name__ == '__main__':
    socketio.run(app, host='127.0.0.1', port=8443,
                 certfile='ssl/server/server.cer', keyfile='ssl/server/server.key',
                 ca_certs='ssl/server/ca.cer', cert_reqs=ssl.CERT_REQUIRED,
                 ssl_version=ssl.PROTOCOL_TLSv1_2)

以下是客户端连接时出现的错误:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gevent/greenlet.py", line 534, in 
    result = self._run(*self.args, **self.kwargs)
  File "/usr/lib/python2.7/site-packages/gevent/baseserver.py", line 25, in
    return handle(*args_tuple)
  File "/usr/lib/python2.7/site-packages/gevent/server.py", line 126, in wr
    ssl_socket = self.wrap_socket(client_socket, **self.ssl_args)
  File "/usr/lib/python2.7/site-packages/gevent/_sslgte279.py", line 691, i
    ciphers=ciphers)
  File "/usr/lib/python2.7/site-packages/gevent/_sslgte279.py", line 271, i
    raise x

    SSLEOFError: EOF occurred in violation of protocol (_ssl.c:590)
    <Greenlet at 0x7fdd593c94b0: _handle_and_close_when_done(<bound method WSGInd method WSGIServer.do_close of <WSGIServer a, (<socket at 0x7fdd590f4410 SSLEOFError

如果有帮助,我的系统也有OpenSSL版本1.0.2.j。任何想法都将不胜感激!

1 个答案:

答案 0 :(得分:0)

在代码之上使用patch_all。甚至在烧瓶和socketio进口之前。

from gevent import monkey
monkey.patch_all()

from flask import Flask
from flask_socketio import SocketIO

import ssl