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

时间:2015-02-19 14:49:53

标签: http asynchronous tornado

我想使用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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我试过curl(天真),请求模块一切正常!但不是异步的(但对于龙卷风的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'