urllib hangs

Benjamin Niemann pink at odahoda.de
Tue Aug 24 23:38:05 CEST 2004

I don't know how it exactly works myself. But for dynamic languages like 
python it is part of the design that things can be changed on-the-fly:

###### foo.py
# assume this module is part of the standard lib
def A():
     print "A called"

def B():
     A()   # this could also be in another module
           # the effect would be the same

###### bar.py
# this is an addon that modifies foo
import foo

origA = foo.A
def newA():
     print "before A"
     print "after A"
foo.A = newA

###### test.py
import foo
import bar


It doesn't matter where the files are placed - once modules are found in 
a directory listed in os.path they are all 'equal'.
This is a powerful feature (if you know how to use it correctly) and one 
thing that make dynamic languages dynamic. Once a module is imported, 
all imports result in a reference to the same module - if that one is 
modified, the modification will be visible everywhere where the module 
is imported (but this is not true for 'from foo import A', because this 
is a reference to the original function that cannot be changed by anyone 
once you get a hold of it).
(My first try was to overwrite time.time(), hoping that time.ctime() 
would show a modified time. This didn't work, probably because both 
time() and ctime() make a call to a lower level function. A good example 
for not tampering with other peoples code unless you know how it works...)

Ok, enough to explain, why you don't have to modify any file of the 
standard library in order to change its the behaviour.
The author of timeoutsocket was in fact not clear about the location 
where the actual file has to be placed, probably because he assumed that 
everyone knows that it doesn't matter.

Jay Donnell wrote:
>>have you actually looked at the comments in that
>>module yet?  It does say exactly what you need to do...
> I don't understand the inner workings of the socket or timeoutsocket
> modules. On it's face it doesn't make sense that importing
> timeoutsocket would magically override the behaviour of socket without
> me doing anything else to the socket module. This appears to be what
> happens, but it certainly isn't clear to someone that doesn't know how
> it works. Here is what timeoutsocket says, "After this module
> has been imported, all socket creation goes through this shim. ".
> After reading this I was unsure if I needed to install
> timeoutsocket.py into the base python distro because, again, it seems
> odd that simply dropping timeoutsocket.py into my cwd and importing it
> will override the behaviour of the socket module. This didn't say
> "exactly what I need to do". It assumed a few things that seemed odd
> to me. How does timeoutsocket.py " insert a shim into the socket
> module."? What does that mean??? It wasn't clear! In the time it took
> you and the other guy to criticize me you could have simply said,
> "yeah, just drop it into your cwd and import it".
> P.S. - I really do appreciate the help that Benjamin gave.
> Here is what timeoutsocket says.
> --------------------------------------------------------------------------
> "This module enables a timeout mechanism on all TCP connections.   
> After this module
> has been imported, all socket creation goes through this shim.  As a
> result, every TCP connection will support a timeout.
> The beauty of this method is that it immediately and transparently
> enables the entire python library to support timeouts on TCP sockets.
> As an example, if you wanted to SMTP connections to have a 20 second
> timeout:
>     import timeoutsocket
>     import smtplib
>     timeoutsocket.setDefaultSocketTimeout(20)
> The timeout applies to the socket functions that normally block on
> execution:  read, write, connect, and accept.  If any of these
> operations exceeds the specified timeout, the exception Timeout
> will be raised.
> ---------------------------------------------------------------------

More information about the Python-list mailing list