download x bytes at a time over network

R. David Murray rdmurray at bitdance.com
Tue Mar 17 07:13:18 EDT 2009


Saurabh <phonethics at gmail.com> wrote:
> > This isn't exactly how things work.  The server *sends* you bytes.  It can
> > send you a lot at once.  To some extent you can control how much it sends
> > before it waits for you to catch up, but you don't have anywhere near
> > byte-level control (you might have something like 32kb or 64kb level
> > control).
> 
> What abt in Python3 ?
> It seems to have some header like the one below : b'b495 - binary mode
> with 46229 bytes ? Or is it something else ?
> 
> >>> import urllib.request
> >>> url = "http://feeds2.feedburner.com/jquery/"
> >>> handler = urllib.request.urlopen(url)
> >>> data = handler.read(1000)
> >>> print("""Content :\n%s \n%s \n%s""" % ('=' * 100, data, '=' * 100))
> Content :
> ====================================================================================================
> b'b495\r\n<?xml version="1.0" encoding="UTF-8"?>\r\n<?xml-stylesheet

That "b'..." is the string representation of the bytes object returned
by urllib.request.  Remember that in python3 bytes and strings are two
very different types.  You get bytes from urllib.request because urllib
can't know what encoding the bytes are in.  You have to decide how to
decode them in order to convert it into a unicode string object.

--
R. David Murray           http://www.bitdance.com




More information about the Python-list mailing list