How do I get a content-type of a file in Python? (with url..)

content-type, http, python, url

Solution

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)

Problem

Suppose I haev a video file: http://mydomain.com/thevideofile.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'] ```

Original source