Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force urllib2 to time out?

Tags:

python

urllib2

I want to to test my application's handling of timeouts when grabbing data via urllib2, and I want to have some way to force the request to timeout.

Short of finding a very very slow internet connection, what method can I use?

I seem to remember an interesting application/suite for simulating these sorts of things. Maybe someone knows the link?

like image 350
alexgolec Avatar asked Nov 15 '10 20:11

alexgolec


2 Answers

I usually use netcat to listen on port 80 of my local machine:

nc -l 80

Then I use http://localhost/ as the request URL in my application. Netcat will answer at the http port but won't ever give a response, so the request is guaranteed to time out provided that you have specified a timeout in your urllib2.urlopen() call or by calling socket.setdefaulttimeout().

like image 176
ʇsәɹoɈ Avatar answered Oct 21 '22 04:10

ʇsәɹoɈ


You could set the default timeout as shown above, but you could use a mix of both since Python 2.6 in there is a timeout option in the urlopen method:

import urllib2
import socket

try:
    response = urllib2.urlopen("http://google.com", None, 2.5)
except URLError, e:
    print "Oops, timed out?"
except socket.timeout:
    print "Timed out!"

The default timeout for urllib2 is infinite, and importing socket ensures you that you'll catch the timeout as socket.timeout exception

like image 41
VKolev Avatar answered Oct 21 '22 03:10

VKolev