exception http.client in Python

0

I'm trying to handle exceptions I have the following code:

conn = http.client.HTTPSConnection (site, timeout = 5)

When the waiting time exceeds What exception should I use?

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')

I've never handled exceptions and I do not want to stop when a website does not respond within the timeout.

the code is as follows

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')

and in the documentation it is not that I say much. link

    
asked by elloco careloco 08.06.2018 в 23:36
source

2 answers

0

it was required to use the following exception as urllib. except (http.client.HTTPException, socket.timeout, socket.error):

    
answered by 09.06.2018 / 00:27
source
1

The exception that is returned when the timeout is exceeded comes from socket : socket.timeout

import  http.client
import socket


try:
    conn = http.client.HTTPSConnection(site, timeout=5)
    conn.request("GET", "/")
    # ...
except socket.timeout:
    # Manejo de la excepción
    print("Tiempo excedido")
    
answered by 09.06.2018 в 00:28