SSL [SOLVED]: Returning an image from a Django view using django-sslserver

SSL [SOLVED]: Returning an image from a Django view using django-sslserver

Home Forums SSL SSL [SOLVED]: Returning an image from a Django view using django-sslserver

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36778

    Anonymous

    QuestionQuestion

    I’m trying to return an image from a Django 1.11 view while using django-sslserver and Pillow. Here’s a minimal view I made for testing.

    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
    

    In my template I use:

    <img src={% url "get_image" %} />
    

    In urls.py, I use:

    url(r'^get_image.png', get_image, name='get_image')
    

    The image response works fine with Django runserver but fails under both django-sslserver and runserver_plus from django-extensions. What I see in Chrome is a broken image icon and the error “ERR_CONTENT_LENGTH_MISMATCH”.

    When using django-sslserver I get the error:

    [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
    

    Does anyone know how I can make an image response work with django-sslserver or a similar solution for supporting SSL in a Django development environment? I’ve searched but not been able to find an example of someone having this particular problem.

    Thanks

    #36779

    Anonymous

    Accepted AnswerAnswer

    Since it’s not completely clear what Image.save() does in this context, could you try doing it this way?

    def get_image(request):
        image_data = open('oh_noes_cat.png', mode='r').read()
        return HttpResponse(image_data, content_type="image/png")
    

    Source: https://stackoverflow.com/questions/47982858/returning-an-image-from-a-django-view-using-django-sslserver
    Author: Mzzl
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.