Parse http GET and POST parameters from BaseHTTPHandler?

Posted by ataylor on Stack Overflow See other posts from Stack Overflow or by ataylor
Published on 2010-03-22T05:32:24Z Indexed on 2010/03/22 5:41 UTC
Read the original article Hit count: 333

Filed under:
|

BaseHTTPHandler from the BaseHTTPServer module doesn't seem to provide any convenient way to access http request parameters. What is the best way to parse the GET parameters from the path, and the POST parameters from the request body?

Right now, I'm using this for GET:

def do_GET(self):
    parsed_path = urlparse.urlparse(self.path)
    try:
        params = dict([p.split('=') for p in parsed_path[4].split('&')])
    except:
        params = {}

This works for most cases, but I'd like something more robust that handles encodings and cases like empty parameters properly. Ideally, I'd like something small and standalone, rather than a full web framework.

© Stack Overflow or respective owner

Related posts about python

Related posts about http