简体   繁体   中英

gevent and SSL cause “EOF occurred in violation of protocol”

I'm trying to run a simple SSL-enabled application using gevent.pywsgi 's WSGIServer . However, I keep getting SSLError: [Errno 8] _ssl.c:510: EOF occurred in violation of protocol after about 10-15 second after first request is made (from Chrome), during what I assume is an attempt to re-handshake:

Traceback (most recent call last):
  File "D:\SOMEPATH\lib\site-packages\gevent\greenlet.py", line 327, in run
    result = self._run(*self.args, **self.kwargs)
  File "D:\SOMEPATH\lib\site-packages\gevent\server.py", line 102, in wrap_socket_and_handle
    ssl_socket = self.wrap_socket(client_socket, **self.ssl_args)
  File "D:\SOMEPATH\lib\site-packages\gevent\ssl.py", line 383, in wrap_socket
    ciphers=ciphers)
  File "D:\SOMEPATHK\lib\site-packages\gevent\ssl.py", line 94, in __init__
    self.do_handshake()
  File "D:\SOMEPATH\lib\site-packages\gevent\ssl.py", line 305, in do_handshake
    return self._sslobj.do_handshake()
    SSLError: [Errno 8] _ssl.c:510: EOF occurred in violation of protocol
<Greenlet at 0x4998850: <bound method WSGIServer.wrap_socket_and_handle of <WSGIServer at 0x499d6d0 fileno=500 address=127.0.0.1:12344>>(<socket at 0x49f50d0 fileno=912 sock=127.0.0.1:123, ('127.0.0.1', 6398))> failed with SSLError

The page loads just fine. My minimal working example is as follows:

from gevent import monkey
monkey.patch_all()

from gevent import ssl
from flask import Flask
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

@app.route('/')
def main():
   return 'hi!'

server = WSGIServer(
    ('127.0.0.1', 12344),
    app,
    keyfile='server.key',
    certfile='server.crt',
    ssl_version=ssl.PROTOCOL_TLSv1,
)

print 'Serving..'

server.serve_forever()
  1. I have tried forcing the TLSv1 version of the protocol , as suggested in numerous other threads, most of which reference this answer . This can be seen in the MWE.
  2. I have verified that I get no error using Flask's default, non- gevent in-built server , with SSL setup in a way similar to this snippet .
  3. Studying the sources. Eventually, the exception comes from a wrapped C function after several SSL_ERROR_WANT_READ "exceptions" are handled in do_handshake() .

I use gevent==1.0.1 and Python 2.7.8 (default, Jun 30 2014, 16:03:49) on a Windows machine right now.

How do I get rid of that error?

Make sure you specify of your files server.key and server.crt.

Also, when making a HTTP request to the server, don't forget to specify ' ' in https://127.0.0.1:12344/ '

This is probably due to the reason that, when using gevent, server(or client) is not guaranteed to answer immediately for the handshake, and the connectin times out

Look what happened in my case, though I am getting this on the client side 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM