Home  >  Article  >  Backend Development  >  How to Use HEAD Requests in Python 2 to Determine MIME Type Without Downloading Content?

How to Use HEAD Requests in Python 2 to Determine MIME Type Without Downloading Content?

Linda Hamilton
Linda HamiltonOriginal
2024-11-05 03:05:02199browse

How to Use HEAD Requests in Python 2 to Determine MIME Type Without Downloading Content?

HEAD HTTP Requests in Python 2: Retrieving Headers for MIME Type Determination

Question:

To determine the MIME type of a URL without downloading the content, how can a HEAD request be sent in Python 2?

Answer:

urllib2 offers a convenient approach to execute HEAD requests. It simplifies the process by parsing the URL, eliminating the need to manually split it into host name and path:

The headers can be accessed through response.info(). Additionally, you can retrieve the redirected URL using response.geturl():

The above is the detailed content of How to Use HEAD Requests in Python 2 to Determine MIME Type Without Downloading Content?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn