How do I access the data sent to my server using BaseHTTPRequestHandler? [duplicate] How do I access the data sent to my server using BaseHTTPRequestHandler? [duplicate] python-3.x python-3.x

How do I access the data sent to my server using BaseHTTPRequestHandler? [duplicate]


Give this a try. I stole it from an answer to another question

def do_POST(self):    ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))    if ctype == 'multipart/form-data':        postvars = cgi.parse_multipart(self.rfile, pdict)    elif ctype == 'application/x-www-form-urlencoded':        length = int(self.headers.getheader('content-length'))        postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)    else:        postvars = {}    print(postvars.get("listName", "didn't find it"))


1) In the server (do_POST), how do I access the data I think I'm sending with my request (i.e. {"listName":"Test list","listDesc":"A test...)?

you can access the data just by:

print self.rfile.read(length).

after make sure this is working. you can do other parse work. I suggest use simplejson to decode the json string. urllib.parse.parse_qs seems unnecessary.

2) Is my request even sending the data in the first place?

the code looks fine. to make sure it works, just try:    curl -d "asdf" http://yourhost:yourportto see if the server have same response. so you can know whether the server side or client side goes wrong.

3) Is there a place where this is documented in novice-accessible terms?

the official document is always a good choice:http://docs.python.org/2/library/basehttpserver.html