Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate timeout response

I'm creating a Python script that is always checking if a webapp is ok, because it was usual to send timeout response to several requests.

This script uses httplib:

conn = httplib.HTTPConnection("10.255.255.1")
conn.request("HEAD", "/")
ping = conn.getresponse()

Then simply analyses the http code returned.

I do not control the webapp, so I can't create a endpoint that would return whatever I want, so my question is: How can I simulate that I've received a timeout?

like image 561
pedrorijo91 Avatar asked Apr 19 '15 20:04

pedrorijo91


1 Answers

You can simulate some httplib errors by looking up some of the exceptions at here (specifically, look at the exceptions).

Very surprisingly, I noticed the timeout it throws is a socket.timeout; from a different package. Actually, I discovered this by giving an unrealistically small timeout as argument to httplib.HTTPConnection of 0.0001 seconds.

Thus, you can simulate the error by raising the socket.timeout:

def requestResultingInTimeout():
    # Pretend to request
    raise socket.timeout

Don't forget to import socket though.

You might handle it like such:

from socket import timeout
try:
    requestResultingInTimeout()
except timeout:
    print("Now that I got a timeout, I'll print this message instead")
like image 84
PascalVKooten Avatar answered Sep 22 '22 12:09

PascalVKooten