summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_urllib2net.py16
-rw-r--r--Lib/urllib/request.py9
2 files changed, 22 insertions, 3 deletions
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index a475f56..cd225c9 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -174,6 +174,22 @@ 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)
+
+ try:
+ with urllib.request.urlopen('http://www.imdb.com') as res:
+ pass
+ except ValueError as e:
+ self.fail("urlopen failed for sites not sending Connection:close")
+ else:
+ self.assertTrue(res)
+
+ req = urllib.request.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/urllib/request.py b/Lib/urllib/request.py
index 553fe3f..ca643eb 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1143,11 +1143,14 @@ class AbstractHTTPHandler(BaseHandler):
try:
h.request(req.get_method(), req.selector, req.data, headers)
- r = h.getresponse() # an HTTPResponse instance
- except socket.error as err:
+ except socket.error as err: # timeout error
raise URLError(err)
finally:
- h.close()
+ try:
+ r = h.getresponse() # an HTTPResponse instance
+ except Exception as exp:
+ h.close()
+ raise exp
r.url = req.get_full_url()
# This line replaces the .msg attribute of the HTTPResponse