From ad607dd9a8f08023050166c04fbbfea88ddf2d61 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Tue, 2 Jun 2009 19:32:53 -0700 Subject: [PATCH] finished stubbing examples for HTTP chapter --- http-web-services.html | 109 +++++++++++++++++++++++++++++++++-------- 1 file changed, 89 insertions(+), 20 deletions(-) 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'
  • FIXME -

    How httplib2 Handles Last-Modified and ETag headers

    +

    How httplib2 Handles Last-Modified and ETag Headers

    FIXME @@ -442,7 +442,7 @@ reply: 'HTTP/1.1 304 Not Modified'

  • FIXME -

    How http2lib Handles compression

    +

    How http2lib Handles Compression

    FIXME @@ -468,10 +468,79 @@ reply: 'HTTP/1.1 200 OK' 'status': '304', 'vary': 'Accept-Encoding,User-Agent'} -

    How httplib2 Handles redirects

    +

    How httplib2 Handles Redirects

    FIXME +

    +>>> 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'
    +
      +
    1. FIXME +
    + +
    +>>> 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
    +
      +
    1. FIXME +
    +

    Beyond HTTP GET

    @@ -479,17 +548,17 @@ reply: 'HTTP/1.1 200 OK'

    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