Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Last active March 11, 2022 19:51
Show Gist options
  • Save gnilchee/46abb95246629b0b1df1 to your computer and use it in GitHub Desktop.
Save gnilchee/46abb95246629b0b1df1 to your computer and use it in GitHub Desktop.
Multi-threaded Python 3 HTTPS Server
#!/usr/bin/env python3
import sys, os, socket, ssl
from socketserver import ThreadingMixIn
from http.server import SimpleHTTPRequestHandler, HTTPServer
HOST = socket.gethostname()
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
pass
'''Default to standard HTTPS port (requires root to execute) and current working directory will be served'''
PORT = 443
CWD = os.getcwd()
https_server = ThreadingSimpleServer(('0.0.0.0', PORT), SimpleHTTPRequestHandler)
https_server.socket = ssl.wrap_socket(https_server.socket, keyfile='/var/www/ssl/python_httpd.key', certfile='/var/www/ssl/python_httpd.crt', server_side=True, ssl_version=ssl.PROTOCOL_TLSv1_2, ca_certs=None, do_handshake_on_connect=True, suppress_ragged_eofs=True, ciphers='ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK')
print("Serving HTTPS traffic from", CWD, "on", HOST, "using port", PORT)
try:
https_server.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server per users request.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment