我已经使用Python2.7 CGIHTTPServer运行了一个web服务。太棒了。它很轻。但是,我现在要求它使用HTTPS和我拥有的证书。关于如何做到这一点的指导很少。实际上,我为Python2.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 -nodesssl_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(),那么现在怎么办?,请记住,我有另一个已经成功处理web请求的python。
发布于 2019-09-15 04:27:40
我在cert文件旁边添加了keyfile,并执行了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)https://stackoverflow.com/questions/44104977
复制相似问题