def test_urllib2(self): self.assertEqual(self.server.request_count, 0) try: urllib2.urlopen('http://127.0.0.1:%s' % port) assert False, 'should not get there' except urllib2.HTTPError, ex: assert ex.code == 501, ` ex `
def get_location(self, accounts): for account in accounts: if not account.server.settings_url or account.presence.disable_location: continue query_string = "action=get_location" url = urlparse.urlunparse(account.server.settings_url[:4] + (query_string,) + account.server.settings_url[5:]) req = urllib2.Request(url) try: data = urllib2.urlopen(req).read() except Exception: continue try: response = cjson.decode(data.replace('\\/', '/')) except TypeError: continue if response and self.location != response: self.location = response self.publish() break
def fetch(cls, url, etag=None, descriptor_etag=None): headers = {'If-None-Match': etag} if etag else {} req = urllib2.Request(url, headers=headers) try: response = urllib2.urlopen(req) content = response.read() info = response.info() except (ConnectionLost, urllib2.URLError, urllib2.HTTPError): return None content_type = info.getheader('content-type') etag = info.getheader('etag') if etag.startswith('W/'): etag = etag[2:] etag = etag.replace('\"', '') if content_type == prescontent.PresenceContentDocument.content_type: try: pres_content = prescontent.PresenceContentDocument.parse(content) data = base64.decodestring(pres_content.data.value) except Exception: return None return cls(data, IconDescriptor(url, descriptor_etag or etag))
def test_trivial(self): # A couple trivial tests self.assertRaises(ValueError, urllib2.urlopen, 'bogus url') # XXX Name hacking to get this to work on Windows. fname = os.path.abspath(urllib2.__file__).replace('\\', '/') if fname[1:2] == ":": fname = fname[2:] # And more hacking to get it to work on MacOS. This assumes # urllib.pathname2url works, unfortunately... if os.name == 'mac': fname = '/' + fname.replace(':', '/') elif os.name == 'riscos': import string fname = os.expand(fname) fname = fname.translate(string.maketrans("/.", "./")) file_url = "file://%s" % fname f = urllib2.urlopen(file_url) buf = f.read() f.close()
def fetch(cls, url, etag=None, descriptor_etag=None): headers = {'If-None-Match': etag} if etag else {} req = urllib2.Request(url, headers=headers) try: response = urllib2.urlopen(req) content = response.read() info = response.info() except (ConnectionLost, urllib2.URLError, urllib2.HTTPError): return None content_type = info.getheader('content-type') etag = info.getheader('etag') if etag.startswith('W/'): etag = etag[2:] etag = etag.replace('\"', '') if content_type == prescontent.PresenceContentDocument.content_type: try: pres_content = prescontent.PresenceContentDocument.parse(content) data = base64.decodestring(pres_content.data.value) except Exception: return None return cls(data, IconDescriptor(url, descriptor_etag or etag)) else: return None