文件上传时出现Unicode错误

时间:2016-01-29 16:56:33

标签: python django python-2.7

我正在编写一个watchdog应用来监控文件系统,并将创建和修改事件的文件(requests)发布到网址。

我继承了一个不稳定的Django应用程序,在linux和OSX(utf8编码文件)上测试时有点/有点工作。

现在我正在部署到Windows并看到UnicodeDecodeErrors:

第一个嫌疑人是视线中的这一行(第88行):

files = [io.StringIO(unicode(request.body))]

在我开始从Windows测试框发布之前,我们设法把事情放在一起:

def do_stuff(self, fname, mimetype):
    auth = self.get_auth()
    index_url = self.get_index_url()
    files = {
        "file": (fname, open(fname, 'rb'), mimetype, {'Expires': '0'})
   }
    resp = requests.post(
        index_url, auth=auth, files=files, headers={'Content-Type': "application/octet-stream"}
    )

如果我正在正确阅读请求文档,那就是将文件发布到api端点的方法。但我从windows获取UnicodeDecodeErrors并从linux上清除文件上传。

任何见解都表示赞赏。

Traceback是:

[ERROR] {2016-01-29 17:16:13,612 base.handle_uncaught_exception()@base.py:224 56331 4557533184}:
Internal Server Error: /_search/default/test_wd
Traceback (most recent call last):
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/utils/decorators.py", line 29, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in bound_func
    return func(self, *args2, **kwargs2)
  File "apps/utils/views.py", line 47, in dispatch
    return super(CSRFExemptMixin, self).dispatch(request, *args, **kwargs)
  File "apps/needle/views/base.py", line 71, in dispatch
    rv = super(RestApiView, self).dispatch(request, *args, **kwargs)
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/utils/decorators.py", line 29, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in bound_func
    return func(self, *args2, **kwargs2)
  File "apps/needle/views/base.py", line 234, in dispatch
    return super(BaseNeedleView, self).dispatch(request, *args, **kwargs)
  File "/Users/opencrowd/pyvenv/lib/python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
    return handler(request, *args, **kwargs)
  File "apps/needle/views/index_views.py", line 88, in post
    files = [io.StringIO(unicode(request.body))]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 254: ordinal not in range(128)

0 个答案:

没有答案