繁体   English   中英

使用Python 2.7 CGIHTTPServer的HTTPS

[英]HTTPS with Python 2.7 CGIHTTPServer

我已经有一个使用Python 2.7 CGIHTTPServer运行的Web服务。 这很棒。 轻巧。 但是,我现在要求它与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()

所以现在怎么办? 再次提醒您,我有另一个已经成功处理Web请求的python脚本。

我在证书文件旁边添加了密钥文件,并执行了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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM