This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: urllib.request.urlopen gives empty response bodies for some sites
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.1, Python 3.2
process
Status: closed Resolution: duplicate
Dependencies: Superseder: urlib.request fails to open some sites
View: 12576
Assigned To: Nosy List: ezio.melotti, royliu
Priority: normal Keywords:

Created on 2011-07-24 05:12 by royliu, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py royliu, 2011-07-24 05:12 A small test script.
Messages (2)
msg141039 - (view) Author: Roy Liu (royliu) Date: 2011-07-24 05:12
When testing urllib.request.urlopen in Python 3, I found that it gave empty responses for some sites. In other words, reading from the file-like object gives zero bytes. Python 2.x's urllib2.urlopen did not give this behavior. I isolated the problem down to the following difference:

@@ -1137,8 +1137,6 @@
             r = h.getresponse()  # an HTTPResponse instance
         except socket.error as err:
             raise URLError(err)
-        finally:
-            h.close()
 
         r.url = req.get_full_url()
         # This line replaces the .msg attribute of the HTTPResponse

The "finally" clause is absent in urllib2.py but present in Python 3.2's request.py. I think it has something to do with the HTTPConnection being closed before data could be read. Still, it's puzzling because some sites still give expected answers. Please find attached a small test script for "www.wsj.com" for which the response body should be empty without applying the above patch.
msg141040 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-07-24 05:17
Duplicate of #12576.
History
Date User Action Args
2022-04-11 14:57:20adminsetgithub: 56837
2011-07-24 05:17:41ezio.melottisetstatus: open -> closed

superseder: urlib.request fails to open some sites

nosy: + ezio.melotti
messages: + msg141040
resolution: duplicate
stage: resolved
2011-07-24 05:12:20royliucreate