No Content-Length header, nor length property

Kev Dwyer kevin.p.dwyer at gmail.com
Wed Jul 8 08:50:09 CEST 2015


zljubisic at gmail.com wrote:

> 
> 
>> Hello,
>> 
>> urlopen returns an HttpResponse
>> object(https://docs.python.org/3/library/http.client.html#httpresponse-objects).
>>  You need to call read() on the return value to get the page content, or
>> you could consider the getheader method to check for a Content- Length
>> header.
>> 
>> Hope that helps,
>> 
>> Kev
> 
> Kev, did you mean?
> 
> import urllib.request
> 
> url = 'http://radio.hrt.hr/prvi-program/aod/download/118467/'
> site = urllib.request.urlopen(url)
> 
> print( site.getheader('Content-Length'))    # None
> x = site.read(1)                            # No 'Content-Length' header
> 
> print('File size:', site.length)
> 
> I am still not getting anywhere. :(
> 
> Regards.


Ah - looking at the response headers, they include "Transfer-Encoding	
chunked" - I don't think urlopen handles chunked responses by default, 
though I could be wrong, I don't have time to check the docs right now.

The requests library (https://pypi.python.org/pypi/requests) seems to handle 
them - http://docs.python-requests.org/en/latest/user/advanced/#chunk-encoded-requests





More information about the Python-list mailing list