Last active
March 12, 2024 13:54
-
-
Save gnilchee/246474141cbe588eb9fb to your computer and use it in GitHub Desktop.
Multi-threaded Python3 HTTP Server
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 | |
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.") |
Does this multi-threaded server work on Windows Server?
Does this multi-threaded server work on Windows Server?
Yes. It works well.
How to set a range to listen on all ports?
good work, thanks!
It does not support download with multiple connections. i am using this: https://github.com/danvk/RangeHTTPServer
It supports python2 & python3
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
My code in stackoverflow: https://stackoverflow.com/a/51559006/7159205