[Python-checkins] cpython (2.7): merge from 3.2 - fix urlopen behavior on sites which do not send (or

senthil.kumaran python-checkins at python.org
Wed Jul 27 03:37:37 CEST 2011


http://hg.python.org/cpython/rev/b66bbbdc7abd
changeset:   71523:b66bbbdc7abd
branch:      2.7
parent:      71518:73ae3729b8fe
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Wed Jul 27 09:37:17 2011 +0800
summary:
  merge from 3.2 - fix urlopen behavior on sites which do not send (or obsfuscates) Connection: Close header.

files:
  Lib/test/test_urllib2net.py |   8 ++++++++
  Lib/urllib2.py              |  10 +++++-----
  2 files changed, 13 insertions(+), 5 deletions(-)


diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -186,6 +186,14 @@
             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
--- a/Lib/urllib2.py
+++ b/Lib/urllib2.py
@@ -1166,14 +1166,14 @@
 
         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.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list