blob: 4750ad9600cc3ef430e54b867b28eb63ac86ea1b [file] [log] [blame]
Victor Stinner311110a2020-06-11 18:26:23 +02001import errno
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00002import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Hai Shi79bb2c92020-08-06 19:51:29 +08004from test.support import os_helper
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +03005from test.support import socket_helper
Thomas Wouters477c8d52006-05-27 19:21:47 +00006from test.test_urllib2 import sanepathname2url
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00007
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00008import os
Jeremy Hylton1afc1692008-06-18 20:49:58 +00009import socket
Jeremy Hylton1afc1692008-06-18 20:49:58 +000010import urllib.error
11import urllib.request
Senthil Kumaranb8f7ea62010-04-20 10:35:49 +000012import sys
Berker Peksagb77983d2014-10-10 14:34:16 +030013
doko@ubuntu.come5751482013-12-26 17:37:11 +010014support.requires("network")
15
Christian Heimes969fe572008-01-25 11:23:10 +000016
Georg Brandlc28e1fa2008-06-10 19:20:26 +000017def _retry_thrice(func, exc, *args, **kwargs):
Christian Heimes969fe572008-01-25 11:23:10 +000018 for i in range(3):
19 try:
Georg Brandlc28e1fa2008-06-10 19:20:26 +000020 return func(*args, **kwargs)
21 except exc as e:
Neal Norwitz2f142582008-01-26 19:49:41 +000022 last_exc = e
Christian Heimes969fe572008-01-25 11:23:10 +000023 continue
Christian Heimes969fe572008-01-25 11:23:10 +000024 raise last_exc
25
Georg Brandlc28e1fa2008-06-10 19:20:26 +000026def _wrap_with_retry_thrice(func, exc):
27 def wrapped(*args, **kwargs):
28 return _retry_thrice(func, exc, *args, **kwargs)
29 return wrapped
30
Victor Stinnerc11b3b12018-12-05 01:58:31 +010031# bpo-35411: FTP tests of test_urllib2net randomly fail
32# with "425 Security: Bad IP connecting" on Travis CI
33skip_ftp_test_on_travis = unittest.skipIf('TRAVIS' in os.environ,
34 'bpo-35411: skip FTP test '
35 'on Travis CI')
36
37
Georg Brandlc28e1fa2008-06-10 19:20:26 +000038# Connecting to remote hosts is flaky. Make it more robust by retrying
39# the connection several times.
Jeremy Hylton1afc1692008-06-18 20:49:58 +000040_urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen,
41 urllib.error.URLError)
Christian Heimes969fe572008-01-25 11:23:10 +000042
Thomas Wouters477c8d52006-05-27 19:21:47 +000043
Victor Stinner311110a2020-06-11 18:26:23 +020044class TransientResource(object):
45
46 """Raise ResourceDenied if an exception is raised while the context manager
47 is in effect that matches the specified exception and attributes."""
48
49 def __init__(self, exc, **kwargs):
50 self.exc = exc
51 self.attrs = kwargs
52
53 def __enter__(self):
54 return self
55
56 def __exit__(self, type_=None, value=None, traceback=None):
57 """If type_ is a subclass of self.exc and value has attributes matching
58 self.attrs, raise ResourceDenied. Otherwise let the exception
59 propagate (if any)."""
60 if type_ is not None and issubclass(self.exc, type_):
61 for attr, attr_value in self.attrs.items():
62 if not hasattr(value, attr):
63 break
64 if getattr(value, attr) != attr_value:
65 break
66 else:
67 raise ResourceDenied("an optional resource is not available")
68
69# Context managers that raise ResourceDenied when various issues
70# with the Internet connection manifest themselves as exceptions.
71# XXX deprecate these and use transient_internet() instead
72time_out = TransientResource(OSError, errno=errno.ETIMEDOUT)
73socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
74ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
75
76
Thomas Wouters477c8d52006-05-27 19:21:47 +000077class AuthTests(unittest.TestCase):
78 """Tests urllib2 authentication features."""
79
80## Disabled at the moment since there is no page under python.org which
81## could be used to HTTP authentication.
82#
83# def test_basic_auth(self):
Georg Brandl24420152008-05-26 16:32:26 +000084# import http.client
Thomas Wouters477c8d52006-05-27 19:21:47 +000085#
86# test_url = "http://www.python.org/test/test_urllib2/basic_auth"
87# test_hostport = "www.python.org"
88# test_realm = 'Test Realm'
89# test_user = 'test.test_urllib2net'
90# test_password = 'blah'
91#
92# # failure
93# try:
Christian Heimes969fe572008-01-25 11:23:10 +000094# _urlopen_with_retry(test_url)
Thomas Wouters477c8d52006-05-27 19:21:47 +000095# except urllib2.HTTPError, exc:
96# self.assertEqual(exc.code, 401)
97# else:
98# self.fail("urlopen() should have failed with 401")
99#
100# # success
101# auth_handler = urllib2.HTTPBasicAuthHandler()
102# auth_handler.add_password(test_realm, test_hostport,
103# test_user, test_password)
104# opener = urllib2.build_opener(auth_handler)
105# f = opener.open('http://localhost/')
Christian Heimes969fe572008-01-25 11:23:10 +0000106# response = _urlopen_with_retry("http://www.python.org/")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000107#
108# # The 'userinfo' URL component is deprecated by RFC 3986 for security
109# # reasons, let's not implement it! (it's already implemented for proxy
110# # specification strings (that is, URLs or authorities specifying a
111# # proxy), so we must keep that)
Georg Brandl24420152008-05-26 16:32:26 +0000112# self.assertRaises(http.client.InvalidURL,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000113# urllib2.urlopen, "http://evil:thing@example.com")
114
115
Thomas Woutersb2137042007-02-01 18:02:27 +0000116class CloseSocketTest(unittest.TestCase):
117
118 def test_close(self):
Victor Stinner7cb92042019-07-02 14:50:19 +0200119 # clear _opener global variable
120 self.addCleanup(urllib.request.urlcleanup)
121
Thomas Woutersb2137042007-02-01 18:02:27 +0000122 # calling .close() on urllib2's response objects should close the
123 # underlying socket
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100124 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300125 with socket_helper.transient_internet(url):
Nadeem Vawda61baebd2012-01-25 08:02:05 +0200126 response = _urlopen_with_retry(url)
127 sock = response.fp
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200128 self.assertFalse(sock.closed)
Nadeem Vawda61baebd2012-01-25 08:02:05 +0200129 response.close()
130 self.assertTrue(sock.closed)
Thomas Woutersb2137042007-02-01 18:02:27 +0000131
Thomas Wouters477c8d52006-05-27 19:21:47 +0000132class OtherNetworkTests(unittest.TestCase):
133 def setUp(self):
134 if 0: # for debugging
135 import logging
136 logger = logging.getLogger("test_urllib2net")
137 logger.addHandler(logging.StreamHandler())
138
Thomas Wouters477c8d52006-05-27 19:21:47 +0000139 # XXX The rest of these tests aren't very good -- they don't check much.
140 # They do sometimes catch some major disasters, though.
141
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100142 @skip_ftp_test_on_travis
Thomas Wouters477c8d52006-05-27 19:21:47 +0000143 def test_ftp(self):
144 urls = [
Ammar Askard81bea62017-07-18 19:27:24 -0700145 'ftp://www.pythontest.net/README',
146 ('ftp://www.pythontest.net/non-existent-file',
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200147 None, urllib.error.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000148 ]
149 self._test_urls(urls, self._extra_handlers())
150
Thomas Wouters477c8d52006-05-27 19:21:47 +0000151 def test_file(self):
Hai Shi79bb2c92020-08-06 19:51:29 +0800152 TESTFN = os_helper.TESTFN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000153 f = open(TESTFN, 'w')
154 try:
155 f.write('hi there\n')
156 f.close()
157 urls = [
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000158 'file:' + sanepathname2url(os.path.abspath(TESTFN)),
159 ('file:///nonsensename/etc/passwd', None,
160 urllib.error.URLError),
Thomas Wouters477c8d52006-05-27 19:21:47 +0000161 ]
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000162 self._test_urls(urls, self._extra_handlers(), retry=True)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000163 finally:
164 os.remove(TESTFN)
165
Senthil Kumaran3800ea92012-01-21 11:52:48 +0800166 self.assertRaises(ValueError, urllib.request.urlopen,'./relative_path/to/file')
167
Thomas Wouters477c8d52006-05-27 19:21:47 +0000168 # XXX Following test depends on machine configurations that are internal
169 # to CNRI. Need to set up a public server with the right authentication
170 # configuration for test purposes.
171
172## def test_cnri(self):
173## if socket.gethostname() == 'bitdiddle':
174## localhost = 'bitdiddle.cnri.reston.va.us'
175## elif socket.gethostname() == 'bitdiddle.concentric.net':
176## localhost = 'localhost'
177## else:
178## localhost = None
179## if localhost is not None:
180## urls = [
181## 'file://%s/etc/passwd' % localhost,
182## 'http://%s/simple/' % localhost,
183## 'http://%s/digest/' % localhost,
184## 'http://%s/not/found.h' % localhost,
185## ]
186
187## bauth = HTTPBasicAuthHandler()
188## bauth.add_password('basic_test_realm', localhost, 'jhylton',
189## 'password')
190## dauth = HTTPDigestAuthHandler()
191## dauth.add_password('digest_test_realm', localhost, 'jhylton',
192## 'password')
193
194## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
195
Senthil Kumarand95cc752010-08-08 11:27:53 +0000196 def test_urlwithfrag(self):
Benjamin Peterson258f3f02014-11-05 11:27:14 -0500197 urlwith_frag = "http://www.pythontest.net/index.html#frag"
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300198 with socket_helper.transient_internet(urlwith_frag):
Georg Brandl5be365f2010-10-28 14:55:02 +0000199 req = urllib.request.Request(urlwith_frag)
200 res = urllib.request.urlopen(req)
201 self.assertEqual(res.geturl(),
Benjamin Peterson258f3f02014-11-05 11:27:14 -0500202 "http://www.pythontest.net/index.html#frag")
Senthil Kumarand95cc752010-08-08 11:27:53 +0000203
Senthil Kumaran83070752013-05-24 09:14:12 -0700204 def test_redirect_url_withfrag(self):
Benjamin Petersonb811a972014-11-05 13:10:08 -0500205 redirect_url_with_frag = "http://www.pythontest.net/redir/with_frag/"
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300206 with socket_helper.transient_internet(redirect_url_with_frag):
Senthil Kumaran83070752013-05-24 09:14:12 -0700207 req = urllib.request.Request(redirect_url_with_frag)
208 res = urllib.request.urlopen(req)
209 self.assertEqual(res.geturl(),
Benjamin Petersonb811a972014-11-05 13:10:08 -0500210 "http://www.pythontest.net/elsewhere/#frag")
Senthil Kumaran83070752013-05-24 09:14:12 -0700211
Senthil Kumaran42ef4b12010-09-27 01:26:03 +0000212 def test_custom_headers(self):
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100213 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300214 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000215 opener = urllib.request.build_opener()
216 request = urllib.request.Request(url)
217 self.assertFalse(request.header_items())
218 opener.open(request)
219 self.assertTrue(request.header_items())
220 self.assertTrue(request.has_header('User-agent'))
221 request.add_header('User-Agent','Test-Agent')
222 opener.open(request)
223 self.assertEqual(request.get_header('User-agent'),'Test-Agent')
Senthil Kumaran42ef4b12010-09-27 01:26:03 +0000224
INADA Naoki36d56ea2018-04-18 00:31:29 +0900225 @unittest.skip('XXX: http://www.imdb.com is gone')
Senthil Kumaran1299a8f2011-07-27 08:05:58 +0800226 def test_sites_no_connection_close(self):
227 # Some sites do not send Connection: close header.
228 # Verify that those work properly. (#issue12576)
229
Senthil Kumarane324c572011-07-31 11:45:14 +0800230 URL = 'http://www.imdb.com' # mangles Connection:close
Senthil Kumaran1299a8f2011-07-27 08:05:58 +0800231
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300232 with socket_helper.transient_internet(URL):
Senthil Kumarane324c572011-07-31 11:45:14 +0800233 try:
234 with urllib.request.urlopen(URL) as res:
235 pass
Pablo Galindo293dd232019-11-19 21:34:03 +0000236 except ValueError:
Senthil Kumarane324c572011-07-31 11:45:14 +0800237 self.fail("urlopen failed for site not sending \
238 Connection:close")
239 else:
240 self.assertTrue(res)
241
242 req = urllib.request.urlopen(URL)
243 res = req.read()
244 self.assertTrue(res)
Senthil Kumaran1299a8f2011-07-27 08:05:58 +0800245
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000246 def _test_urls(self, urls, handlers, retry=True):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000247 import time
248 import logging
249 debug = logging.getLogger("test_urllib2").debug
250
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000251 urlopen = urllib.request.build_opener(*handlers).open
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000252 if retry:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000253 urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000254
255 for url in urls:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200256 with self.subTest(url=url):
257 if isinstance(url, tuple):
258 url, req, expected_err = url
Georg Brandl5be365f2010-10-28 14:55:02 +0000259 else:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200260 req = expected_err = None
261
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300262 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000263 try:
Victor Stinner1d0f9b32019-12-10 22:09:23 +0100264 f = urlopen(url, req, support.INTERNET_TIMEOUT)
Berker Peksag8b63d3a2014-10-25 05:42:30 +0300265 # urllib.error.URLError is a subclass of OSError
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200266 except OSError as err:
267 if expected_err:
268 msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
269 (expected_err, url, req, type(err), err))
270 self.assertIsInstance(err, expected_err, msg)
271 else:
272 raise
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200273 else:
274 try:
Victor Stinner311110a2020-06-11 18:26:23 +0200275 with time_out, \
276 socket_peer_reset, \
277 ioerror_peer_reset:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200278 buf = f.read()
279 debug("read %d bytes" % len(buf))
Christian Heimes03c8ddd2020-11-20 09:26:07 +0100280 except TimeoutError:
Antoine Pitroubc2c4c92014-09-17 00:39:21 +0200281 print("<timeout: %s>" % url, file=sys.stderr)
282 f.close()
283 time.sleep(0.1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000284
285 def _extra_handlers(self):
286 handlers = []
287
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000288 cfh = urllib.request.CacheFTPHandler()
Nadeem Vawda08f5f7a2011-07-23 14:03:00 +0200289 self.addCleanup(cfh.clear_cache)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000290 cfh.setTimeout(1)
291 handlers.append(cfh)
292
293 return handlers
294
Christian Heimesbbe741d2008-03-28 10:53:29 +0000295
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000296class TimeoutTest(unittest.TestCase):
Victor Stinner7cb92042019-07-02 14:50:19 +0200297 def setUp(self):
298 # clear _opener global variable
299 self.addCleanup(urllib.request.urlcleanup)
300
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000301 def test_http_basic(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200302 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100303 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300304 with socket_helper.transient_internet(url, timeout=None):
Georg Brandl5be365f2010-10-28 14:55:02 +0000305 u = _urlopen_with_retry(url)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200306 self.addCleanup(u.close)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200307 self.assertIsNone(u.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000308
Georg Brandlf78e02b2008-06-10 17:40:04 +0000309 def test_http_default_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200310 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100311 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300312 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000313 socket.setdefaulttimeout(60)
314 try:
315 u = _urlopen_with_retry(url)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200316 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000317 finally:
318 socket.setdefaulttimeout(None)
319 self.assertEqual(u.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000320
321 def test_http_no_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200322 self.assertIsNone(socket.getdefaulttimeout())
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100323 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300324 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000325 socket.setdefaulttimeout(60)
326 try:
327 u = _urlopen_with_retry(url, timeout=None)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200328 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000329 finally:
330 socket.setdefaulttimeout(None)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200331 self.assertIsNone(u.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000332
Georg Brandlf78e02b2008-06-10 17:40:04 +0000333 def test_http_timeout(self):
Stéphane Wirtela40681d2019-02-22 14:45:36 +0100334 url = support.TEST_HTTP_URL
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300335 with socket_helper.transient_internet(url):
Georg Brandl5be365f2010-10-28 14:55:02 +0000336 u = _urlopen_with_retry(url, timeout=120)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200337 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000338 self.assertEqual(u.fp.raw._sock.gettimeout(), 120)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000339
Ammar Askard81bea62017-07-18 19:27:24 -0700340 FTP_HOST = 'ftp://www.pythontest.net/'
Christian Heimes969fe572008-01-25 11:23:10 +0000341
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100342 @skip_ftp_test_on_travis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000343 def test_ftp_basic(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200344 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300345 with socket_helper.transient_internet(self.FTP_HOST, timeout=None):
Georg Brandl5be365f2010-10-28 14:55:02 +0000346 u = _urlopen_with_retry(self.FTP_HOST)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200347 self.addCleanup(u.close)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200348 self.assertIsNone(u.fp.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000349
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100350 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000351 def test_ftp_default_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200352 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300353 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000354 socket.setdefaulttimeout(60)
355 try:
356 u = _urlopen_with_retry(self.FTP_HOST)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200357 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000358 finally:
359 socket.setdefaulttimeout(None)
360 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000361
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100362 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000363 def test_ftp_no_timeout(self):
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200364 self.assertIsNone(socket.getdefaulttimeout())
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300365 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000366 socket.setdefaulttimeout(60)
367 try:
368 u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200369 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000370 finally:
371 socket.setdefaulttimeout(None)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200372 self.assertIsNone(u.fp.fp.raw._sock.gettimeout())
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000373
Victor Stinnerc11b3b12018-12-05 01:58:31 +0100374 @skip_ftp_test_on_travis
Georg Brandlf78e02b2008-06-10 17:40:04 +0000375 def test_ftp_timeout(self):
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300376 with socket_helper.transient_internet(self.FTP_HOST):
Georg Brandl5be365f2010-10-28 14:55:02 +0000377 u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
Victor Stinnereaca5c82011-06-17 14:53:02 +0200378 self.addCleanup(u.close)
Georg Brandl5be365f2010-10-28 14:55:02 +0000379 self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000380
Thomas Wouters477c8d52006-05-27 19:21:47 +0000381
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000382if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400383 unittest.main()