Last active
September 4, 2015 21:20
-
-
Save skgsergio/de5d758829b6f30f15b2 to your computer and use it in GitHub Desktop.
Comprobación de si una web carga de forma rapida y sencilla.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
try: | |
import urllib2 # Python 2 | |
except ImportError: | |
import urllib.request as urllib2 # Python 3 | |
def isup(url): | |
try: | |
if not url.startswith(("http://", "https://")): | |
url = "http://" + url | |
rq = urllib2.Request( | |
url, | |
headers={ | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0' | |
} | |
) | |
rq.get_method = lambda: 'HEAD' | |
urllib2.urlopen(rq) | |
return "Carga sin problemas." | |
except urllib2.HTTPError as e: | |
return "Carga pero da el siguiente error: %s" % (e) | |
except urllib2.URLError: | |
return "No carga." | |
urls = ( | |
'http://httpbin.org/get', | |
'http://httpbin.org/status/404', | |
'http://httpbin.org/status/500', | |
'http://httpbin.ogr/', | |
'httpbin.org/get', | |
'httpbin.org', | |
'https://httpbin.org/get' | |
) | |
for u in urls: | |
print("Probando %s... %s" % (u, isup(u))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment