diff --git a/http-web-services.html b/http-web-services.html index b9db5ea..f16dabe 100644 --- a/http-web-services.html +++ b/http-web-services.html @@ -300,7 +300,7 @@ Content-Type: application/xml >>> data2 == data ③ True
Cache-Control and Expires to allow caching, Last-Modified and ETag to enable “not-modified” tracking. Even the Vary: Accept-Encoding header hints that the server would support compression, if only you would bloody well ask for it. But you’re not listening.
+Cache-Control and Expires to allow caching, Last-Modified and ETag to enable “not-modified” tracking. Even the Vary: Accept-Encoding header hints that the server would support compression, if only you would ask for it. But you didn’t.
httplib2FIXME +
To use httplib2, create an instance of the httplib2.Http class.
-
httplib2 handles caching
+>>> import httplib2
+>>> h = httplib2.Http('.cache')
+>>> response, content = h.request('http://diveintopython3.org/examples/feed.xml')
+>>> response.status
+200
+>>> content[:52]
+b'<?xml version="1.0" encoding="utf-8"?>\r\n<feed xmlns='
+>>> len(content)
+3070
+httplib2 Handles CachingFIXME -
httplib2 handles Last-Modified and ETag headers
+# continued from previous example
+>>> response2, content2 = h.request('http://diveintopython3.org/examples/feed.xml')
+>>> response2.status
+200
+>>> content2[:52]
+b'<?xml version="1.0" encoding="utf-8"?>\r\n<feed xmlns='
+>>> len(content2)
+3070
+
+# NOT continued from previous example!
+# Please exit out of the interactive shell
+# and launch a new one.
+>>> import httplib2
+>>> httplib2.debuglevel = 1
+>>> h = httplib2.Http('.cache')
+>>> response, content = h.request('http://diveintopython3.org/examples/feed.xml')
+>>> len(content)
+3070
+>>> response.status
+200
+>>> response.fromcache
+True
+
+# continued from previous example
+>>> response2, content2 = h.request('http://diveintopython3.org/examples/feed.xml',
+... headers={'cache-control':'no-cache'})
+connect: (diveintopython3.org, 80)
+send: b'GET /examples/feed.xml HTTP/1.1
+Host: diveintopython3.org
+user-agent: Python-httplib2/$Rev: 259 $
+accept-encoding: deflate, gzip
+cache-control: no-cache'
+reply: 'HTTP/1.1 200 OK'
+…further debugging information omitted…
+>>> response2.status
+200
+>>> response2.fromcache
+False
+>>> print(dict(response2.items()))
+{'status': '200',
+ 'content-length': '3070',
+ 'content-location': 'http://diveintopython3.org/examples/feed.xml',
+ 'accept-ranges': 'bytes',
+ 'expires': 'Wed, 03 Jun 2009 00:40:26 GMT',
+ 'vary': 'Accept-Encoding',
+ 'server': 'Apache',
+ 'last-modified': 'Sun, 31 May 2009 22:51:11 GMT',
+ 'connection': 'close',
+ '-content-encoding': 'gzip',
+ 'etag': '"bfe-255ef5c0"',
+ 'cache-control': 'max-age=86400',
+ 'date': 'Tue, 02 Jun 2009 00:40:26 GMT',
+ 'content-type': 'application/xml'}
+httplib2 Handles Last-Modified and ETag headersFIXME -
http2lib handles compressionhttp2lib Handles compressionFIXME -
httplib2 handles redirectshttplib2 Handles redirectsFIXME