Python 3.0 urllib.parse 错误“Type str 不支持缓冲区 API”

发布于 2024-07-14 01:44:10 字数 497 浏览 5 评论 0原文

  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

有人可以指导我如何避免这种情况吗? 我通过将数据输入 cgi.Fieldstorage 来获取它,但我似乎无法以任何其他方式做到这一点。

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杯别 2024-07-21 01:44:10

urllib 正在尝试做:

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

这不起作用。 字节字符串和 unicode 字符串在 Py3k 中的混合比以前更不顺畅——故意让编码问题迟早出现问题。

所以这个错误相当不透明地告诉你“你不能将字节字符串传递给 urllib.parse”。 假设您正在执行 POST 请求,其中表单编码的字符串作为内容主体进入 cgi; 内容主体仍然是字节字符串/流,因此它现在与新的 urllib 发生冲突。

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

我是否提到过 Python 3.0 的库(尤其是与 Web 相关的库)仍然相当糟糕? :-)

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? :-)

明媚殇 2024-07-21 01:44:10

来自Python教程( http://www.python.org/doc/3.0/ tutorial/stdlib.html )有一个使用 urlopen 方法的示例。 它会引发同样的错误。

for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
    if 'EST' in line or 'EDT' in line:  # look for Eastern Time
        print(line)

您需要使用 str 函数将字节事物转换为具有正确编码的字符串。 如下:

for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
    lineStr = str( line, encoding='utf8' )
    if 'EST' in lineStr or 'EDT' in lineStr:  # look for Eastern Time
        print(lineStr)

From the python tutorial ( http://www.python.org/doc/3.0/tutorial/stdlib.html ) there is an example of using urlopen method. It raises the same error.

for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
    if 'EST' in line or 'EDT' in line:  # look for Eastern Time
        print(line)

You'll need to use the str function to convert the byte thingo to a string with the correct encoding. As follows:

for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
    lineStr = str( line, encoding='utf8' )
    if 'EST' in lineStr or 'EDT' in lineStr:  # look for Eastern Time
        print(lineStr)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文