Learning Python Network Programming

(Sean Pound) #1

HTTP and Working with the Web


Do the following to view a response object's headers:





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








response.getheaders()





[('Date', 'Sun, 07 Sep 2014 19:58:48 GMT'), ('Server', 'Apache'),
('Content-Location', 'index.en.html'), ('Vary', 'negotiate,accept-
language,Accept-Encoding')...


The getheaders() method returns the headers as a list of tuples of the form
(header name, header value). A complete list of HTTP 1.1 headers and their
meanings can be found in RFC 7231. Let's look at how to use some headers in
requests and responses.


Customizing requests


To make use of the functionality that headers provide, we add headers to a request
before sending it. To do this, we can't just use urlopen(). We need to follow
these steps:



  • Create a Request object

  • Add headers to the request object

  • Use urlopen() to send the request object


We're going to learn how to customize a request for retrieving a Swedish version of
the Debian home page. We will use the Accept-Language header, which tells the
server our preferred language for the resource it returns. Note that not all servers
hold versions of resources in multiple languages, so not all servers will respond to
Accept-LanguageLinux home page.


First, we create a Request object:





from urllib.request import Request








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





Next we add the header:





req.add_header('Accept-Language', 'sv')





The add_header() method takes the name of the header and the contents of the
header as arguments. The Accept-Language header takes two-letter ISO 639-1
language codes. The code for Swedish is sv.


Lastly, we submit the customized request with urlopen():





response = urlopen(req)




Free download pdf