例外Python HTTPSConnection

时间:2018-06-08 18:25:35

标签: python exception

我试图为此做例外 conn = http.client.HTTPSConnection(site,timeout = 5) 当它超过超时。我应该使用什么例外?

try:
            conn = http.client.HTTPSConnection(site, timeout=5)
            conn.request("GET", "/")
            except http.client.NotConnected as e:
            cprint("%-30s %-50s %-10s %-30s %-20s" %(time.strftime("%c"),site, st,rs,elapse_time),'red')

我从未处理过例外情况,我要求在网站发布时不要停止。

代码如下

while 1:
    cantT=0
    for site in SITES:
            #prueba
            #conn = urllib.request.urlopen(site)
            #conn = httplib.HTTPConnection(site, timeout=10)
            #Variables de conexion y estado
        try:
            conn = http.client.HTTPSConnection(site, timeout=5)
            conn.request("GET", "/")
            response = conn.getresponse()
            rs = response.reason
            st = str (response.status)
            cantT+= 1
            #se calcula el tiempo transcurido para el check
            inicio = time.time()
            elapse_time = str(time.time()-inicio)
            if response.status == 200:
                cprint("%-30s %-50s %-10s %-30s %-20s" %(time.strftime("%c"),site, st,rs,elapse_time),'green')

1 个答案:

答案 0 :(得分:0)

需要使用以下异常作为urllib。

except (http.client.HTTPException, socket.timeout, socket.error):