Python Gevent Pywsgi server with ssl

2024/10/8 10:52:10

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 Flaskapp = 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 runresult = self._run(*self.args, **self.kwargs)File "C:\Python27\lib\site-packages\gevent\baseserver.py", line 26, in 
_handle_and_close_when_donereturn handle(*args_tuple)File "C:\Python27\lib\site-packages\gevent\server.py", line 173, in 
wrap_socket_and_handlessl_socket = self.wrap_socket(client_socket, **self.ssl_args)File "C:\Python27\lib\site-packages\gevent\_sslgte279.py", line 702, in 
wrap_socketciphers=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.

Answer

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.

https://en.xdnf.cn/q/70130.html

Related Q&A

unexpected keyword argument buffering - python client

I am receiving the error as "getresponse() got an unexpected keyword argument buffering". Complete error log is :[INFO ] Kivy v1.8.0 [INFO ] [Logger ] Record lo…

numpy and pandas timedelta error

In Python I have an array of dates generated (or read from a CSV-file) using pandas, and I want to add one year to each date. I can get it working using pandas but not using numpy. What am I doing wron…

Pandas - split large excel file

I have an excel file with about 500,000 rows and I want to split it to several excel file, each with 50,000 rows.I want to do it with pandas so it will be the quickest and easiest.any ideas how to make…

Unable to verify secret hash for client at REFRESH_TOKEN_AUTH

Problem"Unable to verify secret hash for client ..." at REFRESH_TOKEN_AUTH auth flow. {"Error": {"Code": "NotAuthorizedException","Message": "Unab…

save a dependecy graph in python

I am using in python3 the stanford dependency parser to parse a sentence, which returns a dependency graph. import pickle from nltk.parse.stanford import StanfordDependencyParserparser = StanfordDepend…

What are the specific rules for constant folding?

I just realized that CPython seems to treat constant expressions, which represent the same value, differently with respect to constant folding. For example:>>> import dis >>> dis.dis(…

installing opencv for python on mavericks

I am trying to install opencv on a Macbook Pro late 2013 with mavericks. I didnt find any binaries so I am trying to build it. I tried http://www.guidefreitas.com/installing-opencv-2-4-2-on-mac-osx-mou…

Python 3 reading CSV file with line breaks in rows

I have a large CSV file with one column and line breaks in some of its rows. I want to read the content of each cell and write it to a text file but the CSV reader is splitting the cells with line brea…

Python appending dictionary, TypeError: unhashable type?

abc = {} abc[int: anotherint]Then the error came up. TypeError: unhashable type? Why I received this? Ive tried str()

Calling C# code within Python3.6

with absolutely no knowledge of coding in C#, I wish to call a C# function within my python code. I know theres quite a lot of Q&As around the same problem, but for some strange reason, im unable t…