From 658e07932d8b7ca81904787cb8cb35c5a92a3a43 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 7 Jun 2009 16:15:19 -0400 Subject: [PATCH] new content! for real! like, with words and stuff! --- http-web-services.html | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/http-web-services.html b/http-web-services.html index 74a7119..3cfe9e2 100644 --- a/http-web-services.html +++ b/http-web-services.html @@ -315,18 +315,25 @@ Content-Type: application/xml
 >>> import httplib2
->>> h = httplib2.Http('.cache')
->>> response, content = h.request('http://diveintopython3.org/examples/feed.xml')
->>> response.status
+>>> h = httplib2.Http('.cache')                                                    
+>>> response, content = h.request('http://diveintopython3.org/examples/feed.xml')  
+>>> response.status                                                                
 200
->>> content[:52]
+>>> content[:52]                                                                   
 b"<?xml version='1.0' encoding='utf-8'?>\r\n<feed xmlns="
 >>> len(content)
 3070
    -
  1. FIXME +
  2. The primary interface to httplib2 is the Http object. For reasons you’ll see in the next section, you should always pass a directory name when you create an Http object. The directory does not need to exist; httplib2 will create it if necessary. +
  3. Once you have an Http object, retrieving data is as simple as calling the request() method with the address of the data you want. This will issue an HTTP GET request for that URL. (Later in this chapter, you’ll see how to issue other HTTP requests, like POST.) +
  4. The request() method returns two values. The first is an httplib2.Response object, which contains all the HTTP headers the server returned. For example, a status code of 200 indicates that the request was successful. +
  5. The content variable contains the actual data that was returned by the HTTP server. The data is returned as bytes, not str. If you want it as a string, you’ll need to determine the character encoding and explicitly convert it.
+
+

You probably only need one httplib2.Http object. There are valid reasons for creating more than one, but you should only do so if you know why you need them. “I need to request data from two different URLs” is not a valid reason. Re-use the Http object and just call the request() method twice. +

+

How httplib2 Handles Caching

FIXME