3

I have a small demo page served with http.server. I tried sharing with my coworkers but discovered that http.server remains blocked on any open connection, so concurrent users can't be served. Is there a way to run http.server to handle concurrent connections? I found nothing useful here: https://docs.python.org/3/library/http.server.html

georgexsh
  • 15,984
  • 2
  • 37
  • 62
Robert Sim
  • 1,428
  • 11
  • 22

3 Answers3

5

IIRC there is no existing config option, but you could extend one with socketserver.ThreadingMixin if you like:

import sys
import socketserver
import http.server


class ThreadedHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
    daemon_threads = True


port = int(sys.argv[1])
server = ThreadedHTTPServer(('', port), http.server.SimpleHTTPRequestHandler)
try:
    server.serve_forever()
except KeyboardInterrupt:
    pass

ps: there is a related python ticket.

georgexsh
  • 15,984
  • 2
  • 37
  • 62
2

You can now use ThreadingHTTPServer

class http.server.ThreadingHTTPServer(server_address, RequestHandlerClass)

This class is identical to HTTPServer but uses threads to handle requests by using the ThreadingMixIn. This is useful to handle web browsers pre-opening sockets, on which HTTPServer would wait indefinitely.

New in version 3.7.

ykaganovich
  • 14,736
  • 8
  • 59
  • 96
0

ThreadingHTTPServer does not handle concurrent requests from multiple browsers, but there is a workaround to do it by preventing HTTPServer from re-binding its socket every instance (answer by personal_cloud): streaming HTTP server supporting multiple connections on one port (You must fixup the imports, and append .encode() to every string argument to write().)

Using that approach my Raspberry Pi 3B+ can stream its camera as a series of still JPEG images to 8 simultaneous browsers at 30 frames per second (same as it gets for 1 user). This is essentially MJPEG, and is much lower latency (<< 1 second) than any video encoding. The camera uses ~70% of a core, and each stream adds ~2%; 8 streams use ~2.5 MB/sec on the network.