13

Is there a way to make BaseHTTPServer.HTTPServer be multi-threaded like SocketServer.ThreadingTCPServer?

Ian
  • 20,207
  • 21
  • 53
  • 96
  • Is there a reason why you need it to be? – jakebman Mar 07 '10 at 22:29
  • 3
    Because I want a basic web server that can handle concurrency? I also don't need/want an all out framework like web.py, cherrypy or anything like that, I just want a really basic webserver like BaseHTTPServer that can handle multiple concurrent requests. – Ian Mar 07 '10 at 22:32
  • 1
    here is a reference: [link](http://www.doughellmann.com/PyMOTW/BaseHTTPServer/index.html#module-BaseHTTPServer), threading, forking – sfossen Apr 08 '10 at 17:52
  • Use Apache and mod_wsgi. –  Mar 19 '10 at 16:20

1 Answers1

19

You can simply use the threading mixin using both of those classes to make it multithread :)

It won't help you much in performance though, but it's atleast multithreaded.

from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer

class MultiThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    pass
Jordfräs
  • 7,842
  • 3
  • 26
  • 30
Wolph
  • 69,888
  • 9
  • 125
  • 143
  • 2
    This looks like *a* solution.. however I'd rather opt to write my own server than use something slow.. :( – Ian Mar 08 '10 at 18:49
  • If you're simply looking for hosting Python than why not use an existing http server like nginx, apache or lighttpd? As for the performance, threading it will allow you to make multiple concurrent connections without blocking so in the case of multiple simultaneous requests it will be faster. But it will still use only 1 processor. – Wolph Mar 09 '10 at 00:16
  • 1
    I'm not looking for that, I'm making a Queue server that takes incoming requests (http or some similar format) and does an action based on the request. – Ian Mar 09 '10 at 04:09
  • here is a reference: [link](http://www.doughellmann.com/PyMOTW/BaseHTTPServer/index.html#module-BaseHTTPServer), threading, forking – sfossen Apr 08 '10 at 17:52
  • @Ian: since you're making a queue server you might want to try some other technology. Take a look at the eventlet library: http://wiki.secondlife.com/wiki/Eventlet – Wolph Apr 08 '10 at 22:53
  • 1
    For quicky POC imps, this is a great way to allow multiple connections to not step on each other's comm buffers. Thanks for sharing. – Buzz Moschetti Sep 24 '15 at 16:36