1) 场景,python
python使用request库,出现以下错误.python2.7, requests 2.5.1 git
ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))github
好好的东西,怎么忽然出现这个状况,蛋疼无比json
具体的堆栈调用服务器
就是使用requests的postapp
一看requests的源码,就发现,是在解析stream中的chunked的时候报错了.python2.7
补充知识: http的chunked请参考: http://blog.csdn.net/zhangboyj/article/details/6236780 curl
由于请求的代码是别人的服务器,没办法让对方修改.post
通常状况下http返回数据,都会有个Content-Length的东西.可是在chunked的状况下,就没了.url
这时候,咱们须要改变一下策略.
chunked是http1.1才有的东西,
咱们将http请求的版本,修改成1.0
使用curl命令能够添加一个参数 -0或者-1
curl -0 http://xx.com -v
能够看到提交请求的头HTTP/1.0
curl -1 http://xx.com -v
能够看到提交请求的头HTTP/1.1
如今为了保留大部分代码都不变的状况,须要修改requests发送http请求的时候,修改成1.0的http头
import httplib httplib.HTTPConnection._http_vsn = 10 httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
header = { 'Content-Type': 'application/json;', 'Host':'www.xxxx.com' } resp = requests.post(url=url, data='{}', headers= header ,timeout=120)
某些状况下,竟然必定要带Host.请留意
而后继续请求,经过wireshark抓包能够看到.
从返回的数据包里面,没有看到chunked的字样了,不过也没Content-Length
备注: https://github.com/ryanmcgrath/twython/issues/288
上面连接说更新requests版本到2.12.1就没问题了.