OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 <html> |
| 3 <head> |
| 4 <link rel="STYLESHEET" href="ref.css" type='text/css' /> |
| 5 <link rel="first" href="ref.html" title='The httplib2 Library' /> |
| 6 <link rel='contents' href='contents.html' title="Contents" /> |
| 7 <link rel='last' href='about.html' title='About this document...' /> |
| 8 <link rel='help' href='about.html' title='About this document...' /> |
| 9 <link rel="prev" href="response-objects.html" /> |
| 10 <link rel="parent" href="module-httplib2.html" /> |
| 11 <link rel="next" href="about.html" /> |
| 12 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 13 <meta name='aesop' content='information' /> |
| 14 <title>1.1.4 Examples </title> |
| 15 </head> |
| 16 <body> |
| 17 <div class="navigation"> |
| 18 <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 19 <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 20 <tr> |
| 21 <td class='online-navigation'><a rel="prev" title="1.1.3 response Objects" |
| 22 href="response-objects.html"><img src='previous.png' |
| 23 border='0' height='32' alt='Previous Page' width='32' /></a></td> |
| 24 <td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensiv
e" |
| 25 href="module-httplib2.html"><img src='up.png' |
| 26 border='0' height='32' alt='Up one Level' width='32' /></a></td> |
| 27 <td class='online-navigation'><a rel="next" title="About this document ..." |
| 28 href="about.html"><img src='next.png' |
| 29 border='0' height='32' alt='Next Page' width='32' /></a></td> |
| 30 <td align="center" width="100%">The httplib2 Library</td> |
| 31 <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 32 href="contents.html"><img src='contents.png' |
| 33 border='0' height='32' alt='Contents' width='32' /></a></td> |
| 34 <td class='online-navigation'><img src='blank.png' |
| 35 border='0' height='32' alt='' width='32' /></td> |
| 36 <td class='online-navigation'><img src='blank.png' |
| 37 border='0' height='32' alt='' width='32' /></td> |
| 38 </tr></table> |
| 39 <div class='online-navigation'> |
| 40 <b class="navlabel">Previous:</b> |
| 41 <a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Object
s</a> |
| 42 <b class="navlabel">Up:</b> |
| 43 <a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A compr
ehensive</a> |
| 44 <b class="navlabel">Next:</b> |
| 45 <a class="sectref" rel="next" href="about.html">About this document ...</a> |
| 46 </div> |
| 47 <hr /></div> |
| 48 </div> |
| 49 <!--End of Navigation Panel--> |
| 50 |
| 51 <h2><a name="SECTION002140000000000000000"></a><a name="httplib2-example"></a> |
| 52 <br> |
| 53 1.1.4 Examples |
| 54 </h2> |
| 55 |
| 56 <p> |
| 57 To do a simple <code>GET</code> request just supply the absolute URI |
| 58 of the resource: |
| 59 |
| 60 <p> |
| 61 <div class="verbatim"><pre> |
| 62 import httplib2 |
| 63 h = httplib2.Http() |
| 64 resp, content = h.request("http://bitworking.org/") |
| 65 assert resp.status == 200 |
| 66 assert resp['content-type'] == 'text/html' |
| 67 </pre></div> |
| 68 |
| 69 <p> |
| 70 Here is more complex example that does a PUT |
| 71 of some text to a resource that requires authentication. |
| 72 The Http instance also uses a file cache |
| 73 in the directory <code>.cache</code>. |
| 74 |
| 75 <p> |
| 76 <div class="verbatim"><pre> |
| 77 import httplib2 |
| 78 h = httplib2.Http(".cache") |
| 79 h.add_credentials('name', 'password') |
| 80 resp, content = h.request("https://example.org/chap/2", |
| 81 "PUT", body="This is text", |
| 82 headers={'content-type':'text/plain'} ) |
| 83 </pre></div> |
| 84 |
| 85 <p> |
| 86 Here is an example that connects to a server that |
| 87 supports the Atom Publishing Protocol. |
| 88 |
| 89 <p> |
| 90 <div class="verbatim"><pre> |
| 91 import httplib2 |
| 92 h = httplib2.Http() |
| 93 h.add_credentials(myname, mypasswd) |
| 94 h.follow_all_redirects = True |
| 95 headers = {'Content-Type': 'application/atom+xml'} |
| 96 body = """<?xml version="1.0" ?> |
| 97 <entry xmlns="http://www.w3.org/2005/Atom"> |
| 98 <title>Atom-Powered Robots Run Amok</title> |
| 99 <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> |
| 100 <updated>2003-12-13T18:30:02Z</updated> |
| 101 <author><name>John Doe</name></author> |
| 102 <content>Some text.</content> |
| 103 </entry> |
| 104 """ |
| 105 uri = "http://www.example.com/collection/" |
| 106 resp, content = h.request(uri, "POST", body=body, headers=headers) |
| 107 </pre></div> |
| 108 |
| 109 <p> |
| 110 Here is an example of providing data to an HTML form processor. |
| 111 In this case we presume this is a POST form. We need to take our |
| 112 data and format it as "application/x-www-form-urlencoded" data and use that as a |
| 113 body for a POST request. |
| 114 |
| 115 <p> |
| 116 <div class="verbatim"><pre> |
| 117 >>> import httplib2 |
| 118 >>> import urllib |
| 119 >>> data = {'name': 'fred', 'address': '123 shady lane'} |
| 120 >>> body = urllib.urlencode(data) |
| 121 >>> body |
| 122 'name=fred&address=123+shady+lane' |
| 123 >>> h = httplib2.Http() |
| 124 >>> resp, content = h.request("http://example.com", method="POST", body
=body) |
| 125 </pre></div> |
| 126 |
| 127 <p> |
| 128 Here is an example of using a proxy server: |
| 129 <div class="verbatim"><pre> |
| 130 import httplib2 |
| 131 import socks |
| 132 |
| 133 httplib2.debuglevel=4 |
| 134 h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'localh
ost', 8000)) |
| 135 r,c = h.request("http://bitworking.org/news/") |
| 136 </pre></div> |
| 137 |
| 138 <p> |
| 139 |
| 140 <p> |
| 141 <IMG |
| 142 WIDTH="556" HEIGHT="20" ALIGN="BOTTOM" BORDER="0" |
| 143 SRC="img1.png" |
| 144 ALT="\begin{center}\vbox{\input{modref.ind} |
| 145 }\end{center}"> |
| 146 <p> |
| 147 |
| 148 <p> |
| 149 |
| 150 <div class="navigation"> |
| 151 <div class='online-navigation'> |
| 152 <p></p><hr /> |
| 153 <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 154 <tr> |
| 155 <td class='online-navigation'><a rel="prev" title="1.1.3 response Objects" |
| 156 href="response-objects.html"><img src='previous.png' |
| 157 border='0' height='32' alt='Previous Page' width='32' /></a></td> |
| 158 <td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensiv
e" |
| 159 href="module-httplib2.html"><img src='up.png' |
| 160 border='0' height='32' alt='Up one Level' width='32' /></a></td> |
| 161 <td class='online-navigation'><a rel="next" title="About this document ..." |
| 162 href="about.html"><img src='next.png' |
| 163 border='0' height='32' alt='Next Page' width='32' /></a></td> |
| 164 <td align="center" width="100%">The httplib2 Library</td> |
| 165 <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 166 href="contents.html"><img src='contents.png' |
| 167 border='0' height='32' alt='Contents' width='32' /></a></td> |
| 168 <td class='online-navigation'><img src='blank.png' |
| 169 border='0' height='32' alt='' width='32' /></td> |
| 170 <td class='online-navigation'><img src='blank.png' |
| 171 border='0' height='32' alt='' width='32' /></td> |
| 172 </tr></table> |
| 173 <div class='online-navigation'> |
| 174 <b class="navlabel">Previous:</b> |
| 175 <a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Object
s</a> |
| 176 <b class="navlabel">Up:</b> |
| 177 <a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A compr
ehensive</a> |
| 178 <b class="navlabel">Next:</b> |
| 179 <a class="sectref" rel="next" href="about.html">About this document ...</a> |
| 180 </div> |
| 181 </div> |
| 182 <hr /> |
| 183 <span class="release-info">Release 0.3, documentation updated on Mar 8, 2007.</s
pan> |
| 184 </div> |
| 185 <!--End of Navigation Panel--> |
| 186 |
| 187 </body> |
| 188 </html> |
OLD | NEW |