如何使用python-magic通过Internet获取文件的文件类型? [英] How do I use python-magic to get the file type of a file over the Internet?
问题描述
m = magic.Magic()
m.from_buffer(thefile.read(1024))
但这一次,我无法下载该文件,因为图像可能是20兆字节。我想使用Python魔法来查找文件类型而不下载整个文件。
如果python-magic不能这样做。 .is是观察标题中MIME类型的下一个最好的方法吗?但是这个怎么准确?
我需要准确性。 您可以调用<$ c
$ b
thefile = urllib2.urlopen($ c> read(1024)
someURL)
然后,使用您现有的代码。 urlopen
会返回一个类似文件的对象,所以这个工作很自然。
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.
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.
这篇关于如何使用python-magic通过Internet获取文件的文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!