Skip to content

Instantly share code, notes, and snippets.

@MikeiLL
Last active March 12, 2019 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeiLL/979f6d8607373daa726b917e2dd468c0 to your computer and use it in GitHub Desktop.
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.
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