Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Gevent Pywsgi server with ssl

I'm trying to use gevent.pywsgi.WSGIServer to wrap a Flask app. Everything works fine, however, when I try to add a key and a certificate for ssl, it's not even able to accept any clients anymore.

This is a simple example that throws an error:

from gevent.pywsgi import WSGIServer
from flask import Flask

app = Flask(__name__)
app.debug = True

@app.route('/')
def index():
    """
    Renders the homepage.
    """
    return render_template('index.html')

if __name__ == "__main__":
    app.config["SECRET_KEY"] = "ITSASECRET"
    http_server = WSGIServer(('localhost', 5000), app, keyfile='key.pem', 
certfile='cert.pem')
    http_server.serve_forever()

This is the stack trace of the error:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\gevent\greenlet.py", line 536, in run
    result = self._run(*self.args, **self.kwargs)
  File "C:\Python27\lib\site-packages\gevent\baseserver.py", line 26, in 
_handle_and_close_when_done
    return handle(*args_tuple)
  File "C:\Python27\lib\site-packages\gevent\server.py", line 173, in 
wrap_socket_and_handle
    ssl_socket = self.wrap_socket(client_socket, **self.ssl_args)
  File "C:\Python27\lib\site-packages\gevent\_sslgte279.py", line 702, in 
wrap_socket
    ciphers=ciphers)
  File "C:\Python27\lib\site-packages\gevent\_sslgte279.py", line 270, in 
__init__
    raise x
SSLError: [SSL: HTTP_REQUEST] http request (_ssl.c:661)
Mon May 15 22:10:19 2017 <Greenlet at 0x29da440: 
_handle_and_close_when_done(<bound method WSGIServer.wrap_socket_and_handle 
of, <bound method WSGIServer.do_close of <WSGIServer a, (<socket at 
0x29f8190 fileno=[Errno 9] Bad file de)> failed with SSLError

I'm using Python 2.7.13 and gevent 1.2.1

For what it matters, both the certificate and the key were generated by me.

like image 492
Daniel Avatar asked May 15 '17 19:05

Daniel


1 Answers

I figured out that the problem was caused by the client sending a regular HTTP request, instead of HTTPS. I just needed to explicitly use a https:// URL in my browser.

like image 78
Daniel Avatar answered Sep 22 '22 14:09

Daniel