waiting for request using BaseRequestHandler

Discussion in 'Python' started by Ben, Oct 16, 2003.

  1. Ben

    Ben Guest

    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
     
    Ben, Oct 16, 2003
    #1
    1. Advertising

  2. Ben

    bromden Guest

    please read the docstring of the class BaseHTTPRequestHandler

    >>> import BaseHTTPServer
    >>> print BaseHTTPServer.BaseHTTPRequestHandler.__doc__


    normally you should subclass BaseHTTPRequestHandler and override
    methods do_GET(), do_POST() and so on, which are handling specific
    HTTP commands,
    the info about a request being processed is stored in the class
    members described in the docstring, specifically the path is
    in (guess) "path",
    moreover, you shouldn't assemble response header yourself,
    use the methods send_response(), send_error() instead,

    so what you should do is something like

    class ListHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_GET(self):
    if (self.path) == '/Peter':
    data = self.makeList()
    self.send_response(200)
    self.send_header('Content-Length', str(len(data)))
    self.end_headers()
    self.wfile.write(data)
    ....
    else:
    ....

    --
    bromden[at]gazeta.pl
     
    bromden, Oct 16, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Birtle
    Replies:
    2
    Views:
    2,102
    John Saunders
    Oct 16, 2003
  2. ao11
    Replies:
    0
    Views:
    427
  3. Robert Oschler
    Replies:
    2
    Views:
    99
    Chris Riesbeck
    Sep 17, 2003
  4. Dennis Lee Bieber
    Replies:
    3
    Views:
    198
    Dave Angel
    Aug 9, 2012
  5. Terry Reedy
    Replies:
    0
    Views:
    173
    Terry Reedy
    Aug 9, 2012
Loading...

Share This Page