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
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.
+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.)
+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.
+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.Httpobject. 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 theHttpobject and just call therequest()method twice. +
httplib2 Handles CachingFIXME