stilde/contrib/serve-https.py

30 lines
1,017 B
Python
Raw Permalink Normal View History

import os
import http.server
import socketserver
import ssl
# Set the path to the directory you want to serve
directory_to_serve = os.path.expanduser('~/.local/opt/stilde')
# Set the port for your HTTPS server
port = 1984
# Specify the SSL/TLS certificate and key files
certfile = os.path.expanduser('~/.ssl/MYRootCA/localhost.crt')
keyfile = os.path.expanduser('~/.ssl/MYRootCA/localhost.key')
# Create a custom handler to use the SSL context
class MyHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=directory_to_serve, **kwargs)
# Create an SSL context
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile=certfile, keyfile=keyfile)
# Create the HTTPS server
with socketserver.TCPServer(("127.0.1.1", port), MyHandler) as httpd:
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
print(f"Serving directory at https://127.0.1.1:{port}")
httpd.serve_forever()