Request and Response Objects

Whenever a call is made to requests.get() and friends you are doing two major things. First, you are constructing a Request object which will be sent off to a server to request or query some resource. Second, a Response object is generated once requests gets a response back from the server. The Response object contains all of the information returned by the server and also contains the Request object you created originally. Here is a simple request to get some very important information from Wikipedia’s servers:

  1. >>> r = requests.get('http://en.wikipedia.org/wiki/Monty_Python')

If we want to access the headers the server sent back to us, we do this:

  1. >>> r.headers
  2. {'content-length': '56170', 'x-content-type-options': 'nosniff', 'x-cache':
  3. 'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', 'content-encoding':
  4. 'gzip', 'age': '3080', 'content-language': 'en', 'vary': 'Accept-Encoding,Cookie',
  5. 'server': 'Apache', 'last-modified': 'Wed, 13 Jun 2012 01:33:50 GMT',
  6. 'connection': 'close', 'cache-control': 'private, s-maxage=0, max-age=0,
  7. must-revalidate', 'date': 'Thu, 14 Jun 2012 12:59:39 GMT', 'content-type':
  8. 'text/html; charset=UTF-8', 'x-cache-lookup': 'HIT from cp1006.eqiad.wmnet:3128,
  9. MISS from cp1010.eqiad.wmnet:80'}

However, if we want to get the headers we sent the server, we simply access the request, and then the request’s headers:

  1. >>> r.request.headers
  2. {'Accept-Encoding': 'identity, deflate, compress, gzip',
  3. 'Accept': '*/*', 'User-Agent': 'python-requests/1.2.0'}