def urlretrieve(self, *args, **kwargs): resource = args[0] with support.transient_internet(resource): file_location, info = urllib_request.urlretrieve(*args, **kwargs) try: yield file_location, info finally: support.unlink(file_location)
def urlopen(self, *args, **kwargs): resource = args[0] with support.transient_internet(resource): r = urllib_request.urlopen(*args, **kwargs) try: yield r finally: r.close()
def test_getcode(self): # test getcode() with the fancy opener to get 404 error codes URL = "http://www.python.org/XXXinvalidXXX" with support.transient_internet(URL): open_url = urllib_request.FancyURLopener().open(URL) try: code = open_url.getcode() finally: open_url.close() self.assertEqual(code, 404)
def testURLread(self): with support.transient_internet("www.python.org"): f = urllib_request.urlopen("http://www.python.org/") x = f.read()