diff --git a/http-web-services.html b/http-web-services.html index 6b28d04..ce17a3d 100644 --- a/http-web-services.html +++ b/http-web-services.html @@ -397,7 +397,7 @@ reply: 'HTTP/1.1 200 OK'
httplib2 Handles Last-Modified and ETag headershttplib2 Handles Last-Modified and ETag HeadersFIXME @@ -442,7 +442,7 @@ reply: 'HTTP/1.1 304 Not Modified'
http2lib Handles compressionhttp2lib Handles CompressionFIXME @@ -468,10 +468,79 @@ reply: 'HTTP/1.1 200 OK' 'status': '304', 'vary': 'Accept-Encoding,User-Agent'} -
httplib2 Handles redirectshttplib2 Handles RedirectsFIXME +
+>>> response, content = h.request('http://diveintopython3.org/examples/feed-302.xml')
+connect: (diveintopython3.org, 80)
+send: b'GET /examples/feed-302.xml HTTP/1.1
+Host: diveintopython3.org
+accept-encoding: deflate, gzip
+user-agent: Python-httplib2/$Rev: 259 $'
+reply: 'HTTP/1.1 302 Found'
+send: b'GET /examples/feed.xml HTTP/1.1
+Host: diveintopython3.org
+accept-encoding: deflate, gzip
+user-agent: Python-httplib2/$Rev: 259 $'
+reply: 'HTTP/1.1 200 OK'
+>>> print(dict(response.items()))
+{'status': '200',
+ 'content-length': '3070',
+ 'content-location': 'http://diveintopython3.org/examples/feed.xml',
+ 'accept-ranges': 'bytes',
+ 'expires': 'Thu, 04 Jun 2009 02:21:41 GMT',
+ 'vary': 'Accept-Encoding',
+ 'server': 'Apache',
+ 'last-modified': 'Wed, 03 Jun 2009 02:20:15 GMT',
+ 'connection': 'close',
+ '-content-encoding': 'gzip',
+ 'etag': '"bfe-4cbbf5c0"',
+ 'cache-control': 'max-age=86400',
+ 'date': 'Wed, 03 Jun 2009 02:21:41 GMT',
+ 'content-type': 'application/xml'}
+>>> response, content = h.request('http://diveintopython3.org/examples/feed-302.xml')
+connect: (diveintopython3.org, 80)
+send: b'GET /examples/feed-302.xml HTTP/1.1
+Host: diveintopython3.org
+accept-encoding: deflate, gzip
+user-agent: Python-httplib2/$Rev: 259 $'
+reply: 'HTTP/1.1 302 Found'
+
+>>> response, content = h.request('http://diveintopython3.org/examples/feed-301.xml')
+connect: (diveintopython3.org, 80)
+send: b'GET /examples/feed-301.xml HTTP/1.1
+Host: diveintopython3.org
+accept-encoding: deflate, gzip
+user-agent: Python-httplib2/$Rev: 259 $'
+reply: 'HTTP/1.1 301 Moved Permanently'
+>>> print(dict(response.items()))
+{'status': '200',
+ 'content-length': '3070',
+ 'content-location': 'http://diveintopython3.org/examples/feed.xml',
+ 'accept-ranges': 'bytes',
+ 'expires': 'Thu, 04 Jun 2009 02:21:41 GMT',
+ 'vary': 'Accept-Encoding',
+ 'server': 'Apache',
+ 'last-modified': 'Wed, 03 Jun 2009 02:20:15 GMT',
+ 'connection': 'close',
+ '-content-encoding': 'gzip',
+ 'etag': '"bfe-4cbbf5c0"',
+ 'cache-control': 'max-age=86400',
+ 'date': 'Wed, 03 Jun 2009 02:21:41 GMT',
+ 'content-type': 'application/xml'}
+>>> response2, content2 = h.request('http://diveintopython3.org/examples/feed-301.xml')
+>>> response2.fromcache
+True
+⁂
FIXME
->>> import httplib2
->>> from urllib.parse import urlencode
->>> h = httplib2.Http('.cache')
->>> data = {"status": "Test update from Python 3"}
->>> h.add_credentials("diveintomark", "MY_SECRET_PASSWORD")
->>> resp, content = h.request("http://twitter.com/statuses/update.xml", "POST", urlencode(data))
->>> resp.status
+>>> import httplib2
+>>> from urllib.parse import urlencode
+>>> h = httplib2.Http('.cache')
+>>> data = {"status": "Test update from Python 3"}
+>>> h.add_credentials("diveintomark", "MY_SECRET_PASSWORD")
+>>> resp, content = h.request("http://twitter.com/statuses/update.xml", "POST", urlencode(data))
+>>> resp.status
200
->>> from xml.etree import ElementTree as etree
->>> tree = etree.fromstring(content)
->>> print(etree.tostring(tree))
+>>> from xml.etree import ElementTree as etree
+>>> tree = etree.fromstring(content)
+>>> print(etree.tostring(tree))
<status>
<created_at>Sat May 30 19:11:38 +0000 2009</created_at>
<id>1973974228</id>
@@ -538,13 +607,13 @@ reply: 'HTTP/1.1 200 OK'
FIXME
-
->>> tree.findtext("id")
-'1973974228'
->>> resp, delete_content = h.request("http://twitter.com/statuses/destroy/{0}.xml".format(tree.findtext("id")), "DELETE")
->>> resp.status
-200
-
+
+# continued from the previous example
+>>> tree.findtext("id")
+'1973974228'
+>>> resp, delete_content = h.request("http://twitter.com/statuses/destroy/{0}.xml".format(tree.findtext("id")), "DELETE")
+>>> resp.status
+200
⁂