gpt4 book ai didi

python - 如何使用 Tornado 通过 http POST 发送压缩文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:33 25 4
gpt4 key购买 nike

我需要通过 HTTP 请求发送压缩的 JSON 文件

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?

最佳答案

实际上非常简单:传递给 HTTPRequestbody 必须是:

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

关于python - 如何使用 Tornado 通过 http POST 发送压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35651291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com