StreamingHttpResponse
のファイルをDjango restフレームワークを使用してクラスベースのビューから返そうとしています。しかし、私は私のコードへの参照が含まれていないスタックトレースと非常に不可解なエラーメッセージが表示されます。私のgetメソッドは次のようになりPython UnicodeDecodeError: 'ascii'コーデックは、位置12の0xd0バイトをデコードできません:序数が範囲外です(128)
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
:ビットをグーグルから
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ファイルでなければなりません。ここで何が起こっているのですか、何が間違っていますか?
あなたのapache envvarsの中で、次の行を追加します。デコードが発生する)、テキストでは解決されません... –
そうです、それです。したがって、Pythonは他のものが指定されていなければ、物事はASCIIであると考えます。それはちょっと驚くべきことです... – jonalv
- ライブラリーが明示的に実行するデコードや、システムのデフォルトエンコードが何であっても... –