Skip to content

Instantly share code, notes, and snippets.

@timopb
Last active August 20, 2023 19:22
Show Gist options
  • Save timopb/5376bdbd869d4e1e4ae69eacbc9ccbae to your computer and use it in GitHub Desktop.
Save timopb/5376bdbd869d4e1e4ae69eacbc9ccbae to your computer and use it in GitHub Desktop.
Very basic HTTPS Server in Python (3.10)
import http.server
import ssl
import os
host = 'localhost'
port = 443
folder = 'wwwroot'
def main():
pwd = os.getcwd()
try:
# Use openssl to create a self signed certificate:
# openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('cert.pem', 'key.pem')
print(f"Serving {folder} on {host}:{port}")
os.chdir(folder)
httpd = http.server.HTTPServer((host, port), http.server.SimpleHTTPRequestHandler)
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
httpd.serve_forever()
finally:
os.chdir(pwd)
if __name__ == "__main__":
main()
@ullix
Copy link

ullix commented May 12, 2022

Thanks. Things can be so simple once you know how to do it ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment