In python, how would I check if a url ending in .jpg exists?
ex: http://www.fakedomain.com/fakeImage.jpg
thanks
In python, how would I check if a url ending in .jpg exists?
ex: http://www.fakedomain.com/fakeImage.jpg
thanks
I think you can try send a http request to the url and read the response.If no exception was caught,it probably exists.
Looks like http://www.fakedomain.com/fakeImage.jpg
automatically redirected to http://www.fakedomain.com/index.html
without any error.
Redirecting for 301 and 302 responses are automatically done without giving any response back to user.
Please take a look HTTPRedirectHandler, you might need to subclass it to handle that.
Here is the one sample from diveintopython.org
import httplib
conn = httplib.HTTPConnection( 'www.fakedomain.com' )
conn.request( 'HEAD', '/fakeImage.jpg' )
r1 = conn.getresponse()
conn.close()
if r1.status == 200:
print 'file exists'
else:
print 'file does not exist'
That works for me, anyway. I should point out that the guts of this program are lifted directly from the Python httplib documentation.
I don't know why you are doing this, but in any case: it should be noted that just because a request to an "image" succeeds, doesn't mean it is what you think it is (it could redirect to anything, or return any data of any type, and potentially cause problems depending on what you do with the response).
Sorry, I went on a binge reading about online exploits and how to defend against them today :P
Try it with mechanize:
import mechanize
br = mechanize.Browser()
br.set_handle_redirect(False)
try:
br.open_novisit('http://www.fakedomain.com/fakeImage.jpg')
print 'OK'
except:
print 'KO'