我正在尝试将分块编码数据发布到 httpbin.org/post。我尝试了两个选项:Requests 和 httplib
使用请求
#!/usr/bin/env python
import requests
def gen():
l = range(130)
for i in l:
yield '%d' % i
if __name__ == "__main__":
url = 'http://httpbin.org/post'
headers = {
'Transfer-encoding':'chunked',
'Cache-Control': 'no-cache',
'Connection': 'Keep-Alive',
#'User-Agent': 'ExpressionEncoder'
}
r = requests.post(url, headers = headers, data = gen())
print r
使用 httplib
#!/usr/bin/env python
import httplib
import os.path
if __name__ == "__main__":
conn = httplib.HTTPConnection('httpbin.org')
conn.connect()
conn.putrequest('POST', '/post')
conn.putheader('Transfer-Encoding', 'chunked')
conn.putheader('Connection', 'Keep-Alive')
conn.putheader('Cache-Control', 'no-cache')
conn.endheaders()
for i in range(130):
conn.send(str(i))
r = conn.getresponse()
print r.status, r.reason
在这两种情况下,每当我分析 Wireshark 跟踪时,我都看不到多个块被发送。相反,我看到的是所有数据都在一个块中发送?我在这里错过了什么吗?