urllib2 timeout

此生再无相见时 提交于 2019-12-06 09:01:34

from my small research, the urllib2.urlopen() function is added in python 2.6

so, the timeout problem should be resolved by sending custom timeout to the urllib2.urlopen function. the code should look like this ;

response = urllib2.urlopen( "---insert url here---", None, your-timeout-value)

the your-timeout-value parameter is an optional parameter which defines the timeout in seconds.

EDIT : According to your comment, I got that you don't need the code for waiting too long then you should have the following code to not get stuck;

import socket
import urllib2

socket.setdefaulttimeout(10)

10 can be changed according to a math formula related to the connection speed & website loading time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!