Last active
September 23, 2021 07:45
-
-
Save stefanv/d6160b19dc47ff17891b4bca4ed42d9e to your computer and use it in GitHub Desktop.
Multi-threaded HTTP server in Python
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
#!/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