2016-02-26 51 views
0

我需要發送一個壓縮的JSON文件與HTTP請求如何發送一個壓縮文件,使用HTTP POST與龍捲風

def create_gzip(): 
    with open('articoli.json', 'rb') as f_in, gzip.open('articoli.json.gz', 'wb') as f_out: 
     shutil.copyfileobj(f_in, f_out) 
    return open('articoli.json.gz', 'rb') 

body = create_gzip() 
headers = tornado.httputil.HTTPHeaders({"content-type": "application/zip charset=utf-8"}) 
request = tornado.httpclient.HTTPRequest("http://localhost:8889/variazione", method='POST', headers=headers, body=body) 
http_response = http_client.fetch(request) 

但是當我嘗試這樣做,它提供了以下錯誤:

TypeError: Expected bytes, unicode, or None; got <type 'file'> 

如何發送文件?有辦法讓請求接受gzip嗎?

回答

0

它實際上很簡單:你傳遞給HTTPRequestbody必須是:

body=open('articoli.json.gz',"rb").read() 
tornado.httpclient.HTTPRequest("http://localhost:8889/variazione", method='POST', 
               headers=headers, body=body) 
0

您的body包含一個文件,而不是實際的數據。試試這個:

gzip_file = create_gzip() 
body = bytearray(gzip_file.read())