快速起步python3 https服务

创建PEM密钥

生成SSL

openssl req -new -x509 -keyout localhost.pem -out localhost.pem -days 365 -nodes

创建服务器脚本

创建文件 https-server.py.

import http.server
import ssl

server_address = ('127.0.0.1', 443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='./localhost.pem', server_side=True)
httpd.serve_forever()

# generate private key (./server.pem)
# > openssl req -new -x509 -keyout localhost.pem -out localhost.pem -days 365 -nodes
# > chmod 0400 ./localhost.pem

# generate public key (./)
# > ssh-keygen -e -f ./localhost.pem > ./localhost_pub.pem

# run the server
# > sudo python3 ./https-server.py

要在 192.168.x.x 访问,改一下脚本中 IP 0.0.0.0。

启动服务器:

sudo python3 ./https-server.py

打开浏览器访问:https://localhost/