user3580316 user3580316 - 1 year ago 164
Python Question

Python Request response time timeouts

I am trying to perform some benchmarking and having some issues with Request. The problem is that if the response time is high it throws some errors. How can I make it return the

if the
waits more than for instance 2 seconds.

time = requests.get('').elapsed.total_seconds()

if time < 1:
print "Low response time"
print "High reponse time"

Answer Source

Use timeout parameter of requests.get. requests.get will raise requests.exceptions.Timeout exception if request took more time than timeout value.

    resp = requests.get('', timeout=1.0)
except requests.exceptions.Timeout as e:
    print "High reponse time"
    print "Low response time"