Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 543
Eduard Florinescu Avatar asked Sep 18 '12 09:09

Eduard Florinescu


People also ask

What is Urllib request in python?

The urllib. request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. See also. The Requests package is recommended for a higher-level HTTP client interface.

What is Urlopen in python?

Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols. Urllib is a package that collects several modules for working with URLs, such as: urllib.


2 Answers

A Python3 solution to this:

import urllib.request
with urllib.request.urlopen('http://www.google.com') as response:
    info = response.info()
    print(info.get_content_type())      # -> text/html
    print(info.get_content_maintype())  # -> text
    print(info.get_content_subtype())   # -> html
like image 197
DomTomCat Avatar answered Sep 22 '22 07:09

DomTomCat


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'
like image 41
Mikhail Karavashkin Avatar answered Sep 20 '22 07:09

Mikhail Karavashkin