python - SSL 和 WSGI 应用程序 - Python

标签 python ssl wsgi gevent

我有一个 WSGI 应用程序,我想将其置于 SSL 之后。我的 WSGI 服务器是 gevent

在这种情况下,通过 SSL 为应用提供服务的好方法是什么?

最佳答案

gevent.wsgi模块没有内置的 SSL 支持。如果你正在使用它,把它放在 nginx 后面,它会通过 HTTPS 接收请求,但使用非加密的 HTTP 将它们代理到你的 gevent 应用程序。

gevent.pywsgi模块确实具有内置的 SSL 支持并具有兼容的接口(interface)。设置 keyfilecertfile 参数以使服务器使用 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/

相关文章:

python - 在 Django 中,如何允许打印语句与 Apache WSGI 一起工作?

python - 确定 SSL 包使用的默认协议(protocol)版本?

apache - Ubuntu 14.04 Apache + SSL 服务器,如何配置 Varnish

java - API证书不受浏览器信任,我该怎么办?

ssl - 如何设置 Apache CXF 客户端以使用 WebSphere 信任库? (接收到 "No trusted certificate found"异常。)

python - 使用 Python Flask、mod_wsgi、apache2 - 无法获得自定义 500 错误页面

python - VIM 如何区分 `Ctrl-J` 和 `LF` 之间的区别?

python - 使用 pandas.io.sql.read_frame,我可以像 read_csv 一样解析日期吗?

python - DRF : how to not allow create() in Serializer

nginx - 使用uwsgi协议(protocol)将uWSGI连接到NGinx有什么好处?