[Python-Dev] Adding resume (206) support to urllib(2)

"Martin v. Löwis" martin at v.loewis.de
Wed Dec 13 08:30:00 CET 2006


Fredrik Lundh schrieb:
> given that urllib2 already supports partial requests, I'm not sure I see 
> the point of reimplementing this on top of httplib.  an example:
> 
>    import urllib2
> 
>    request = urllib2.Request("http://www.pythonware.com/daily/index.htm")
>    request.add_header("range", "bytes=0-999")

But what does this do if the URL was a file URL, or an ftp URL?
You have to know the syntax of the range header, and you have to
know the syntax of the content-range header, to process it. With
that, you can just as easily use httplib:

py> import httplib
py> h=httplib.HTTPConnection("www.pythonware.com")
py> h.putrequest("GET","/daily/index.htm")
py> h.putheader("range","bytes=0-999")
py> h.endheaders()
py> r=h.getresponse()
py> r.getheader("content-range")
'bytes 0-999/245105'
py> len(r.read())
1000

If you add protocol-specifics to urllib, the abstraction that urllib
provides goes away, and you are better off (IMO) to use the underlying
protocol library in the first place.

I'm not sure what the OP wanted to contribute in the first place
(given that it "works" already either way), but it might have been
a convenience API for the range header, and a parser for the
content-range header. That should go IMO into httplib, so that all
users of httplib get access to it, not just urllib*.

Regards,
Martin


More information about the Python-Dev mailing list