Created
March 7, 2021 01:43
-
-
Save gh640/3c54c2eb5a8af889e3ab5f49cc4befc6 to your computer and use it in GitHub Desktop.
Sample: A simple https server with Python for development (Python 3.9+).
This file contains 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
"""Simple https server for development.""" | |
import ssl | |
from http.server import HTTPServer, SimpleHTTPRequestHandler | |
CERTFILE = './localhost.pem' | |
def main(): | |
https_server(certfile=CERTFILE) | |
def https_server(*, certfile): | |
print('`https_server()` starts...') | |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | |
context.load_cert_chain(CERTFILE) | |
server_address = ('', 443) | |
with HTTPServer(server_address, SimpleHTTPRequestHandler) as httpd: | |
httpd.socket = context.wrap_socket(httpd.socket, server_side=True) | |
print_server_info(httpd) | |
try: | |
httpd.serve_forever() | |
except Exception as e: | |
httpd.server_close() | |
raise e | |
def print_server_info(server): | |
print(f"""Server info: | |
name: {server.server_name} | |
address: {server.server_address} | |
""") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More examples: