Skip to content

Instantly share code, notes, and snippets.

@maulvi
Forked from gnilchee/http_multithreaded.py
Created April 24, 2017 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maulvi/e5b5f75f8c3cebdecfdc5efa62f99803 to your computer and use it in GitHub Desktop.
Save maulvi/e5b5f75f8c3cebdecfdc5efa62f99803 to your computer and use it in GitHub Desktop.
Multi-threaded Python3 HTTP Server
#!/usr/bin/env python3
import sys, os, socket
from socketserver import ThreadingMixIn
from http.server import SimpleHTTPRequestHandler, HTTPServer
HOST = socket.gethostname()
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
pass
'''
This sets the listening port, default port 8080
'''
if sys.argv[1:]:
PORT = int(sys.argv[1])
else:
PORT = 8080
'''
This sets the working directory of the HTTPServer, defaults to directory where script is executed.
'''
if sys.argv[2:]:
os.chdir(sys.argv[2])
CWD = sys.argv[2]
else:
CWD = os.getcwd()
server = ThreadingSimpleServer(('0.0.0.0', PORT), SimpleHTTPRequestHandler)
print("Serving HTTP traffic from", CWD, "on", HOST, "using port", PORT)
try:
while 1:
sys.stdout.flush()
server.handle_request()
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