Skip to content

Instantly share code, notes, and snippets.

@gh640
Created March 7, 2021 01:43
Show Gist options
  • Select an option

  • Save gh640/3c54c2eb5a8af889e3ab5f49cc4befc6 to your computer and use it in GitHub Desktop.

Select an option

Save gh640/3c54c2eb5a8af889e3ab5f49cc4befc6 to your computer and use it in GitHub Desktop.

Revisions

  1. gh640 revised this gist Mar 7, 2021. No changes.
  2. gh640 created this gist Mar 7, 2021.
    37 changes: 37 additions & 0 deletions simple-https-server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    """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()