Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/08/2012, 11:02
hGamboa
 
Fecha de Ingreso: agosto-2012
Mensajes: 3
Antigüedad: 11 años, 8 meses
Puntos: 0
Busqueda Problema con python para accesar página https

Hola, buen día.

Desde hace algunos días tengo un problema accesando a https://www.sicop-sep.hacienda.gob.mx/ por medio de python.

Busco obtener el código de retorno para saber si está o no funcionando, sin embargo, arroja un error y no tengo idea del por qué ocurre.

El código tiene una función que sigue los redireccionamientos y funciona bien con otras páginas https (probé https://www.eff.org/https-everywhere/ y https://www.cia.gov/kids-page/index.html), pero, en la que me marca error, no he tenido suerte. De hecho, sólo he podido entrar a la página usando Firefox 3.x.

¿Hay algo equivocado en el código o para ese tipo de páginas debería de usar algo más?

Agradecezco de antemano su tiempo.

Código:
import urllib.request
import http.client
import urllib.parse
from urllib.parse import urlparse 

def resolve_http_redirect(url, depth=0):
    if depth > 10:
        raise Exception("Redirected "+depth+" times, giving up.")
    o = urllib.parse.urlparse(url,allow_fragments=True)

    print(o)

    conn = http.client.HTTPSConnection(o.netloc)
    path = o.path

    print (path)

    if o.query:
        path +='?'+o.query
    conn.request("HEAD", path)
    res = conn.getresponse()
    headers = dict(res.getheaders())
    if 'location' in headers and headers['location'] != url:
        return resolve_http_redirect(headers['location'], depth+1)
    else:
        return url

url_https="https://www.sicop-sep.hacienda.gob.mx/"

host = urlparse(url_https)[1]
print(host)
req = ''.join(urlparse(url_https)[2:]) 
print(req)
conn = http.client.HTTPSConnection(host)
conn.request('HEAD', req) 
resp = conn.getresponse() 
print(resp.status)
print(resp.reason)
print(" ")
print(resp.msg)
answer=resolve_http_redirect(url_https)
print(answer)
Al ejecutar el código arroja lo siguiente:

Cita:

www.sicop-sep.hacienda.gob.mx
/

Traceback (most recent call last):
File "C:\PYTHONppg\prueba_https.py", line 40, in <mod
ule> conn.request('HEAD', req)
File "c:\Python32\lib\http\client.py", line 967, in request
self._send_request(method, url, body, headers)
File "c:\Python32\lib\http\client.py", line 1005, in _send_request
self.endheaders(body)
File "c:\Python32\lib\http\client.py", line 963, in endheaders
self._send_output(message_body)
File "c:\Python32\lib\http\client.py", line 808, in _send_output
self.send(msg)
File "c:\Python32\lib\http\client.py", line 746, in send
self.connect()
File "c:\Python32\lib\http\client.py", line 724, in connect
self.timeout, self.source_address)
File "c:\Python32\lib\socket.py", line 404, in create_connection
raise err
File "c:\Python32\lib\socket.py", line 395, in create_connection
sock.connect(sa)
socket.error: [Errno 10061] No connection could be made because the target machine actively refused