Finding MIME type for a data stream

Peter Otten __peter__ at web.de
Fri Mar 9 05:50:04 EST 2012


Tobiah wrote:

> I'm pulling image data from a database blob, and serving
> it from a web2py app.  I have to send the correct
> Content-Type header, so I need to detect the image type.
> 
> Everything that I've found on the web so far, needs a file
> name on the disk, but I only have the data.
> 
> It looks like the 'magic' package might be of use, but
> I can't find any documentation for it.

After some try-and-error and a look into example.py:

>>> m = magic.open(magic.MAGIC_MIME_TYPE)
>>> m.load()
0
>>> sample = open("tmp.png").read()
>>> m.buffer(sample)
'image/png'





More information about the Python-list mailing list