Linux 下可以用 OpenSSL 抓证书,Windows 下可以尝试在 WSL 下操作。或许在 Windows 下也可以正常用,没测试了。
服务器搭建,简单的一个服务器环境,server.py :
import http.server
import ssl
server_address = ('localhost', 4443)
handler = http.server.SimpleHTTPRequestHandler
httpd = http.server.HTTPServer(server_address, handler)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certfile='cert.pem', keyfile='key.pem')
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
print(f"Serving on https://{server_address[0]}:{server_address[1]}")
httpd.serve_forever()
然后生成需要的证书:
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out cert.pem
询问 CN (Common Name (e.g. server FQDN or YOUR name) ) 的时候输入 localhost ,其它的可以随便填。
然后用 python3 server.py 启动即可,监听端口 4443 。
客户端用 openssl 抓需要的证书:
openssl s_client -showcerts -connect localhost:4443 </dev/null | openssl x509 -outform PEM > my-ca.pem
然后用 curl 来连接就好了:
curl -I --cacert my-ca.pem https://localhost:4443
正常连接:
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.12.3
Content-type: text/html; charset=utf-8
Content-Length: 310
|