import os
import mimetools
+
+def _open_with_retry(func, host, *args, **kwargs):
+ # Connecting to remote hosts is flaky. Make it more robust
+ # by retrying the connection several times.
+ for i in range(3):
+ try:
+ return func(host, *args, **kwargs)
+ except IOError, last_exc:
+ continue
+ except:
+ raise
+ raise last_exc
+
+
class URLTimeoutTest(unittest.TestCase):
TIMEOUT = 10.0
socket.setdefaulttimeout(None)
def testURLread(self):
- f = urllib.urlopen("http://www.python.org/")
+ f = _open_with_retry(urllib.urlopen, "http://www.python.org/")
x = f.read()
class urlopenNetworkTests(unittest.TestCase):
"""
+ def urlopen(self, *args):
+ return _open_with_retry(urllib.urlopen, *args)
+
def test_basic(self):
# Simple test expected to pass.
- open_url = urllib.urlopen("http://www.python.org/")
+ open_url = self.urlopen("http://www.python.org/")
for attr in ("read", "readline", "readlines", "fileno", "close",
"info", "geturl"):
self.assert_(hasattr(open_url, attr), "object returned from "
def test_readlines(self):
# Test both readline and readlines.
- open_url = urllib.urlopen("http://www.python.org/")
+ open_url = self.urlopen("http://www.python.org/")
try:
self.assert_(isinstance(open_url.readline(), basestring),
"readline did not return a string")
def test_info(self):
# Test 'info'.
- open_url = urllib.urlopen("http://www.python.org/")
+ open_url = self.urlopen("http://www.python.org/")
try:
info_obj = open_url.info()
finally:
def test_geturl(self):
# Make sure same URL as opened is returned by geturl.
URL = "http://www.python.org/"
- open_url = urllib.urlopen(URL)
+ open_url = self.urlopen(URL)
try:
gotten_url = open_url.geturl()
finally:
# test can't pass on Windows.
return
# Make sure fd returned by fileno is valid.
- open_url = urllib.urlopen("http://www.python.org/")
+ open_url = self.urlopen("http://www.python.org/")
fd = open_url.fileno()
FILE = os.fdopen(fd)
try:
class urlretrieveNetworkTests(unittest.TestCase):
"""Tests urllib.urlretrieve using the network."""
+ def urlretrieve(self, *args):
+ return _open_with_retry(urllib.urlretrieve, *args)
+
def test_basic(self):
# Test basic functionality.
- file_location,info = urllib.urlretrieve("http://www.python.org/")
+ file_location,info = self.urlretrieve("http://www.python.org/")
self.assert_(os.path.exists(file_location), "file location returned by"
" urlretrieve is not a valid path")
FILE = file(file_location)
def test_specified_path(self):
# Make sure that specifying the location of the file to write to works.
- file_location,info = urllib.urlretrieve("http://www.python.org/",
- test_support.TESTFN)
+ file_location,info = self.urlretrieve("http://www.python.org/",
+ test_support.TESTFN)
self.assertEqual(file_location, test_support.TESTFN)
self.assert_(os.path.exists(file_location))
FILE = file(file_location)
def test_header(self):
# Make sure header returned as 2nd value from urlretrieve is good.
- file_location, header = urllib.urlretrieve("http://www.python.org/")
+ file_location, header = self.urlretrieve("http://www.python.org/")
os.unlink(file_location)
self.assert_(isinstance(header, mimetools.Message),
"header is not an instance of mimetools.Message")