Last active
March 12, 2019 17:14
-
-
Save MikeiLL/979f6d8607373daa726b917e2dd468c0 to your computer and use it in GitHub Desktop.
Check if a URL is valid and points to a live site or not.
This file contains 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
def valid_url(url): | |
if (url.lower() == 'none') or (url == ''): | |
return False | |
try: | |
s = requests.Session() | |
a = requests.adapters.HTTPAdapter(max_retries=5) | |
s.mount(url, a) | |
resp = s.head(url) | |
return resp.ok | |
except requests.exceptions.MissingSchema: | |
# If it's missing the schema, run again with schema added | |
return valid_url('http://' + url) | |
except requests.exceptions.ConnectionError: | |
print('Error trying to connect to {}.'.format(url)) | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment