Can urllib be used with nonblocking sockets?

Skip Montanaro skip at pobox.com
Wed Apr 30 10:00:18 EDT 2003


    Arne> I have written a little python program that uses non-blocking
    Arne> sockets with select and I now want to urrlib in my program and I
    Arne> cannot find a method in urllib that would allow me to tell urllib
    Arne> that it should use non blocking sockets.

In 2.3 you can set the default timeout for sockets before opening the URL:

    dto = socket.getdefaulttimeout()
    socket.setdefaulttimeout(mytimeout)
    try:
        try:
            f = urllib2.urlopen("http://www.python.org/")
            x = f.read()
        except socket.error, msg:
            print "timeout"
    finally:
        # restore default
        socket.setdefaulttimeout(dto)

Skip





More information about the Python-list mailing list