urllib2 and timeout question

Gerard C Blais gerard.blais at gmail.com
Mon May 2 15:04:37 EDT 2005



I'm trying to get a page from a slow server. and i'm timing out.

I've tried 

socket.setdefaulttimeout9300), and 

urllib2.socket.setdefaulttimeout(300), and

 urllib2.socket.setdefauttimeout(300.0), 

 but all  seem to give up after 20 seconds.

What's the best (or a good) way to increase the timeout?

(the url works fine, but slowly, when pasted into a browser)

Thanks,

Gerry

============

Code below:

import time 
import urllib2
import socket

starting_time = time.clock()

socket.setdefaulttimeout(300)
print "timeout set"

lat             = 38.836971
lon             = -77.378228

starting_width  = 20
ending_width    = 0.01

index = 0
width = starting_width

while width > ending_width:
    width *= 0.3
    index += 1
    url = "http://tiger.census.gov/cgi-bin/mapper/map.gif?lat=" +
str(lat) + \
        "&lon=" + str(lon) + \
        "&wid=" + str(width) + \
        "&iht=719&iwd=843&&on=majroads&mlat=" + str(lat) + \
        "&mlon=" + str(lon) + \
        "&msym=redpin&mlabel=SMM&conf=mapnew.con"
    
    url_start_time = time.clock()
    print round(starting_time,0), url
    
    try:
        fd = urllib2.urlopen(url)
    except:
        print time.clock(), "timed out"



More information about the Python-list mailing list