Avoid with exception the interruption of the script due to failure of the connection or internet

0

Greetings I made the following script that brings me the states of the web through a txt file. I do not know if it's the fastest way but it works for me, the issue is that I do not know very well the issue of exceptions with socket. I have the timeout working, but the script continues to be interrupted when there is no internet or the connection to the socket is queued.

import urllib.request
import urllib.error
import time
import os
import ssl
import socket
#import httplib #este es para python2
from termcolor import *
#Para colorear en windows
import colorama
colorama.init()
#Para desastivar el check de SSL
ssl._create_default_https_context = ssl._create_unverified_context

__author__ = "Antonio Orozco R"
__copyright__ = "Copyright (C) 2018 Antonio Orozco R. [email protected]"
__license__ = "GPL/GNU V3"
__version__ = "1.1"

#abrir archivo
archivo = open("url.txt", "r")
#Contar numeros de lineas
iterlen = lambda it: sum(1 for _ in it)
cant = iterlen(open("url.txt"))
#ENCABEZADOS DE COLUMNAS
title_Fecha = "Fecha"
title_Url = "URL"
title_Estado = "Estado"
title_Razon = "Razon"
title_Tiempo = "Tiempo"
print ('------------------------------------------------------------------------------------------------------------------------------------------------------')
print ('------------------------------------------------------------------------------------------------------------------------------------------------------')
cprint("Este script esta bajo licencia: "+__license__+" "+__copyright__+" "+__version__,'white')
print ('------------------------------------------------------------------------------------------------------------------------------------------------------')
cprint("%-25s %-80s %-30s %-7s" %(title_Fecha, title_Url, title_Estado, title_Tiempo),'white')
print ('------------------------------------------------------------------------------------------------------------------------------------------------------')

while True:
    cantT=0
    TIMEOUT=8
    for site in archivo.readlines():
        #Borro los saltos de lineas
        if site[-1] == '\n':
            site = site[:-1]
        try:
            #Variables de conexion y estado
            opener = urllib.request.build_opener()
            opener.addheaders = [('User-agent', 'Mozilla/5.0')]
            conn = opener.open(site, timeout=TIMEOUT)
            html_contents = conn.read()
            st = conn.code
            cantT+= 1
            #se calcula el tiempo transcurido para el check
            inicio = time.time()
            elapse_time = str(time.time()-inicio)[0:6]
            if conn.code in (200, 401, 301, 302):
                cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, st,elapse_time),'green')
            if conn.code in (408, 501, 503):
                cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, st,elapse_time),'yellow')
            #Reload
            if cantT == cant:
                #cierro archivo
                archivo.close()
                #reabro archivo
                archivo = open("url.txt", "r")
                #Contar numeros de lineas
                iterlen = lambda it: sum(1 for _ in it)
                cant = iterlen(open("url.txt"))
                print ('------------------------------------------------------------------------------------------------------------------------------------------------------')
                print ('------------------------------------------------------------------------------------------------------------------------------------------------------')
                cprint("Este script esta bajo licencia: "+__license__+" "+__copyright__+" "+__version__,'white')
                print ('------------------------------------------------------------------------------------------------------------------------------------------------------')
                cprint("%-25s %-80s %-30s %-7s" %(title_Fecha, title_Url, title_Estado, title_Tiempo),'white')
                print ('------------------------------------------------------------------------------------------------------------------------------------------------------')
        #Manejo de excepciones para timeout y otros errores     
        except ssl.SSLError as e:
                cantT+= 1   
                cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, e.reason, elapse_time),'yellow')
        except socket.timeout as e:
                    cantT+= 1
                    cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, "Timeout",TIMEOUT),'red')
        except urllib.error.URLError as e:
                if isinstance(e.reason, socket.timeout):
                    cantT+= 1
                    cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, "Timeout",TIMEOUT),'red')
                else:
                    cantT+= 1
                    cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, e.reason, elapse_time),'yellow')
        except SocketError as e:
                conn.close()
                cantT+= 1
                cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, e.reason , elapse_time),'red')
                #continue
        except ConnectionResetError as e:
                conn.close()
                cantT+= 1
                cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, e.reason , elapse_time),'red')
                #continue
        except urllib.request.HTTPError as e:
                cantT+= 1
                cprint("%-25s %-80s %-30s %-7s" %(time.strftime("%c"),site, e.reason,elapse_time),'red')

    #Cierre de conexion y sleep 
    time.sleep(2)
    #conn.close(

)

    
asked by Antonio Orozco 14.09.2018 в 23:52
source

0 answers