Skip to content

Commit

Permalink
Multi-threaded Python HTTP Server to reduce HTTP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
younglim committed Aug 27, 2024
1 parent c81ca36 commit c3d1e9e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion www/http_server_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from http.server import HTTPServer, SimpleHTTPRequestHandler
from socketserver import ThreadingMixIn
import base64
import logging
import argparse
Expand Down Expand Up @@ -53,6 +54,11 @@ def requires_authentication(self):
return False


class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
""" Handle requests in a separate thread. """
daemon_threads = True # This allows threads to be killed when the server exits


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--bind", "-b", metavar="ADDRESS", default="127.0.0.1",
Expand All @@ -62,7 +68,7 @@ def requires_authentication(self):
args = parser.parse_args()

server_address = (args.bind, args.port)
httpd = HTTPServer(server_address, AuthHTTPRequestHandler)
httpd = ThreadedHTTPServer(server_address, AuthHTTPRequestHandler)

server_url = f"http://{args.bind}:{args.port}/"
print(f"Starting httpd server at: {server_url}")
Expand Down

0 comments on commit c3d1e9e

Please sign in to comment.