Finding MIME type for a data stream

Dave Angel d at davea.name
Thu Mar 8 17:11:26 EST 2012


On 03/08/2012 04:55 PM, 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.
>
> Also, it seems like image/png works for other types
> of image data, while image/foo does not, yet I'm afraid
> that not every browser will play along as nicely.
>
> Thanks!
>
> Tobiah

First step, ask the authors of the database what format of data this 
blob is in.

Failing that, write the same data locally as a binary file, and see what 
application can open it.  Or if you're on a Linux system, run file on 
it.  "file" can identify most data formats (not just images) just by 
looking at the data.

That assumes, of course, that there's any consistency in the data coming 
out of the database.  What happens if next time this blob is an Excel 
spreadsheet?

-- 

DaveA




More information about the Python-list mailing list