python - 使用 Python 2.7 CGIHTTPServer 的 HTTPS

标签 python ssl cgi-bin

我已经有一个使用 Python 2.7 CGIHTTPServer 运行的网络服务。这很棒。它很轻。但是,我现在要求它使用 HTTPS 和我拥有的证书。关于如何执行此操作的说明很少。事实上,我只找到一篇关于 Python 2.7 的文章。

我的问题很简单而且范围很窄。 按照下面的说明,我该如何启动它?我已经有一个基于事务的 python 脚本。你调用它,它处理你的请求。它需要 SSL。

https://blog.farville.com/15-line-python-https-cgi-server

这需要一个目录结构:

/ssl_server.py
/localhost.pem
/html/index.html   html lives here, aka “root directory”
/html/cgi/         python scripts live here

像这样使用 openssl 制作的自签名 SSL 证书:

openssl req -x509 -sha256 -newkey rsa:2048 -keyout localhost.pem \
-out localhost.pem -days 3650 -nodes

ssl_server.py:

#!/usr/bin/env python
import os, sys
import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable() ## This line enables CGI error reporting
import ssl
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8443)
handler.cgi_directories = ["/cgi"]
os.chdir("html")
srvobj = server(server_address, handler)
srvobj.socket = ssl.wrap_socket (srvobj.socket, certfile="../localhost.pem", server_side=True)
# Force the use of a subprocess, rather than
# normal fork behavior since that doesn't work with ssl
handler.have_fork=False
srvobj.serve_forever()

那么现在呢?再次提醒,请记住我有另一个 python 脚本,它已经成功地处理了网络请求。

最佳答案

我在证书文件旁边添加了 key 文件,并执行了 python ssl_server.py,它成功了

srvobj.socket = ssl.wrap_socket (srvobj.socket, certfile="/etc/letsencrypt/live/myowndomain.com/cert.pem", keyfile="/etc/letsencrypt/live/myowndomain.com/privkey.pem" server_side=True)

关于python - 使用 Python 2.7 CGIHTTPServer 的 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44104977/

相关文章:

Python 在没有咨询系统 CA 包的情况下抛出 SSL 证书验证错误?

java - 通过默认 SSLSocketFactory 创建的 Socket 发送密码是否安全?

Python 类型错误 : unorderable types: builtin_function_or_method() < int()

python - graphlab-sframe : How to remove rows which have same ids and condition on a column?

ssl - docker 证书的最佳实践

python -/dev/mem 访问在树莓派上被拒绝

linux - Linux 主机每秒可以支持多少个请求

apache - 将 CGI 参数传递给 Windows 上 Apache 中的可执行文件

python - Windows 控制台编码

python - pycassa TypeError : A str or unicode, 无法插入 cassandra