Skip to content

Instantly share code, notes, and snippets.

@kkt-ee
Created December 1, 2023 20:58
Show Gist options
  • Save kkt-ee/c16a0fe6c015c589ce8fd8dbab117c48 to your computer and use it in GitHub Desktop.
Save kkt-ee/c16a0fe6c015c589ce8fd8dbab117c48 to your computer and use it in GitHub Desktop.
https server (replacement of python -m http.server)
""" Three steps to make an https alternative to python -m http.server @kkt
1. shell command to generate the files: certificate.pem and key.pem
$ openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem
2. Host the server
3. Add to $PATH
"""
"""2. Host https server"""
import http.server
import ssl
# Specify the path to your SSL certificate and private key
certfile = "/path/to/certificate.pem"
keyfile = "/path/to/key.pem"
# Specify the server address and port
server_address = ('localhost', 8443)
# Create an HTTP server with SSL support
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
# Wrap the socket with SSL
httpd.socket = ssl.wrap_socket(httpd.socket, certfile=certfile, keyfile=keyfile, server_side=True)
# Start the server
print(f"Starting HTTPS server on {server_address[0]}:{server_address[1]}...")
httpd.serve_forever()
"""3. Add to path
save the file as https_server.py
make a bash file in ~/bin: $ nano https.sh
Add the line python /path/to/https_server.py
$chmod +x https.sh
Assuming ~/bin is in $PATH
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment