共计 659 个字符,预计需要花费 2 分钟才能阅读完成。
# taken from https://gist.github.com/DannyHinshaw/a3ac5991d66a2fe6d97a569c6cdac534
# which taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/
# generate server.pem with the following command:
# openssl req -new -x509 -keyout key.pem -out server.pem -days 365 -nodes
import http.server
import ssl
import sys
print("usage: <simple-https-server> <port> <cert pem file> <key pem file>")
port, certfile, keyfile = sys.argv[1:]
server_address = ('0.0.0.0', int(port))
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
server_side=True,
certfile=certfile,
keyfile=keyfile,
ssl_version=ssl.PROTOCOL_TLS)
httpd.serve_forever()
正文完