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

Skip to content

Commit 7496fef

Browse files
committed
merge from 3.2 - Fix closes Issue12576 - fix urlopen behavior on sites which do not send (or obsfuscates) Connection: Close header.
2 parents d3b2aef + 1299a8f commit 7496fef

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
@@ -1143,11 +1143,14 @@ def do_open(self, http_class, req, **http_conn_args):
11431143

11441144
try:
11451145
h.request(req.get_method(), req.selector, req.data, headers)
1146-
r = h.getresponse() # an HTTPResponse instance
1147-
except socket.error as err:
1146+
except socket.error as err: # timeout error
11481147
raise URLError(err)
11491148
finally:
1150-
h.close()
1149+
try:
1150+
r = h.getresponse() # an HTTPResponse instance
1151+
except Exception as exp:
1152+
h.close()
1153+
raise exp
11511154

11521155
r.url = req.get_full_url()
11531156
# 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
@@ -237,6 +237,9 @@ Core and Builtins
237237
Library
238238
-------
239239

240+
- Issue #12576: Fix urlopen behavior on sites which do not send (or obfuscates)
241+
Connection:close header.
242+
240243
- Issue #12102: Document that buffered files must be flushed before being used
241244
with mmap. Patch by Steffen Daode Nurpmeso.
242245

0 commit comments

Comments
 (0)