Skip to content

Instantly share code, notes, and snippets.

@Sy3Omda
Created September 25, 2024 19:28
Show Gist options
  • Save Sy3Omda/ca83ebb59ae370d7af806dc8d211b2c8 to your computer and use it in GitHub Desktop.
Save Sy3Omda/ca83ebb59ae370d7af806dc8d211b2c8 to your computer and use it in GitHub Desktop.
self-signed certificate with python http.server
#!/bin/bash
CERT_FILE="localhost.pem"
cleanup() {
rm -f "$CERT_FILE"
exit 0
}
trap cleanup SIGINT
openssl req -new -x509 -keyout "$CERT_FILE" -out "$CERT_FILE" -days 365 -nodes -subj "/CN=localhost"
python3 -c "
import http.server
import ssl
server_address = ('0.0.0.0', 443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certfile='$CERT_FILE')
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
httpd.serve_forever()
"
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment