python-m http.server 443--使用SSL?

2022-02-21 00:00:00 python python-3.x http server ssl

问题描述

是否可以使用SSL证书创建临时Python3 HTTP服务器?例如:

$ python3 -m http.server 443 --certificate /path/to/cert

解决方案

不是从命令行,但是编写一个简单的脚本来执行此操作非常简单。

from http.server import HTTPServer, BaseHTTPRequestHandler 
import ssl
httpd = HTTPServer(('localhost', 4443), BaseHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(
    httpd.socket,
    keyfile="path/to/key.pem",
    certfile='path/to/cert.pem',
    server_side=True)
httpd.serve_forever()

Credit

如果您不局限于标准库并且可以安装pip包,则还有许多其他选项,例如,您可以安装接受命令行选项的uwsgi。

相关文章