Learning Python Network Programming

(Sean Pound) #1

HTTP and Working with the Web


The q value follows the encoding name, and it is separated by a semicolon. The
maximum q value is 1.0, and this is also the default if no q value is given. So, the
preceding line should be interpreted as my first preference for encoding is gzip, my
second preference is deflate, and my third preference is identity, if nothing else
is available.


Content negotiation


Content compression with the Accept-Encoding header and language selection
with the Accept-Language header are examples of content negotiation, where the
client specifies its preferences regarding the format and the content of the requested
resource. The following headers can also be used for this:



  • Accept: For requesting a preferred file format

  • Accept-Charset: For requesting the resource in a preferred character set


There are additional aspects to the content negotiation mechanism, but because it's
inconsistently supported and it can become quite involved, we won't be covering it
in this chapter. RFC 7231 contain all the details that you need. Take a look at sections
such as 3.4, 5.3, 6.4.1, and 6.5.6, if you find that your application requires this.


Content types


HTTP can be used as a transport for any type of file or data. The server can use the
Content-Type header in a response to inform the client about the type of data that
it has sent in the body. This is the primary means an HTTP client determines how it
should handle the body data that the server returns to it.


To view the content type, we inspect the value of the response header,
as shown here:





response = urlopen('http://www.debian.org')








response.getheader('Content-Type')





'text/html'


The values in this header are taken from a list which is maintained by IANA. These
values are variously called content types, Internet media types, or MIME types
(MIME stands for Multipurpose Internet Mail Extensions, the specification
in which the convention was first established). The full list can be found at
http://www.iana.org/assignments/media-types.

Free download pdf