Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1299a8f

Browse files
committed
Fix closes Issue12576 - fix urlopen behavior on sites which do not send (or obsfuscates) Connection: Close header.
1 parent 5cd3e30 commit 1299a8f

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

Lib/test/test_urllib2net.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,22 @@ def test_custom_headers(self):
174174
opener.open(request)
175175
self.assertEqual(request.get_header('User-agent'),'Test-Agent')
176176

177+
def test_sites_no_connection_close(self):
178+
# Some sites do not send Connection: close header.
179+
# Verify that those work properly. (#issue12576)
180+
181+
try:
182+
with urllib.request.urlopen('http://www.imdb.com') as res:
183+
pass
184+
except ValueError as e:
185+
self.fail("urlopen failed for sites not sending Connection:close")
186+
else:
187+
self.assertTrue(res)
188+
189+
req = urllib.request.urlopen('http://www.imdb.com')
190+
res = req.read()
191+
self.assertTrue(res)
192+
177193
def _test_urls(self, urls, handlers, retry=True):
178194
import time
179195
import logging

Lib/urllib/request.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,11 +1134,14 @@ def do_open(self, http_class, req, **http_conn_args):
11341134

11351135
try:
11361136
h.request(req.get_method(), req.selector, req.data, headers)
1137-
r = h.getresponse() # an HTTPResponse instance
1138-
except socket.error as err:
1137+
except socket.error as err: # timeout error
11391138
raise URLError(err)
11401139
finally:
1141-
h.close()
1140+
try:
1141+
r = h.getresponse() # an HTTPResponse instance
1142+
except Exception as exp:
1143+
h.close()
1144+
raise exp
11421145

11431146
r.url = req.get_full_url()
11441147
# This line replaces the .msg attribute of the HTTPResponse

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Core and Builtins
3737
Library
3838
-------
3939

40+
- Issue #12576: Fix urlopen behavior on sites which do not send (or obfuscates)
41+
Connection:close header.
42+
4043
- Issue #12102: Document that buffered files must be flushed before being used
4144
with mmap. Patch by Steffen Daode Nurpmeso.
4245

0 commit comments

Comments
 (0)