问题描述
我正在将ebay sdk移植到python3,我偶然发现了以下问题。
我使用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)
当我调用self._curl.perform以下错误:
pycurl.error:(23,'写错身体(1457!= 1460)')
据我所知,这意味着写函数有一个问题,它是什么。可能与从StringIO模块迁移到io有关,但我不确定。
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)
我试图做同样的伎俩与lambdas(而不是定义那些尴尬的功能,但它没有工作。
我相信问题是pycurl不再像以前一样使用StringIO了,一个解决方案是使用io.BytesIO,然后你可以把信息写入缓冲区并解码成字符串。
使用BytesIO与pycurl而不是StringIO:
e = io.BytesIO()
c.setopt(pycurl.WRITEFUNCTION,e.write)
从BytesIO对象解码字节信息: / p>
htmlString = e.getvalue()。decode('UTF-8')
您可以使用任何类型的解码,但这应该提供一个您可以解析的字符串对象。
希望这有助于人们使用Python 3。
I'm porting ebay sdk to python3 and I've stumbled upon the following issue.
I'm using pycurl to send some HTTP requests. Here is how I configure it:
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:
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.
UPD:I've tried the following:
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.
解决方案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.
Using BytesIO with pycurl instead of StringIO:
e = io.BytesIO() c.setopt(pycurl.WRITEFUNCTION, e.write)
Decoding byte information from the BytesIO object:
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.
这篇关于Pycurl和io.StringIO - pycurl.error:(23,'写失败的身体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!