gpt4 book ai didi

http - 如何使用 Tornado HTTPRequest 发布原始文件

转载 作者:可可西里 更新时间:2023-11-01 15:30:09 26 4
gpt4 key购买 nike

我想使用 Tornado (AsyncHTTPClient) 在 POST 请求中发送一些数据

rec_body = {'source': self.request.body, 'top': str(self.config["top"]), 'model': self.config["model"]}

其中 self.request.body 是原始二进制文件(图像)。

我试着这样做:

http_client = AsyncHTTPClient()
rec_body = {'source': self.request.body, 'top': str(self.config["top"]), 'model': self.config["model"]}
request = HTTPRequest( url = os.path.join(self.config["dest_addr"], self.config["sub_sect"]) , method='POST', body =rec_body)
result = http_client.fetch( request, callback=self.handle_request)

但得到了这个错误

  File "/usr/local/lib/python2.7/dist-packages/tornado/httpclient.py", line 424, in __init__
self.body = body
File "/usr/local/lib/python2.7/dist-packages/tornado/httpclient.py", line 468, in body
self._body = utf8(value)
File "/usr/local/lib/python2.7/dist-packages/tornado/escape.py", line 203, in utf8
"Expected bytes, unicode, or None; got %r" % type(value)
TypeError: Expected bytes, unicode, or None; got <type 'dict'>
ERROR:tornado.access:500 POST /upload (192.168.72.84) 13.14ms

我做错了什么?

最佳答案

我尝试了 curl(天真地),请求模块一切正常,但不是异步的。对于 Tornado 的 AsyncHTTPClient 有很好的 recipe from flickr .

Deals with multipart POST requests.
The code is adapted from the recipe found at :
http://code.activestate.com/recipes/146306/
No author name was given.
Author : Alexis Mignon (c)
email : alexis.mignon@gmail.Com
Date : 06/08/2011

代码如下:

import mimetypes

from tornado.gen import coroutine, Return
from tornado.httpclient import HTTPRequest

from tornado_flickrapi.httpclient import fetch


@coroutine
def posturl(url, fields, files):
try:
response = yield post_multipart(url, fields, files)
except Exception as e:
raise e
raise Return(response)


@coroutine
def post_multipart(url, fields, files):
"""
Post fields and files to an http host as multipart/form-data.
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be
uploaded as files.
Return the server's response page.
"""
content_type, body = encode_multipart_formdata(fields, files)
headers = {"Content-Type": content_type, 'content-length': str(len(body))}
request = HTTPRequest(url, "POST", headers=headers, body=body, validate_cert=False)

try:
response = yield fetch(request)
except Exception as e:
raise e

raise Return(response)


def encode_multipart_formdata(fields, files):
"""
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be
uploaded as files.
Return (content_type, body) ready for httplib.HTTP instance
"""
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = '\r\n'
L = []
for (key, value) in fields:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for (key, filename, value) in files:
filename = filename.encode("utf8")
L.append('--' + BOUNDARY)
L.append(
'Content-Disposition: form-data; name="%s"; filename="%s"' % (
key, filename
)
)
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body


def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'

关于http - 如何使用 Tornado HTTPRequest 发布原始文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28609345/

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