make a http head request in python

To make a HTTP Head request in Python, we can use the requests library. We'll start by installing requests if it isn't already installed:

main.py
!pip install requests
22 chars
2 lines

Once we have installed requests, we can use the head method to make the HTTP Head request:

main.py
import requests

url = "https://www.example.com"
response = requests.head(url)

print(response.status_code)
print(response.headers)
132 chars
8 lines

In this example, we make a HTTP Head request to https://www.example.com, and store the response object in a variable called response. We then print the status code and headers of the response.

Note that the head method returns a similar response object as the get method, except that the response body is not included. This makes it a good choice for cases where we only need information about the resource, and not the content of the response.

gistlibby LogSnag