Skip to content

Instantly share code, notes, and snippets.

@dehowell
Created March 23, 2011 22:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dehowell/884204 to your computer and use it in GitHub Desktop.
Save dehowell/884204 to your computer and use it in GitHub Desktop.
Python function to test if a file at a URL exists.
import urllib2
def file_exists(location):
request = urllib2.Request(location)
request.get_method = lambda : 'HEAD'
try:
response = urllib2.urlopen(request)
return True
except urllib2.HTTPError:
return False
@meryemCH
Copy link

Hello ,
i wanna fetch file in server with authentication,i try this :

values = {"username": "user", "password": "password"} try: r = urllib.request.urlopen(url, values) # response return r.getcode() == 200 except urllib.request.HTTPError: return False

but thasnt work, can u help me please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment