Skip to content

Instantly share code, notes, and snippets.

@zealinux
Forked from fedir/checkUrlExists.m1.py
Created December 18, 2018 06:51
Show Gist options
  • Select an option

  • Save zealinux/643e6f36f07ddef1395da4fccf64b8f8 to your computer and use it in GitHub Desktop.

Select an option

Save zealinux/643e6f36f07ddef1395da4fccf64b8f8 to your computer and use it in GitHub Desktop.
Check if URL exists via Python
from urllib2 import urlopen
code = urlopen("http://example.com/").code
if (code / 100 >= 4):
print "Nothing there."
# via http://stackoverflow.com/questions/1966086/how-can-i-determine-if-anything-at-the-given-url-does-exist
import urllib2
ret = urllib2.urlopen('http://hostname/directory/file.jpg')
if ret.code == 200:
print "Exists!"
# via http://stackoverflow.com/questions/7347888/how-do-i-check-if-a-file-on-http-exists-using-python-django
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment