gpt4 book ai didi

python - 使用 Python 2.7 CGIHTTPServer 的 HTTPS

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:54 30 4
gpt4 key购买 nike

我已经有一个使用 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com