Skip to content

Instantly share code, notes, and snippets.

@NoahCardoza
Last active September 14, 2020 09:52
Show Gist options
  • Save NoahCardoza/f9c6c97d7d52322ea930c8b8d031099d to your computer and use it in GitHub Desktop.
Save NoahCardoza/f9c6c97d7d52322ea930c8b8d031099d to your computer and use it in GitHub Desktop.
An HTTP server that will handle both HTTP and HTTPS in Python 3
from http.server import HTTPServer
class HTTPAndHTTPSServer(HTTPServer):
def __init__(ssl_context, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ssl_context = ssl_context
def get_request(self):
conn, addr = self.socket.accept()
if self.context and conn.recv(1, socket.MSG_PEEK) == b'\x16':
conn = self.ssl_context.wrap_socket(conn, server_side=True)
return conn, addr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment