Skip to content

Instantly share code, notes, and snippets.

@eliezerfot123
Last active May 3, 2022 02:57
Show Gist options
  • Save eliezerfot123/5924119 to your computer and use it in GitHub Desktop.
Save eliezerfot123/5924119 to your computer and use it in GitHub Desktop.
COMPROBAR QUE UNA WEB ESTE DISPONIBLE EN PYTHON
rewimport httplib
import urlparse
# funcion que se encarga de obtener respuesta del estatus del servidor web
def get_server_status_code(url):
# descarga sólo el encabezado de una URL y devolver el código de estado del servidor.
host, path = urlparse.urlparse(url)[1:3]
try:
conexion = httplib.HTTPConnection(host)
conexion.request('HEAD', path)
return conexion.getresponse().status
except StandardError:
return None
# función que se encarga de checkear que exista la url a guardar
def check_url(url):
# Comprobar si existe un URL sin necesidad de descargar todo el archivo. Sólo comprobar el encabezado URL.
# variable que se encarga de traer las respuestas
codigo = [httplib.OK, httplib.FOUND, httplib.MOVED_PERMANENTLY]
return get_server_status_code(url) in codigo
print check_url('http://www.google.com')
@geopelia
Copy link

geopelia commented Nov 2, 2013

Recomiendo añadir una línea al inicio donde se indique la codificación para evitar problemas con los acentos. Por ejemplo

# -*- coding: utf-8 -*-

Referencia: http://www.python.org/dev/peps/pep-0263/

@neo-jack-official
Copy link

(rewimport httplib) es (import httplib)
Consulta estoy tratando de hacer una modificacion y no me resulta a ver si me puedes ayudar..
Estoy tratando que la respuesta no sea True o False, si no directamente que diga si esta operando o no
#!/usr/bin/python

-- coding: utf-8 --

import httplib
import urlparse
import logging

def get_server_status_code(url):

host, path = urlparse.urlparse(url)[1:3]
try:
    conexion = httplib.HTTPConnection(host)
    conexion.request('HEAD', path)
    return conexion.getresponse().status
except StandardError:
    return None

def check_url(url):

codigo = [httplib.OK, httplib.FOUND, httplib.MOVED_PERMANENTLY]
return get_server_status_code(url) in codigo

checker = check_url('http://www.google.com')
print check_url('http://www.google.com')
#checker = check_url("http://" + args.host)
if(checker == True):
logging.info("El Servidor esta: ONLINE")
if not(checker == True):
logging.info("El Servidor esta: OFFLINE")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment