python requests with session

Python Requests with Session

Python requests is a popular library that provides an easy way to send HTTP/1.1 requests using Python. It allows users to send HTTP/1.1 requests with headers, form data, multipart files, and other parameters. Requests also comes with support for cookies and sessions, which can be useful when working with websites that require authentication.

Getting Started with Requests Session

Using a session object in Requests allows you to persist certain parameters across requests. This includes cookies, headers, and authentication details. To start using sessions in requests, you simply create a Session object:


import requests

s = requests.Session()

Once you have a session object, you can use it to send HTTP requests:


response = s.get('http://httpbin.org/get')

The above code sends a GET request using the session object. Requests will now store the cookies returned by the server in the session object for future requests.

Session Authentication

Sessions can be used to handle authentication across multiple requests. For example, if a website requires a user to log in before accessing certain pages, you can use a session object to keep the user logged in across multiple requests:


payload = {'username': 'myusername', 'password': 'mypassword'}
s.post('http://httpbin.org/post', data=payload)

response = s.get('http://httpbin.org/get')

In the above example, we first send a POST request to the login page with the username and password data. Requests will store the cookies returned by the server in the session object. Then we send a GET request to a protected page. Requests will use the stored cookies to authenticate the request and access the protected page.

Session Headers

Session headers can be used to set custom headers that will be sent with every request made using the session object:


s.headers.update({'x-test': 'true'})

response = s.get('http://httpbin.org/headers')

The above code sets a custom 'x-test' header in the session object. Requests will send this header with every request made using the session object.

Session Timeouts

Sessions can be configured with timeouts to limit the amount of time that a request will wait for a response from the server:


s = requests.Session()
s.mount('http://', requests.adapters.HTTPAdapter(max_retries=3))
s.timeout = 10

response = s.get('http://httpbin.org/get')

In the above code, we set a timeout of 10 seconds for all requests made using the session object. If a request takes longer than 10 seconds to complete, Requests will raise a Timeout exception. We also add an HTTPAdapter with max_retries set to 3, which will retry failed requests up to 3 times before giving up.

Conclusion

Python requests with session is a powerful tool that allows you to easily manage cookies, headers, and authentication details across multiple requests. It can be particularly useful when working with websites that require authentication or when you need to persist data across multiple requests. By using sessions in Requests, you can simplify your code and make it more maintainable.