使用python-requests发送文件

时间:2013-11-11 05:43:21

标签: python file environment python-requests

我做的很简单 - 从客户端获取文件并将其发送到另一台服务器:

data = request.environ['wsgi.input'].read()

url = request.cfg.DOC_PARSE_URL
params = {'AddedPath': str(request.cfg.FORM_TEMP_URL+uid+'/'), 'Button': 'Generate'}

logger.warning('url:'+repr(url))
for k in params:
    logger.warning('params:'+repr(k)+' '+repr(params[k]))

logger.warning('data:'+repr(type(data))+str(len(data)))
#logger.warning('data:'+data)

files = {'WordFile': ('process.doc', data)}

r = requests.post(url, files=files, data=params, stream=False)

并且在我自己的PC和开发服务器上一切正常,但在生产服务器上,此代码与UnicodeDecodeError崩溃:

Traceback (most recent call last):
File "/sites/mo/admin/utils/dispatcher.py", line 492, in __call__
    response = self.view_map[endpoint](request, **params)
File "/sites/mo/admin/utils/admin_views.py", line 231, in parse_doc
    r = requests.post(url, files=files, data=params, stream=False)
File "/sites/mo/admin/../third_party/requests/api.py", line 88, in post
    return request('post', url, data=data, **kwargs)
File "/sites/mo/admin/../third_party/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
File "/sites/mo/admin/../third_party/requests/sessions.py", line 360, in request
    resp = self.send(prep, **send_kwargs)
File "/sites/mo/admin/../third_party/requests/sessions.py", line 463, in send
    r = adapter.send(request, **kwargs)
File "/sites/mo/admin/../third_party/requests/adapters.py", line 292, in send
    timeout=timeout
File "/sites/mo/admin/../third_party/requests/packages/urllib3/connectionpool.py", line 428, in urlopen
    body=body, headers=headers)
File "/sites/mo/admin/../third_party/requests/packages/urllib3/connectionpool.py", line 283, in _make_request
    conn.request(method, url, **httplib_request_kw)
File "/usr/lib64/python2.7/httplib.py", line 946, in request
    self._send_request(method, url, body, headers)
File "/usr/lib64/python2.7/httplib.py", line 987, in _send_request
    self.endheaders(body)
File "/usr/lib64/python2.7/httplib.py", line 940, in endheaders
    self._send_output(message_body)
File "/usr/lib64/python2.7/httplib.py", line 801, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 739: ordinal not in range(128)

所有值都是字节字符串(我插入日志以确保)所以不应该有任何unicode问题。似乎有一些环境影响,但我无法弄清楚。有谁知道会导致这样的问题?

0 个答案:

没有答案