我有一个 WSGI 应用程序,我想将其置于 SSL 之后。我的 WSGI 服务器是 gevent。
在这种情况下,通过 SSL 为应用提供服务的好方法是什么?
最佳答案
gevent.wsgi模块没有内置的 SSL 支持。如果你正在使用它,把它放在 nginx 后面,它会通过 HTTPS 接收请求,但使用非加密的 HTTP 将它们代理到你的 gevent 应用程序。
gevent.pywsgi模块确实具有内置的 SSL 支持并具有兼容的接口(interface)。设置 keyfile
和 certfile
参数以使服务器使用 SSL。这是一个例子:wsgiserver_ssl.py :
#!/usr/bin/python
"""Secure WSGI server example based on gevent.pywsgi"""
from __future__ import print_function
from gevent import pywsgi
def hello_world(env, start_response):
if env['PATH_INFO'] == '/':
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"<b>hello world</b>"]
else:
start_response('404 Not Found', [('Content-Type', 'text/html')])
return [b'<h1>Not Found</h1>']
print('Serving on https://127.0.0.1:8443')
server = pywsgi.WSGIServer(('0.0.0.0', 8443), hello_world, keyfile='server.key', certfile='server.crt')
# to start the server asynchronously, call server.start()
# we use blocking serve_forever() here because we have no other jobs
server.serve_forever()
关于python - SSL 和 WSGI 应用程序 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2857273/