簡體   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