How to get file content type in Python? (with url ..)

Suppose I have a video file:

http://mydomain.com/thevideofile.mp4

How to get the title and content type of this file? With Python But I do not want to download the whole file. I want him back:

video/mp4 

Edit: this is what I did. What do you think?

 f = urllib2.urlopen(url) params['mime'] = f.headers['content-type'] 
+6
content-type python url
source share
3 answers

Same:

 >>> import httplib >>> conn = httplib.HTTPConnection("mydomain.com") >>> conn.request("HEAD", "/thevideofile.mp4") >>> res = conn.getresponse() >>> print res.getheaders() 

This will load and print the headers because it makes a HEAD request:

requests a response identical to the one that matches the GET request, but with no response body. This is useful for retrieving meta-information written in response headers, without the need to transport all content.

(via Wikipedia )

+12
source share

This is a response to a higher level than that of Brian. Using the urllib machine has the usual benefits, such as automatic forwarding, etc.

 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 
+4
source share

you can get the type of video using the info () method or dict headers

 f=urllib2.urlopen(url) print f.headers['Content-Type'] print f.info() 

Run with a randomly selected avi file running on the network, which is more than 600 MB

 $ 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 “take bandwidth” only when the file is downloaded, i.e. packets are sent to and from the socket.

0
source share

All Articles