带有ssl的Python Gevent Pywsgi服务器 [英] Python Gevent Pywsgi server with ssl

查看:475
本文介绍了带有ssl的Python Gevent Pywsgi服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gevent.pywsgi.WSGIServer包装Flask应用程序.一切正常,但是,当我尝试为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()

这是错误的堆栈跟踪:

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

我正在使用Python 2.7.13和gevent 1.2.1

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.

推荐答案

我发现问题是由客户端发送常规HTTP请求而不是HTTPS引起的.我只需要在浏览器中显式使用https:// URL.

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.

这篇关于带有ssl的Python Gevent Pywsgi服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆