带有 ssl 的 Python Gevent Pywsgi 服务器

标签 python ssl flask gevent wsgiserver

我正在尝试使用 gevent.pywsgi.WSGIServer 来包装 Flask 应用程序。一切正常,但是,当我尝试为 ssl 添加 key 和证书时,它甚至无法再接受任何客户端。

这是一个抛出错误的简单示例:

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

重要的是,证书和 key 都是我生成的。

最佳答案

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

关于带有 ssl 的 Python Gevent Pywsgi 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987456/

相关文章:

python - 从 Flask 脚本管理器中获取调试状态

python - 带有 python 和 flask 的 RESTful API 请求和读取文本文件

python - tkinter Text() 小部件上的 4096 单行字符限制?

python - 逐行比较两个不同长度的数据帧并为每行添加具有相等值的列

facebook - cURL Facebook 连接 : Unknown SSL protocol error

python-3.x - 带有自定义 SSL 证书的 Docker 上的 Exchangelib

python - 避免循环导入 Python

python - OSError : [Errno 24] Too many open files python , ubuntu

python - 如何在python中测量knn分类器的准确性

azure - 在 Azure Web 应用程序上启用 SSL/HTTPS 的最佳方法是什么