Search code examples
pythonpython-2.7urllib

Python: How to get the Content-Type of an URL?


I need to get the content-type of an internet(intranet) resource not a local file. How can I get the MIME type from a resource behind an URL:

I tried this:

res = urllib.urlopen("http://www.iana.org/assignments/language-subtag-registry")
http_message = res.info()
message = http_message.getplist()

I get: ['charset=UTF-8']

How can I get the Content-Type, can be done using urllib and how or if not what is the other way?


Solution

  • res = urllib.urlopen("http://www.iana.org/assignments/language-subtag-registry" )
    http_message = res.info()
    full = http_message.type # 'text/plain'
    main = http_message.maintype # 'text'