Python 3.0 urllib.parse.parse_qs results in TypeError

John Machin sjmachin at lexicon.net
Tue Jan 13 17:08:36 EST 2009


On Jan 14, 6:54 am, ag73 <andygrov... at gmail.com> wrote:
> Hi,
>
> I am trying to parse data posted to a Python class that extends
> http.server.BaseHTTPRequestHandler. Here is the code I am using:
>
>         def do_POST(self):
>                 ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
>                 length = int(self.headers['Content-Length'])
>                 if ctype == 'application/x-www-form-urlencoded':
>                         qs = self.rfile.read(length)
>                         print("qs="+str(qs))
>                         form = urllib.parse.parse_qs(qs, keep_blank_values=1)
>
> The print statement shows the following output, so it looks like the
> data is being posted correctly:
>
> qs=b'file_data=b
> %27IyEvdXNyL2Jpbi9lbnYgcHl0aG9uCiMgZW5jb2Rpbmc6IHV0Zi04CiIiIgp1bnRpdGxlZC5w­eQoK
> %5CnQ3JlYXRlZCBieSBBbmR5IEdyb3ZlIG9uIDIwMDgtMTItMDIuCkNvcHlyaWdodCAoYykgMjA­wOCBf
> %5CnX015Q29tcGFueU5hbWVfXy4gQWxsIHJpZ2h0cyByZXNlcnZlZC4KIiIiCgppbXBvcnQgc3l­zCmlt
> %5CncG9ydCBvcwoKCmRlZiBtYWluKCk6CglwcmludCAibmFtZTE9dmFsdWUxIgoJcHJpbnQgIm5­hbWUy
> %5CnPXZhbHVlMiIKCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgoJbWFpbigpCgo%3D
> %5Cn%27&filename=test.py'
>
> However, the last line of code that calls parse_qs causes the
> following exception to be thrown:
>
> <class 'TypeError'>
> Type str doesn't support the buffer API

Please show the full traceback.



More information about the Python-list mailing list