python requests module set timeout

Python requests module set timeout

If you are familiar with Python and web scraping, then you must have heard of the requests module. Requests is a simple and elegant Python library that allows you to send HTTP/1.1 requests extremely easily. It is a must-have tool for any Python developer. However, sometimes you may want to control how long your code waits for a response from a server. This is where the timeout parameter of the requests module comes in handy.

What is the timeout parameter?

The timeout parameter specifies the number of seconds that the request should wait for a response from the server. If no response is received within this time, the request will be aborted and an exception will be raised.

How to set the timeout parameter?

The timeout parameter can be set in two ways:

  • Globally: You can set a global default timeout for all requests made using the requests library by setting the value of the timeout parameter in the session object.
  • Per-request: You can set a timeout value for a specific request by passing the value of the timeout parameter in the request method.

Setting a global default timeout


import requests

session = requests.Session()
session.timeout = 5 # Set the default timeout to 5 seconds

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

In this example, we created a session object and set its timeout parameter to 5 seconds. Now, any request made using this session object will have a default timeout of 5 seconds.

Setting a timeout for a specific request


import requests

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

In this example, we set the timeout parameter to 5 seconds for the specific request made using the requests.get() method. If the response is not received within 5 seconds, an exception will be raised.