waiting for request using BaseRequestHandler

Ben crescent_au at yahoo.com
Thu Oct 16 01:49:38 EDT 2003


Hi all,

I'd like to know if there is a way to find out the requested filename
using BaseRequestHandler's handle() method... My code looks like this:

def makeList():
  mylist=[]
  l=''
  if os.path.exists("listfile"):
    mf=get_file("listfile")
    mylist=mf[0].split(' ')
    for c in mylist:
      l += '<a href=http://localhost:8755/%s>'%c+c+'</a><br>'
  else:
    #.................
    #.................
    #................. 

  header =  "HTTP/1.0 200 OK\n"
  header += "Content-Type: text/html\n"
  header += "Content-Length: %d\n\n"%len(l)
  return header+l

class ListHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        request = self.request.recv(1024)
        self.request.send(makeList())

myServer = SocketServer.TCPServer(('',8755), ListHandler)
myServer.serve_forever()

In the above code, if I make a request from a browser such as
"http://localhost:8755", it gives me an html page generated by
makeList() method. So the browser displays links like this(for
example):
  Peter
  -----
  Jack
  ----
  Ken
  ---
  Jane
  ----
  
Here, Peter's link is "http://localhost:8755/Peter". I'd like to know
while the server is waiting for a request, how do I make it determine
the path (eg, "http://localhost:8755/Peter"). Initially it takes any
http reqeust and calls makeList() method... What i want to do is I
want makeList() to be called only when the request is
"http://localhost:8755/". For "http://localhost:8755/Peter", I want to
call another method... How do i fix my webserver to handle this?

Thanks
Ben




More information about the Python-list mailing list