Pycurl和io。StringIO——pycurl。错误:(23,“写体失败”)

时间:2021-01-31 20:26:57

I'm porting ebay sdk to python3 and I've stumbled upon the following issue.

我将ebay sdk移植到python3,我偶然发现了以下问题。

I'm using pycurl to send some HTTP requests. Here is how I configure it:

我正在使用pycurl发送一些HTTP请求。以下是我如何配置它:

    self._curl = pycurl.Curl()
    self._curl.setopt(pycurl.FOLLOWLOCATION, 1)
    self._curl.setopt(pycurl.URL, str(request_url))
    self._curl.setopt(pycurl.SSL_VERIFYPEER, 0)

    self._response_header = io.StringIO()
    self._response_body = io.StringIO()

    self._curl.setopt(pycurl.CONNECTTIMEOUT, self.timeout)
    self._curl.setopt(pycurl.TIMEOUT, self.timeout)

    self._curl.setopt(pycurl.HEADERFUNCTION, self._response_header.write)
    self._curl.setopt(pycurl.WRITEFUNCTION, self._response_body.write)

When I call self._curl.perform() I get the following error:

当我调用self._curl.perform()时,我得到以下错误:

pycurl.error: (23, 'Failed writing body (1457 != 1460)')

As far as I know this means that there is an issue with the write function, but I can't figure out what it is exactly. Could be related to migration from StringIO module to io, but I'm not sure.

据我所知,这意味着写函数有问题,但我不知道它到底是什么。可能与从StringIO模块到io的迁移有关,但我不确定。

UPD: I've tried the following:

UPD:我试过以下方法:

    def body(buf):
        self._response_body.write(buf)

    def header(buf):
        self._response_header.write(buf)

    self._curl.setopt(pycurl.HEADERFUNCTION, header)
    self._curl.setopt(pycurl.WRITEFUNCTION, body)

and it works. I've tried to do the same trick with lambdas (instead of defining those awkward function, but it didn't work.

和它的工作原理。我尝试过对lambdas执行同样的技巧(而不是定义那些笨拙的函数,但它不起作用。

1 个解决方案

#1


16  

I believe the problem is that pycurl no longer functions with StringIO like desired. A solution is to use io.BytesIO instead. You can then get information written into the buffer and decode it into a string.

我认为问题是pycurl不再像预想的那样使用StringIO。解决方法是使用io。BytesIO代替。然后,您可以将信息写入缓冲区并将其解码为字符串。

Using BytesIO with pycurl instead of StringIO:

用BytesIO和pycurl代替StringIO:

e = io.BytesIO()
c.setopt(pycurl.WRITEFUNCTION, e.write)

Decoding byte information from the BytesIO object:

从BytesIO对象解码字节信息:

htmlString = e.getvalue().decode('UTF-8')

You can use any type of decoding you want, but this should give you a string object you can parse.

您可以使用任何类型的解码,但这应该为您提供一个可以解析的字符串对象。

Hope this helps people using Python 3.

希望这能帮助人们使用Python 3。

#1


16  

I believe the problem is that pycurl no longer functions with StringIO like desired. A solution is to use io.BytesIO instead. You can then get information written into the buffer and decode it into a string.

我认为问题是pycurl不再像预想的那样使用StringIO。解决方法是使用io。BytesIO代替。然后,您可以将信息写入缓冲区并将其解码为字符串。

Using BytesIO with pycurl instead of StringIO:

用BytesIO和pycurl代替StringIO:

e = io.BytesIO()
c.setopt(pycurl.WRITEFUNCTION, e.write)

Decoding byte information from the BytesIO object:

从BytesIO对象解码字节信息:

htmlString = e.getvalue().decode('UTF-8')

You can use any type of decoding you want, but this should give you a string object you can parse.

您可以使用任何类型的解码,但这应该为您提供一个可以解析的字符串对象。

Hope this helps people using Python 3.

希望这能帮助人们使用Python 3。