set timeout in python requests

Understanding the timeout parameter in Python requests

If you're working with web scraping, making API calls or any other HTTP requests in Python, you must have come across the Python Requests library. It is a popular Python library used to send HTTP requests to a website and retrieve data from it. One of the essential parameters in requests is the timeout parameter.

The timeout parameter specifies the number of seconds before the request times out. In other words, if the server doesn't respond within a specified time, requests will raise a Timeout exception.

How to set the timeout parameter in Python Requests

Setting the timeout parameter in requests is quite simple. You can set it globally for all requests or specifically for individual requests.

Globally setting a timeout parameter

To set a timeout parameter globally for all requests in your script, you can use the 'timeout' parameter in the session() method.


    import requests
    
    session = requests.session()
    session.timeout = 10  # The timeout is set to 10 seconds
    

Setting a timeout parameter for an individual request

To set a timeout parameter for an individual request, you can pass the 'timeout' parameter to the request method.


    import requests
    
    response = requests.get('https://www.example.com', timeout=5)  # The timeout is set to 5 seconds
    

What happens when a request times out?

When a request times out, requests will raise a Timeout exception.


    import requests
    
    try:
        response = requests.get('https://www.example.com', timeout=5)
    except requests.exceptions.Timeout:
        print('The request timed out.')
    

It is important to note that the timeout parameter is not a guarantee that a request will not take longer than the specified time. If the server is under heavy load, it may take longer to respond, and the request may still time out.