Threading and the HTTPD server classes

james m jamesm at xyke.com
Mon Sep 24 22:12:51 EDT 2001


I've created a simple http server based on the code given in the
python library reference.  Next, I would like to spin this server off
into another thread of execution.  I find that calling
t = threading.Thread(None, my_httpd_proc)
t.start()
t.isAlive()

yields a live thread, but the web browser (IE) complains that it
cannot find server / DNS.

If I modify the httpd function so that threading.Thread() is called on
httpd.serve_forever then everything works correctly.  The web browser
connects and displays the root of the IDLE tree.


This is the code:

## Begin code

def httpd():
    import BaseHTTPServer
    import SimpleHTTPServer
    print "begin jem_httpd"
    server_class = BaseHTTPServer.HTTPServer
    handler_class = SimpleHTTPServer.SimpleHTTPRequestHandler

    server_address = ("jamesmcn-i8k", 3380)
    httpd = server_class(server_address, handler_class)
    print ("about to serve forever")
    
    httpd.serve_forever()


def threaded_httpd():
    import BaseHTTPServer
    import SimpleHTTPServer
    import threading
    print "begin jem_httpd"
    server_class = BaseHTTPServer.HTTPServer
    handler_class = SimpleHTTPServer.SimpleHTTPRequestHandler

    server_address = ("jamesmcn-i8k", 3382)
    httpd = server_class(server_address, handler_class)
    print ("about to serve forever")
    t = threading.Thread(None, httpd.serve_forever)
    t.start()
    print t.isAlive() 

## End code

threaded_httpd() works correctly.

httpd() works, but stalls the IDLE process

t2 = threading.Thread(None, httpd())
t2.start()

appears to do nothing.  Why?

Thanks!

-james



More information about the Python-list mailing list