首页 瞎搞

使用Python的requests包时报错:
requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
按照网上的在requests请求后面添加 steam=True 无效

查阅资料得知Transfer-Encoding:chunked是HTTP 1.1的新特性,chunked编码的基本方法是将大块数据分解成多块小数据,每块都可以自指定长度。
这里出现问题是因为服务器支持的是HTTP 1.0,而requests默认使用的是HTTP 1.1,兼容存在问题,所以一种解决办法是将HTTP协议版本设置为1.0:

  • Python2
import httplib
httplib.HTTPConnection._http_vsn = 10
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
  • Python3
import http.client
http.client.HTTPConnection._http_vsn = 10
http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'

参考:
python requests接收chunked编码问题
how-to-tell-the-http-server-to-not-send-chunked-encoding
requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)'



文章评论

    Cat 访客ChromeWindows
    2020-12-10 9:31   回复

    undefined