Home > Back-end >  How to tell if requests.Session() is good?
How to tell if requests.Session() is good?

Time:05-20

I have the following session-dependent code which must be run continuously.

Code

import requests
http = requests.Session()

while True:
    # if http is not good, then run http = requests.Session() again
    response = http.get(....)
    # process respons
    # wait for 5 seconds

Note: I moved the line http = requests.Session() out of the loop.

Issue

How to check if the session is working?

An example for a not working session may be after the web server is restarted. Or loadbalancer redirects to a different web server.

CodePudding user response:

I would flip the logic and add a try-except.

import requests
http = requests.Session()

while True:
    try:
        response = http.get(....)
    except requests.ConnectionException:
        http = requests.Session()
        continue
    # process respons
    # wait for 5 seconds  

See this answer for more info. I didn't test if the raised exception is that one, so please test it.

CodePudding user response:

The requests.Session object is just a persistence and connection-pooling object to allow shared state between different HTTP request on the client-side.

If the server unexpectedly closes a session, so that it becomes invalid, the server probably would respond with some error-indicating HTTP status code.

Thus requests would raise an error. See Errors and Exceptions:

All exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException.

See the extended classes of RequestException.

Approach 1: implement open/close using try/except

Your code can catch such exceptions within a try/except-block. It depends on the server's API interface specification how it will signal a invalidated/closed session. This signal response should be evaluated in the except block.

Here we use session_was_closed(exception) function to evaluate the exception/response and Session.close() to close the session correctly before opening a new one.

import requests

# initially open a session object
s = requests.Session()

# execute requests continuously
while True:
    try:
        response = s.get(....)
        # process response
    except requests.exceptions.RequestException as e:
        if session_was_closed(e):
            s.close()  # close the session
            s = requests.Session()  # opens a new session
        else:
            # process non-session-related errors
    # wait for 5 seconds

Depending on the server response of your case, implement the method session_was_closed(exception).

Approach 2: automatically open/close using with

From Advanced Usage, Session Objects:

Sessions can also be used as context managers:

with requests.Session() as s:
    s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')

This will make sure the session is closed as soon as the with block is exited, even if unhandled exceptions occurred.

  • Related