timeout for urllib2.urlopen() in pre Python 2.6 versions

前端 未结 6 539
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 03:34

The urllib2 documentation says that timeout parameter was added in Python 2.6. Unfortunately my code base has been running on Python 2.5 and 2.4 platforms.

6条回答
  •  猫巷女王i
    2020-12-29 03:47

    You must set timeout in two places.

    import urllib2
    import socket
    
    socket.setdefaulttimeout(30)
    f = urllib2.urlopen('http://www.python.org/', timeout=30)
    

提交回复
热议问题