Python 3.0 urllib.parse错误“类型str不支持缓冲区API” [英] Python 3.0 urllib.parse error "Type str doesn't support the buffer API"

查看:77
本文介绍了Python 3.0 urllib.parse错误“类型str不支持缓冲区API”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 文件 /usr/local/lib/python3.0/cgi.py,第477行,位于__init__ 
self.read_urlencoded()
File /usr/local/lib/python3.0/cgi.py,第577行,在read_urlencoded
self.strict_parsing中):
文件 /usr/local/lib/python3.0/urllib/parse .py,第377行,在parse_qsl
对中= [对于s1.split(';')中的s2,对于qs.split('&')中的s1为s2]
TypeError:类型为str不支持缓冲API

有人可以指导我如何避免这种情况吗?我是通过将数据输入到 cgi.Fieldstorage 来获得的,而我似乎无法通过其他任何方式来实现。

解决方案

urllib试图这样做:

  b'a,b' .split(',')

这不起作用。字节字符串和unicode字符串在Py3k中的混合比以前更加不顺畅-故意使编码问题早于而不是迟于出错。告诉您您不能将字节字符串传递给urllib.parse。大概您正在执行POST请求,其中以形式编码的字符串作为内容主体进入cgi。内容主体仍然是字节字符串/流,因此现在与新的urllib冲突。



是的,这是cgi.py中的一个错误,也是2to3的另一个受害者对于新的字符串模型尚未正确修复的转换。它应该将传入的字节流转换为字符,然后再将其传递给urllib。



我是否提到Python 3.0的库(尤其是与Web相关的库)仍然很笨拙? :-)


  File "/usr/local/lib/python3.0/cgi.py", line 477, in __init__
    self.read_urlencoded()
  File "/usr/local/lib/python3.0/cgi.py", line 577, in read_urlencoded
    self.strict_parsing):
  File "/usr/local/lib/python3.0/urllib/parse.py", line 377, in parse_qsl
    pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
TypeError: Type str doesn't support the buffer API

Can anybody direct me on how to avoid this? I'm getting it through feeding data into the cgi.Fieldstorage and I can't seem to do it any other way.

解决方案

urllib is trying to do:

b'a,b'.split(',')

Which doesn't work. byte strings and unicode strings mix even less smoothly in Py3k than they used to — deliberately, to make encoding problems go wrong sooner rather than later.

So the error is rather opaquely telling you ‘you can't pass a byte string to urllib.parse’. Presumably you are doing a POST request, where the form-encoded string is coming into cgi as a content body; the content body is still a byte string/stream so it now clashes with the new urllib.

So yeah, it's a bug in cgi.py, yet another victim of 2to3 conversion that hasn't been fixed properly for the new string model. It should be converting the incoming byte stream to characters before passing them to urllib.

Did I mention Python 3.0's libraries (especially web-related ones) still being rather shonky? :-)

这篇关于Python 3.0 urllib.parse错误“类型str不支持缓冲区API”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆