简体   繁体   中英

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

I am trying to return a file in a StreamingHttpResponse from a class based view using Django rest framework. However I get a very cryptic error message with a stack trace that does not contain any references to my code:

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

My get method looks like this:

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

From googling a bit I get the feeling that this is related to ASCII / UTF8 encoding but I don't understand how that applies to my situation. I am dealing with a binary file. In fact it is a Java jar file but that should be pretty much a zip file as far as I understand. What is going on here and what am I doing wrong?

this is related to language translation. when non ascii filenames with the django storage system. So add following lines in your 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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