The Python in-built http.server is great when to nead to temperarily
start up a simple webserver, to transfer files or host baisc content.
https.server Works exaclty the same, but the connection will be over TLS,
giving slightly more privacy and security.
Just like http.server, by default it will server the current directory,
but you can also set it to serve a specific folder.
It can be imported as Class in Python,
or called on the commandline with python -m https.server or just https.server
Either pass in a pre-generated TLS cert and key, or the project will create a new tempeory one for you.
https.server runs on Python 3.6 or later
Easiest way is from pip:
pip install https.serverServe the current folder over TLS on the default port:
https.server
# OR
python -m https.serverServe a specific folder of TLS
https.server --directory fooServe the current folder on 443 Note: Usually requires root/administrator privliges
https.server 443Serve the current folder on localhost only
https.server --bind 127.0.0.1Save the auto-generated cert for future use:
https.server --save-certSave both server and client certificates (for proper SSL verification):
https.server --save-cert --client-certWhen using --client-cert, the server will generate both a server certificate (cert.pem) and a client certificate (client-cert.pem). The client certificate contains only the public certificate (no private key) and can be used by HTTP clients to verify the server's identity without SSL warnings.
import requests
# Instead of using verify=False (insecure)
# response = requests.get('https://localhost:8443', verify=False)
# Use the client certificate for proper verification
response = requests.get('https://localhost:8443', verify='client-cert.pem')
print(response.text)curl --cacert client-cert.pem https://localhost:8443The client-cert.pem file can be used with any HTTP client that supports custom CA certificates for SSL verification.
You can also use the server as a library to serve cursom HTTP responses:
from http.server import BaseHTTPRequestHandler
from https.server import HTTPSServer, generate_cert
class CustomRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
message = "Hello!"
self.protocol_version = "HTTP/1.1"
self.send_response(200)
self.send_header("Content-Length", len(message))
self.end_headers()
self.wfile.write(bytes(message, "utf8"))
return
cert_path = "cert.pem"
server = ("127.0.0.1", 8443)
generate_cert(cert_path)
httpd = HTTPSServer(cert_path, server, CustomRequestHandler)
httpd.serve_forever()Serve folder over TLS, using an existing certificate Note: Certificate must be PEM encoded, and have both the cert and private key in the same file
https.server --existing-cert mycert.pem