2013-07-15 29 views
3

我想將分塊編碼數據發佈到httpbin.org/post。我嘗試了兩種選擇:請求和httplib的如何在Python中分塊編碼數據

使用要求

#!/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的痕跡,我沒有看到發送多個塊。相反,我所看到的是所有數據都是以單個塊的形式發送的?我在這裏錯過了什麼嗎?

+0

[如何強制http.client在python中發送chunked-encoding HTTP主體?](http://stackoverflow.com/q/9237961/95735) –

+2

你確定嗎?在Wireshark中選擇單個HTTP消息,您應該能夠擴展超文本傳輸​​協議部分。該擴展部分應該有另一個名爲「HTTP chunked response」的子頭,它包含你的數據。 – Lukasa

+1

@Lukasa:是的,你說得對。出於某種原因,我對Wireshark中分塊數據如何出現的理解是有缺陷的。我認爲它總是作爲一個單獨的數據包出現。謝謝你的時間。 –

回答

2

您發佈的代碼不應該正常工作。您仍然得到成功迴應的原因是因爲httpbin.org目前不支持分塊傳輸編碼。見bug https://github.com/kennethreitz/httpbin/issues/102

與上面鏈接的帖子Piotr一樣,您應該用十六進制寫出每個塊的長度,然後寫出塊本身的長度。

我爲你的代碼殺了一個例子。​​端點有a form that you can use for testing。這就是我生成chunk1chunk2表單數據的地方。

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