urllib.urlretrieve never returns???

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Mar 20 00:26:20 CET 2012

On Mon, 19 Mar 2012 20:32:03 +0100, Laszlo Nagy wrote:

> The pythonw.exe may not have the rights to access network resources.
>>> Have you set a default timeout for sockets?
>>> import socket
>>> socket.setdefaulttimeout(10) # 10 seconds
> I have added pythonw.exe to allowed exceptions. Disabled firewall
> completely. Set socket timeout to 10 seconds. Still nothing.
> urllib.urlretrieve does not return from call....
> any other ideas?

I'm sorry if I have missed something, or making suggestions you have 
already tried, but your original post describing the problem is missing 
from my news server.

I gather you are running urlretrieve in a separate thread, inside a GUI?

I have learned that whenever I have inexplicable behaviour in a function, 
I should check my assumptions. In this case, (1) are you sure you have 
the right urlretrieve, and (2) are you sure that your self.Log() method 
is working correctly? Just before the problematic call, do this:

# was:
fpath = urllib.urlretrieve(imgurl)[0]

# becomes:
print(urllib.__file__, urlretrieve)
self.Log(urllib.__file__, urlretrieve)
fpath = urllib.urlretrieve(imgurl)[0]

and ensure that you haven't accidentally shadowed them with something 
unexpected. Does the output printed to the console match the output 

What happens if you take the call to urlretrieve out of the thread and 
call it by hand? Run urllib.urlretrieve(imgurl) directly in the 
interactive interpreter. Does it still hang forever?

When you say it "never" returns, do you mean *never* or do you mean "I 
gave up waiting after five minutes"? What happens if you leave it to run 
all day?

Perhaps it returns after e.g. seven hours, which would be a mystery in 
itself, but at least you have perturbed the problem and have another data 
point. Maybe it isn't dead, just really slow. How big are the files you 
are trying to retrieve? Try retrieving a really small file. Then try 
retrieving a non-existent file.

What happens if you call urlretrieve with a reporthook argument? Does it 
print anything?

What happens if you try to browse to imgurl in your web browser? Are you 
sure the problem is with urlretrieve and not the source?


More information about the Python-list mailing list