Skip to content

Instantly share code, notes, and snippets.

@stefanv
Last active September 23, 2021 07:45
Show Gist options
  • Save stefanv/d6160b19dc47ff17891b4bca4ed42d9e to your computer and use it in GitHub Desktop.
Save stefanv/d6160b19dc47ff17891b4bca4ed42d9e to your computer and use it in GitHub Desktop.
Multi-threaded HTTP server in Python
#!/usr/bin/env python3
# See https://github.com/Nakiami/MultithreadedSimpleHTTPServer/blob/master/MultithreadedSimpleHTTPServer.py
# Python 3.x
from socketserver import ThreadingMixIn
from http.server import SimpleHTTPRequestHandler, HTTPServer
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
pass
import sys
import os
if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8000
if sys.argv[2:]:
os.chdir(sys.argv[2])
server = ThreadingSimpleServer(('', port), SimpleHTTPRequestHandler)
print(f'Starting server on 0.0.0.0:{port}')
try:
while 1:
sys.stdout.flush()
server.handle_request()
except KeyboardInterrupt:
print("Finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment