[英]Processing chunked encoded HTTP POST requests in python (or generic CGI under apache)
[英]How to POST chunked encoded data in Python
我正在嘗試將分塊編碼數據發布到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
#!/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跟蹤時,我都看不到發送多個塊。 相反,我看到的是所有數據都是在一個塊中發送的? 我在這里錯過了什么嗎?
您發布的代碼不應該正常工作。 你仍然得到成功回復的原因是因為httpbin.org目前不支持分塊傳輸編碼。 請參閱錯誤https://github.com/kennethreitz/httpbin/issues/102 。
就像上面鏈接的Piotr一樣,你應該用十六進制寫出每個塊的長度,然后用塊本身寫出。
我為你的代碼編寫了一個例子。 http://httpbin.org/post端點有一個可用於測試的表單 。 這就是我生成chunk1
和chunk2
表單數據的地方。
import httplib
import time
chunk1 = "custname=bob&custtel=11111&custemail=bob%40email.com&si"
chunk2 = "ze=medium&topping=bacon&delivery=11%3A00&comments=if+you%27re+late+we+get+it+free"
if __name__ == "__main__":
conn = httplib.HTTPConnection('httpbin.org')
conn.connect()
conn.putrequest('POST', '/post')
conn.putheader('Transfer-Encoding', 'chunked')
conn.putheader('Content-Type', 'application/x-www-form-urlencoded')
conn.endheaders()
conn.send("%s\r\n" % hex(len(chunk1))[2:])
conn.send("%s\r\n" % chunk1)
time.sleep(1)
conn.send("%s\r\n" % hex(len(chunk2))[2:])
conn.send("%s\r\n" % chunk2)
time.sleep(1)
/* last chunk */
conn.send("0\r\n\r\n")
r = conn.getresponse()
print r.status, r.reason, r.read()
wireshark中的流看起來像下面這樣不正確,因為它不等待(注意尾隨0
)或解釋我們發送的請求體(注意json: null
):
POST /post HTTP/1.1
Host: httpbin.org
Accept-Encoding: identity
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded
37
custname=bob&custtel=11111&custemail=bob%40email.com&si
51
ze=medium&topping=bacon&delivery=11%3A00&comments=if+you%27re+late+we+get+it+free
HTTP/1.1 200 OK
Connection: close
Server: gunicorn/18.0
Date: Fri, 31 Oct 2014 10:37:24 GMT
Content-Type: application/json
Content-Length: 494
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "identity",
"Connect-Time": "2",
"Connection": "close",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"Total-Route-Time": "0",
"Transfer-Encoding": "chunked",
"Via": "1.1 vegur",
"X-Request-Id": "5053a365-ca6a-4c29-b97a-f7a6ded7f2d9"
},
"json": null,
"origin": "110.174.97.16",
"url": "http://httpbin.org/post"
}0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.