python requests http library

Python Requests HTTP Library

Python Requests is a popular third-party HTTP library for Python. It allows us to send HTTP requests using Python without the need for manual socket programming. With Requests, we can send HTTP/1.1 requests and handle responses easily.

Installation

To install Requests, we can use pip, which is a package manager for Python:


pip install requests

Sending HTTP requests

We can send HTTP requests using the requests module. We start by importing it:


import requests

To send a GET request, we use the get() function:


response = requests.get('https://www.example.com')
print(response.content)

The response will be returned as a Response object, which contains information such as the response status code, headers, and content. We can access the content of the response using the content attribute.

HTTP methods

Requests supports all HTTP methods, including GET, POST, PUT, DELETE, and more. To send a POST request, we use the post() function:


response = requests.post('https://www.example.com/post', data={'key': 'value'})
print(response.status_code)

We can also send data in the request body using the json() function:


response = requests.post('https://www.example.com/post', json={'key': 'value'})

Request headers

We can add headers to our requests by passing a dictionary to the headers parameter:


headers = {'Authorization': 'Bearer token'}
response = requests.get('https://www.example.com', headers=headers)

HTTP authentication

Requests supports various types of authentication, including basic authentication, digest authentication, and OAuth. To use basic authentication, we can pass a tuple of the form (username, password) to the auth parameter:


auth = ('user', 'pass')
response = requests.get('https://www.example.com', auth=auth)

HTTP cookies

Requests automatically manages cookies for us. We can access the cookies of a response using the cookies attribute:


response = requests.get('https://www.example.com')
print(response.cookies)

We can also send cookies in our requests by passing a dictionary to the cookies parameter:


cookies = {'name': 'value'}
response = requests.get('https://www.example.com', cookies=cookies)

Error handling

Requests raises exceptions for errors such as connection errors, timeouts, and HTTP errors. We can catch these exceptions using a try-except block:


try:
    response = requests.get('https://www.example.com')
    response.raise_for_status()
except requests.exceptions.RequestException as e:
    print(e)

The raise_for_status() function raises an exception if the response status code is not in the 200 range.

Conclusion

Python Requests is a powerful and easy-to-use HTTP library for Python. It provides a simple and intuitive interface for sending HTTP requests and handling responses. With its support for various features such as authentication, cookies, and error handling, it is an essential tool for any Python developer working with HTTP.