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.server
Serve the current folder over TLS on the default port:
https.server
# OR
python -m https.server
Serve a specific folder of TLS
https.server --directory foo
Serve the current folder on 443 Note: Usually requires root/administrator privliges
https.server 443
Serve the current folder on localhost only
https.server --bind 127.0.0.1
Save the auto-generated cert for future use:
https.server --save-cert
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 DER encoded, and have both the cert and private key in the same file
https.server --existing-cert mycert.der