[Python-es] Guardar y recuperar imagen desde Postgres

Alberto Valverde alberto en toscat.net
Sab Feb 2 13:35:22 CET 2008


Marcos Alcazar wrote:
> Sip, asi es... He probado con write() y con 'image/jpeg'. Creo que es eso.
> el write() o el writeln() me deben estar convirtiendo esos bytes en ascii o
> algo por el estilo.
> 
> Estoy un tanto atascado, no?

Hmm, le he echado un ojo a las fuentes d ewebkit y parece que en vez de
heredar de Page deberías heredar de HTTPContent directamente para
producir salida que no sea HTML. Según el docstring:

"""Content producing HTTP servlet.

HTTPContent is a type of HTTPServlet that is more convenient for
Servlets which represent content generated in response to
GET and POST requests. If you are generating HTML content, you
you probably want your servlet to inherit from Page, which contains
many HTML-related convenience methods.

If you are generating non-HTML content, it is appropriate to inherit
from this class directly.
(...)
"""

Así a ojo, tal vez algo así funcione:

class MiControladorDeImagenes(HTTPResponse):
    def defaultAction(self):
        obj = get_mi_imagen()
        # poner la cabecera content-type
        self.write(obj.data)

No uses writeln ya que inserta retornos de carro.

Si sigues atascado podrías usar la extensión de firefox Live HTTP
Headers o firebug o esnifar el tráfico para comprobar que las cabeceras
y el cuerpo de la respuesta son correctas.

Espero te ayude,
Alberto



Más información sobre la lista de distribución Python-es