Last active
May 3, 2022 02:57
-
-
Save eliezerfot123/5924119 to your computer and use it in GitHub Desktop.
COMPROBAR QUE UNA WEB ESTE DISPONIBLE EN PYTHON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
(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
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/