Why such different HTTP response results between 2.5 and 3.0

an0 an00na at gmail.com
Mon Feb 2 01:01:17 EST 2009


Below are two semantically same snippets for querying the same partial
HTTP response, for Python2.5 and Python 3.0 respectively.
However, the 3.0 version returns a not-so-right result(msg) which is a
bytes of length 239775, while the 2.5 version returns a good msg which
is a 239733 byte-long string that is the content of a proper zip file.
I really can't figure out what's wrong, thought I've sought out some
"\r\n" segments in msg 3.0 that is absent in msg 2.5.
So are there anyone could give me some hints? Thanks in advance.

Code:

# Python 2.5
import urllib2
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm="pluses and minuses",
                          uri='http://www.pythonchallenge.com/pc/hex/
unreal.jpg',
                          user='butter',
                          passwd='fly')
opener = urllib2.build_opener(auth_handler)

req = urllib2.Request('http://www.pythonchallenge.com/pc/hex/
unreal.jpg')
req.add_header('Range', 'bytes=1152983631-')
res = opener.open(req)
msg = res.read()

# Python 3.0
import urllib.request
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm="pluses and minuses",
                          uri='http://www.pythonchallenge.com/pc/hex/
unreal.jpg',
                          user='butter',
                          passwd='fly')
opener = urllib.request.build_opener(auth_handler)

req = urllib.request.Request('http://www.pythonchallenge.com/pc/hex/
unreal.jpg')
req.add_header('Range', 'bytes=1152983631-')
res = opener.open(req)
msg = res.read()



More information about the Python-list mailing list