[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

python urllib2 timeout problem



I wonder whether this is a bug for python urllib2.
The following is the code I used:
  
    target = protocol + "://" + location + directory + target_file
    start = datetime.now()
    try:
        urlrequest = urllib2.Request(target)
        urlresponse = urllib2.urlopen(urlrequest, timeout=4)

        urldata = urlresponse.read()
       
    except (urllib2.URLError, socket.timeout):
        return "ERR"

    stop = datetime.now()
    time_difference = stop - start
    print("\ntime diff is " + str(time_difference.total_seconds()))

According to the definition of socket.timeout, time_difference will
be no more than 4 seconds or no part of output.

However, when I print the variable: time_difference.total_seconds() ,
I found that the result commonly is greater than 4 seconds.

I put my detailed code at the Bitbucket:
            https://bitbucket.org/mudongliang/apt-spy-python/branch/timecheck
And the code is in the file : apt-spy

The following is the part of my output:
mdl@NjuMdl:~/Repos/Bitbucket/apt-spy-python$ sudo ./apt-spy -d testing -c CN --http-only
Benchmarking 8 debian-mirrors:
Mirrorname                               | HTTP Speed
=> checking ftp.cn.debian.org (http) ...
time diff is 14.435451
ftp.cn.debian.org                        | 775.0 kB/s
=> checking www.anheng.com.cn (http) ...
time diff is 95.854271
www.anheng.com.cn                        | 116.7 kB/s
=> checking mirrors.163.com (http) ...
time diff is 29.687312
mirrors.163.com                          | 376.7 kB/s
=> checking debian.bjtu.edu.cn (http) ...
time diff is 3.926047
debian.bjtu.edu.cn                       | 2.8 MB/s  
=> checking debian.ustc.edu.cn (http) ...
time diff is 13.976661
debian.ustc.edu.cn                       | 800.4 kB/s
=> checking mirrors.sohu.com (http) ...
time diff is 30.373006
mirrors.sohu.com                         | 368.1 kB/s
=> checking mirrors.tuna.tsinghua.edu.cn (http) ...
time diff is 4.545543
mirrors.tuna.tsinghua.edu.cn             | 2.5 MB/s  
mirrors.hust.edu.cn                      | ERR       

Mirror with highest throughput: debian.bjtu.edu.cn



Reply to: