Learning Python Network Programming

(Sean Pound) #1
Chapter 2

b'',


b' ',


b' Debian -- The Universal Operating System ']


Encodings are registered with IANA. The current list contains: gzip, compress,
deflate, and identity. The first three refer to specific compression methods.
The last one allows the client to specify that it doesn't want any encoding applied
to the content.


Let's see what happens if we ask for no compression by using the identity
encoding:





req = Request('http://www.debian.org')








req.add_header('Accept-Encoding', 'identity')








response = urlopen(req)








print(response.getheader('Content-Encoding'))





None


When a server uses the identity encoding type, no Content-Encoding header is
included in the response.


Multiple values


To tell the server that we can accept more than one encoding, add more values to the
Accept-Encoding header and separate them by commas. Let's try it. We create our
Request object:





req = Request('http://www.debian.org')





Then, we add our header, and this time we include more encodings:





encodings = 'deflate, gzip, identity'








req.add_header('Accept-Encoding', encodings)





Now, we submit the request and then check the response encoding:





response = urlopen(req)








response.getheader('Content-Encoding')





'gzip'


If needed, relative weightings can be given to specific encodings by adding a q value:





encodings = 'gzip, deflate;q=0.8, identity;q=0.0'




Free download pdf