SimpleHTTPServer + Multithreading

Steve Holden sholden at holdenweb.com
Fri Jun 28 09:39:25 EDT 2002


"Adonis" <deltapigz at telocity.com> wrote ...
> i was googling around and was able to come up with cannibalized code, and
> was wondering if there was anyway to make it more "clean" looking, or as
it
> is, is the best way:
>
> (main idea: a multithreading SimpleHTTPServer)
>
> import BaseHTTPServer, SimpleHTTPServer, SocketServer, socket, webbrowser
>
> class XFSPWebServer(SocketServer.ThreadingMixIn,
BaseHTTPServer.HTTPServer):
>     pass
>
> class ReqHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
>
>     def do_POST(self):
>         data = self.rfile.read(int(self.headers["content-length"]))
>         data = data.split('&')[0].split('=')[1]
>         self.wfile.write("HIH")
>
> PORT = 80
>
> httpd = XFSPWebServer((socket.gethostname(), PORT), ReqHandler)
>
> httpd.serve_forever()
>
> any help would greatly be appreciated.
>

Here's the threading CGI server from Chpater 7 of "Python Web Programming".
Replacing CGIHTTPServer with SimpleHTTPServer should give you what you want.
As you can see, the trick is simply to subclass the server you want, making
sure that the ThreadingMixin class is the first base class.

import SocketServer
import BaseHTTPServer
import CGIHTTPServer

class ThreadingCGIServer(SocketServer.ThreadingMixIn,
                   BaseHTTPServer.HTTPServer):
    pass

import sys

server = ThreadingCGIServer(('', 8080), CGIHTTPServer.CGIHTTPRequestHandler)
#
try:
    while 1:
        sys.stdout.flush()
        server.handle_request()
except KeyboardInterrupt:
    print "Finished"

You can get this, and lots more, code at the download page of
http://pydish.holdenweb.com/pwp/

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------






More information about the Python-list mailing list