1

I'm doing pretty simple thing - get file from client and send it to process to another server:

import requests
import logging


logger = logging.getLogger(__name__)
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)

and everything works fine on my own PC and on dev server, but on production server this code crashes with 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)

All values are bytes string (I inserted logging to ensure) so there shouldn't be any unicode issues. Seems there is some environment influence, but i can't figure it out. Does anybody know what could cause such problem?

4

0 回答 0