How do I use python-magic to get the file type of

2019-06-25 11:45发布

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 might be 20 Megabytes. I want to use Python magic to find the file type without downloading the entire file.

If python-magic can't do it...is the next best way to observe the mime type in the headers? But how accurate is this??

I need accuracy.

2条回答
孤傲高冷的网名
2楼-- · 2019-06-25 12:18

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.

查看更多
Evening l夕情丶
3楼-- · 2019-06-25 12:39

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.

查看更多
登录 后发表回答