comparison smartopen/handlers.py @ 12:bb995bdf82e2

annoyingly work around user agents again; i hate robot protection sometimes
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 27 Dec 2010 11:48:18 -0800
parents ba9058605c5a
children f11ce7b1a349
comparison
equal deleted inserted replaced
11:ba9058605c5a 12:bb995bdf82e2
20 def test(self, query): 20 def test(self, query):
21 return True 21 return True
22 22
23 def exists(self, URL): 23 def exists(self, URL):
24 """does a URL exist?""" 24 """does a URL exist?"""
25 # need a phony user agent so wikipedia won't know we're a bot
26 headers = {}
27 headers['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4'
28
29 request = urllib2.Request(URL, None, headers)
25 try: 30 try:
26 urllib.urlopen(URL) 31 f = urllib2.urlopen(request).read()
27 return True 32 return True
28 except IOError: 33 except urllib2.HTTPError, e:
34 return False
35 except urllib2.URLError, e:
29 return False 36 return False
30 37
31 class URL(Location): 38 class URL(Location):
32 """a straight URL""" 39 """a straight URL"""
33 40