Last active
August 20, 2023 19:22
-
-
Save timopb/5376bdbd869d4e1e4ae69eacbc9ccbae to your computer and use it in GitHub Desktop.
Very basic HTTPS Server in Python (3.10)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. Things can be so simple once you know how to do it ;-)