urllib2 timeout

丶灬走出姿态 提交于 2019-12-08 02:45:29

问题


i'm using urllib2 library for my code, i'm using a lot of (urlopen) EDIT: loadurl

i have a problem on my network, when i'm browsing sites, sometimes my browser gets stuck on "Connecting" to a certain website and sometimes my browser returns a timeout

My question is if i use urllib2 on my code it can timeout when trying to connect for too long to a certain website or the code will get stuck on that line.

i know that urllib2 can handle timeouts without specifying it on code but it can apply for this kind of situation ?

Thanks for your time

EDIT :

def checker(self)
 try:
   html = self.loadurl("MY URL HERE")
   if self.ip_ != html:
(...)
 except Exeption, error:
   html = "bad"

回答1:


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.



来源:https://stackoverflow.com/questions/14148341/urllib2-timeout

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