请稍侯

Python3快速在当前目录建立HTTP(S)服务器

08 September 2024

Python3快速在当前目录建立HTTP(S)服务器

Phthon3 快速启一个默认 8000 端口 http.server

python3 -m http.server

Python3 的简单 HTTPS 服务器脚本

import http.server, ssl

server_ip = '0.0.0.0'
server_port = 4443
server_address = (server_ip, server_port)
server_cert = '../local.test-pem'

httpd = http.server.HTTPServer (server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, server_side=True, certfile=server_cert, ssl_version=ss1 .PROTOCOL_TLS)
print ("Serving HTTPS on" + server_ip + " port " + str(server_port) + " (https://" + server_ip + ":" + str(server_port) + ") ... ")
httpd. serve_forever()

使用 openssl 生成自签证书

# generate self-sign cert, 私钥与证书都存放到了 local.test.pem
openssl req -new -x509 -keyout local.test.pem -out local.test.pem -days 365 -nodes -subj "/C=US/ST=CA/L=LA/0=Test/CN=local.test"