2016-02-26 50 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())