SoFunction
Updated on 2025-03-02

Detailed analysis of various usages of request library in Python

introduction

In modern network applications, communicating with servers is a very basic and important function. PythonrequestsThe library is a very powerful and easy to use HTTP library that allows us to send HTTP requests to interact with web services. This article will introduce in detailrequestsThe use of libraries includes its basic concepts, common functions and some advanced usages.

Install the requests library

In userequestsBefore we can install it, we need to install it first. Can be passedpipCommand to install:

pip install requests

Basic concepts

HTTP request method

The HTTP protocol defines several request methods, the most common ones are:

  • GET: Obtain resources from the server.
  • POST: Submit data to the server, usually used to create new resources.
  • PUT: Update resources on the server.
  • DELETE: Delete resources on the server.

Request URL

The URL (Uniform Resource Locator) is a string used to locate resources. For example:/data

Request header

The request headers contain metadata about the request, such as content type, user agent, etc.

Request body

Body is usually used for POST and PUT requests, containing the data to be sent.

Basic usage

Send a GET request

Sending a GET request is one of the easiest operations. Here is an example:

import requests

response = ('')
print(response.status_code)  # Print status codeprint()         # Print response content

Send a POST request

When sending a POST request, it is usually necessary to pass some data. Here is an example:

import requests

url = '/post'
data = {'key': 'value'}
response = (url, data=data)
print(response.status_code)  # Print status codeprint(())       # Print the response content in JSON format

Pass the request header

Sometimes we need to add a custom request header to the request. Here is an example:

import requests

url = ''
headers = {'User-Agent': 'my-app/0.0.1'}
response = (url, headers=headers)
print(response.status_code)  # Print status codeprint()         # Print response content

Processing response

Status code

The status code of the response indicates the result of the request. Common status codes are:

  • 200: The request was successful.
  • 404: Resource not found.
  • 500: Internal server error.

Response content

The response content can be text, JSON, binary data, etc. Here are some examples:

import requests

response = ('')

# Get text contentprint()

# Get JSON contentprint(())

# Get binary contentprint()

Response header

The response header contains metadata about the response. Here is an example:

import requests

response = ('')
print()

Detailed explanation of advanced usage

In masteringrequestsAfter the basic usage of the library, we can further explore its advanced features. These advanced features can help us handle complex HTTP request scenarios more flexible and efficiently. The following is a detailed description of advanced usage.

1. Session Objects

Session objects allow us to keep certain parameters between multiple requests, such as cookies, headers, etc. Using session objects can simplify code and improve efficiency.

Example:

import requests

# Create a session objectsession = ()

# Update the default headers for the session({'User-Agent': 'my-app/0.0.1'})

# Send the first request and set a cookieresponse1 = ('/cookies/set/sessioncookie/123456789')
print()

# Send a second request to get the cookie set beforeresponse2 = ('/cookies')
print()

In this example, we create a session object and share headers and cookies between multiple requests. This ensures a consistent state between multiple requests.

2. Handling Redirects

By default,requestsThe library automatically handles redirects. We can passallow_redirectsParameters to control whether redirection is allowed.

Example:

import requests

# Redirection prohibitedresponse = ('', allow_redirects=False)
print(response.status_code)  # Print status codeprint(['Location'])  # Print the redirected URL

In this example, we send a GET request to, and redirection is prohibited. The status code of the response is 301, indicating that the request has been permanently redirected, and the response header isLocationThe field contains the redirected target URL.

3. Timeout Settings

When sending a request, we can set a timeout time to avoid long waits. The timeout time can be applied to the connection phase and the read phase.

Example:

import requests
from  import Timeout

try:
    # Set connection timeout and read timeout    response = ('/delay/10', timeout=(3, 5))
except Timeout:
    print('The request timed out')

In this example, we set a 3-second connection timeout and a 5-second read timeout. If the request fails to establish a connection within 3 seconds or fails to read the response data within 5 seconds, it will be thrownTimeoutabnormal.

4. Proxy Support

requestsThe library supports sending requests through a proxy. We can set up different proxy for different protocols (HTTP, HTTPS).

Example:

import requests

# Set up a proxyproxies = {
    'http': 'http://10.10.1.10:3128',
    'https': 'http://10.10.1.10:1080',
}

response = ('/ip', proxies=proxies)
print()

In this example, we set up proxy for HTTP and HTTPS requests respectively. Sending requests through proxy can hide the client's real IP address or bypass certain network restrictions.

5. File Uploads

requestsThe library supports uploading files. We can passfilesParameters to upload files.

Example:

import requests

# Prepare the filefiles = {'file': open('', 'rb')}

# Upload fileresponse = ('/post', files=files)
print()

In this example, we upload a name calledfile. The server will return information about uploading files.

6. Streaming Requests

For large files or responses that require real-time processing, we can use streaming requests. This can avoid loading large amounts of data into memory at one time.

Example:

import requests

# Streaming requestresponse = ('/stream/20', stream=True)

# Read the response content line by linefor line in response.iter_lines():
    if line:
        print(line)

In this example, we send a streaming request and read the response content line by line. This can effectively handle large files or real-time data streams.

7. Custom Authentication

requestsThe library supports custom authentication. We can inheritClass to implement custom authentication logic.

Example:

import requests
from  import AuthBase

# Custom authentication classclass TokenAuth(AuthBase):
    def __init__(self, token):
         = token

    def __call__(self, r):
        ['Authorization'] = f'Bearer {}'
        return r

# Send requests using custom authenticationresponse = ('/get', auth=TokenAuth('my-token'))
print()

In this example, we define a name calledTokenAuthcustom authentication class and used this class when sending requests. This allows various authentication logic to be flexibly implemented.

8. Handle Cookies

requestsThe library provides convenient ways to handle cookies. We can get, set and delete cookies.

Example:

import requests

# Create a session objectsession = ()

# Set a cookie('/cookies/set/sessioncookie/123456789')

# Get all cookiescookies = 
print(cookies)

# Delete a cookiecookies.clear_expired_cookies()
print(cookies)

In this example, we set a cookie through the session object and get and delete cookies. This allows easy management of session state.

9. Handle SSL certificate verification

requestsThe library will verify the SSL certificate by default. We can passverifyParameters to control whether to verify the SSL certificate.

Example:

import requests

# Disable SSL certificate verificationresponse = ('', verify=False)
print()

In this example, we disabled SSL certificate verification. This allows for easy testing of unsecure HTTPS sites, but security risks need to be paid attention to.

10. Custom Adapters

requestsThe library allows us to customize adapters to implement more complex request logic. We can inheritClass to implement custom adapters.

Example:

import requests
from  import HTTPAdapter

# Custom adapterclass MyAdapter(HTTPAdapter):
    def send(self, request, **kwargs):
        print(f'Sending request to {}')
        return super().send(request, **kwargs)

# Create a session object and use a custom adaptersession = ()
('https://', MyAdapter())

# Send a requestresponse = ('/get')
print()

In this example, we define a name calledMyAdaptercustom adapter and use this adapter in the session object. This allows you to easily implement custom request logic.

Summarize

This is the end of this article about the various usages of the request library in Python. For more related Python request library analysis content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!