diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py index 5ffba87fdfc..1375cdada00 100644 --- a/Lib/test/test_urllib2net.py +++ b/Lib/test/test_urllib2net.py @@ -186,6 +186,14 @@ class OtherNetworkTests(unittest.TestCase): opener.open(request) self.assertEqual(request.get_header('User-agent'),'Test-Agent') + def test_sites_no_connection_close(self): + # Some sites do not send Connection: close header. + # Verify that those work properly. (#issue12576) + + req = urllib2.urlopen('http://www.imdb.com') + res = req.read() + self.assertTrue(res) + def _test_urls(self, urls, handlers, retry=True): import time import logging diff --git a/Lib/urllib2.py b/Lib/urllib2.py index 2bce745e637..a0065b5b84d 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -1166,14 +1166,14 @@ class AbstractHTTPHandler(BaseHandler): try: h.request(req.get_method(), req.get_selector(), req.data, headers) + except socket.error, err: # XXX what error? + h.close() + raise URLError(err) + else: try: r = h.getresponse(buffering=True) - except TypeError: #buffering kw not supported + except TypeError: # buffering kw not supported r = h.getresponse() - except socket.error, err: # XXX what error? - raise URLError(err) - finally: - h.close() # Pick apart the HTTPResponse object to get the addinfourl # object initialized properly.