How do I get a content-type of a file in Python? (with url..)
Suppose I haev a video file:
http://mydomain.com/thevide开发者_开发问答ofile.mp4
How do I get the header and the content-type of this file? With Python. But , I don't want to download the entire file. i want it to return:
video/mp4
Edit: this is what I did. What do you think?
f = urllib2.urlopen(url)
params['mime'] = f.headers['content-type']
Like so:
>>> import httplib
>>> conn = httplib.HTTPConnection("mydomain.com")
>>> conn.request("HEAD", "/thevideofile.mp4")
>>> res = conn.getresponse()
>>> print res.getheaders()
That will only download and print the headers because it is making a HEAD request:
Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
(via Wikipedia)
This is a higher level answer than Brian's. Using the urllib machinery has the usual advantages such as handling redirects automatically and so on.
import urllib2
class HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"
url = "http://mydomain.com/thevideofile.mp4"
head = urllib2.urlopen(HeadRequest(url))
head.read() # This will return empty string and closes the connection
print head.headers.maintype
print head.headers.subtype
print head.headers.type
you can get the video type using the info() method or the headers dict
f=urllib2.urlopen(url)
print f.headers['Content-Type']
print f.info()
A test run with an randomly selected avi file googled on the net that is more than 600Mb
$ cat test.py
#!/usr/bin/env python
import urllib2
url="http://www.merseypirates.com/rjnsteve/rjnsteve/oem16.avi"
f=urllib2.urlopen(url)
print f.headers['Content-Type']
$ time python test.py
video/x-msvideo
real 0m4.931s
user 0m0.115s
sys 0m0.042s
it will only "take up bandwidth" when the file is actually downloaded , ie packets are being sent to and from the socket.
精彩评论