Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Last active March 12, 2024 13:54
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gnilchee/246474141cbe588eb9fb to your computer and use it in GitHub Desktop.
Save gnilchee/246474141cbe588eb9fb 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.")
@g10guang
Copy link

@sbguy01
Copy link

sbguy01 commented Oct 16, 2020

Does this multi-threaded server work on Windows Server?

@Jung-336
Copy link

Does this multi-threaded server work on Windows Server?

Yes. It works well.

@VAkris
Copy link

VAkris commented Oct 26, 2021

How to set a range to listen on all ports?

@qianyizhang
Copy link

good work, thanks!

@fahri314
Copy link

fahri314 commented Oct 17, 2022

It does not support download with multiple connections. i am using this: https://github.com/danvk/RangeHTTPServer
It supports python2 & python3

@gigberg
Copy link

gigberg commented Mar 12, 2024

It does not support download with multiple connections. i am using this: https://github.com/danvk/RangeHTTPServer
It supports python2 & python3

yes ,thanks for your advice, However, in my server the total download speed not increase too much(700kb->900kb) with idm 32 thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment