我在尝试使用django-sslserver和Pillow时从Django 1.11视图返回图像。这是我为测试所做的最小视图。
def get_image(request):
img = Image.open('oh_noes_cat.png', mode='r')
response = HttpResponse(content_type='image/png')
img.save(response, 'png')
return response
在我的模板中,我使用:
<img src={% url "get_image" %} />
在urls.py中,我使用:
url(r'^get_image.png', get_image, name='get_image')
图像响应与Django runserver一起运行正常,但在django-extensions下django-sslserver和runserver_plus都失败了。我在Chrome中看到的是一个损坏的图片图标和错误&#34; ERR_CONTENT_LENGTH_MISMATCH&#34;。
使用django-sslserver时出现错误:
[26/Dec/2017 18:55:39] "GET /get_image.png HTTP/1.1" 200 0
Traceback (most recent call last):
File "/usr/lib/python3.5/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/lib/python3.5/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.5/wsgiref/handlers.py", line 279, in write
self._write(data)
File "/usr/lib/python3.5/wsgiref/handlers.py", line 453, in _write
result = self.stdout.write(data)
File "/usr/lib/python3.5/socket.py", line 593, in write
return self._sock.send(b)
File "/usr/lib/python3.5/ssl.py", line 861, in send
return self._sslobj.write(data)
File "/usr/lib/python3.5/ssl.py", line 586, in write
return self._sslobj.write(data)
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1844)
[26/Dec/2017 18:55:39] "GET /get_image.png HTTP/1.1" 500 59
有没有人知道我如何使用django-sslserver或类似的解决方案在Django开发环境中支持SSL?我已经搜索过,但找不到有人遇到这个特殊问题的例子。
由于
答案 0 :(得分:0)
由于Image.save()在此上下文中的作用并不完全清楚,您可以尝试这样做吗?
def get_image(request):
image_data = open('oh_noes_cat.png', mode='r').read()
return HttpResponse(image_data, content_type="image/png")