Skip to content Skip to sidebar Skip to footer

How Do I Use Python-magic To Get The File Type Of A File Over The Internet?

Usually I would download it to StringIO object, then run this: m = magic.Magic() m.from_buffer(thefile.read(1024)) But this time , I can't download the file, because the image mig

Solution 1:

You can call read(1024) without downloading the whole file:

thefile = urllib2.urlopen(someURL)

Then, just use your existing code. urlopen returns a file-like object, so this works naturally.

Solution 2:

If it is one of the common image formats like png of jpg, and you see the server is a reliable one, then you can use the 'Content-Type' header to give what you are looking for.

But this is not as reliable as using the portion of the file and passing it to python-magic, because if server had not identified the proper format and it might have set it to application/octet-stream. This is more common with video formats, but pictures, I think Content-Type is okay.

Sorry, I can't find any statistics or research on Content-Type's accuracy. The suggested answer of downloading only part of the file is a good option too.

Post a Comment for "How Do I Use Python-magic To Get The File Type Of A File Over The Internet?"