SSL和WSGI应用 - Python
我有一个WSGI应用程序,想要让它通过SSL来保护。我的WSGI服务器是gevent。
在这种情况下,有什么好的方法可以通过SSL来提供这个应用呢?
3 个回答
2
我会让HTTP服务器来处理SSL传输。
9
gevent.wsgi模块本身不支持SSL加密。如果你在使用这个模块,建议把它放在nginx后面,nginx可以接收HTTPS请求,然后再通过未加密的HTTP把请求转发给你的gevent应用。
gevent.pywsgi模块则支持SSL加密,并且接口也兼容。你需要设置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()
3
现在,gevent似乎有了一个ssl模块。如果你在gevent上搭建了一个网页服务器,我想你可以修改它,让它在把连接交给http处理程序之前,先用这个模块的ssl套接字类来包装一下传入的连接。
http://blog.gevent.org/2010/02/05/version-0-12-0-released/
http://www.gevent.org/gevent.ssl.html
另外,你也可以使用老牌的apache加上mod_wsgi来运行你的wsgi应用。