繁体   English   中英

Python UnicodeDecodeError:'ascii'编解码器无法解码位置12的字节0xd0:序数不在范围内(128)

[英]Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 12: ordinal not in range(128)

我正在尝试使用Django rest框架从基于类的视图中返回StreamingHttpResponse的文件。 但是,我得到了一个非常隐秘的错误消息,该消息带有不包含对我的代码的任何引用的堆栈跟踪:

16/Jun/2017 11:08:48] "GET /api/v1/models/49 HTTP/1.1" 200 0
Traceback (most recent call last):
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/handlers.py", line 179, in finish_response
    for data in self.result:
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/util.py", line 30, in __next__
    data = self.filelike.read(self.blksize)
  File "/Users/jonathan/anaconda/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 12: ordinal not in range(128)

[...]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/handlers.py", line 141, in run
    self.handle_error()
  File "/Users/jonathan/anaconda/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 88, in handle_error
    super(ServerHandler, self).handle_error()
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/handlers.py", line 368, in handle_error
    self.finish_response()
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/handlers.py", line 274, in write
    self.send_headers()
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "/Users/jonathan/anaconda/lib/python3.6/wsgiref/handlers.py", line 344, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

我的get方法如下所示:

def get(self, request, pk, format=None):
    """
    Get model by primary key (pk)
    """
    try:
        model = QSARModel.objects.get(pk=pk)
    except Exception:
        raise Http404
    filename = model.pluginFileName
    chunk_size = 8192
    response = StreamingHttpResponse(
                   FileWrapper( open(filename), chunk_size ),
                   content_type = 'application/zip' )
    return response

从谷歌搜索一下,我感觉到这与ASCII / UTF8编码有关,但是我不明白这对我的情况有何影响。 我正在处理一个二进制文件。 实际上,这是一个Java jar文件,但据我所知应该几乎是一个zip文件。 这是怎么回事,我在做什么错?

这与语言翻译有关。 Django存储系统使用非ascii文件名时。 因此,请在您的Apache envvars中添加以下行

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

https://code.djangoproject.com/wiki/django_apache_and_mod_wsgi#AdditionalTweaking

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM