jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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" /> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 12 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 13 | <meta name='aesop' content='information' /> |
| 14 | <title>1.1.4 Examples </title> |
| 15 | </head> |
| 16 | <body> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 17 | <div class="navigation"> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 18 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 19 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 20 | <tr> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 21 | <td class='online-navigation'><a rel="prev" title="1.1.3 response Objects" |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 22 | href="response-objects.html"><img src='previous.png' |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 23 | border='0' height='32' alt='Previous Page' width='32' /></a></td> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 24 | <td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive" |
| 25 | href="module-httplib2.html"><img src='up.png' |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 26 | border='0' height='32' alt='Up one Level' width='32' /></a></td> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 27 | <td class='online-navigation'><a rel="next" title="About this document ..." |
| 28 | href="about.html"><img src='next.png' |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 29 | border='0' height='32' alt='Next Page' width='32' /></a></td> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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' |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 33 | border='0' height='32' alt='Contents' width='32' /></a></td> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 41 | <a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</a> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 42 | <b class="navlabel">Up:</b> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 43 | <a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</a> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 44 | <b class="navlabel">Next:</b> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 45 | <a class="sectref" rel="next" href="about.html">About this document ...</a> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 46 | </div> |
| 47 | <hr /></div> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 48 | </div> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 49 | <!--End of Navigation Panel--> |
| 50 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 51 | <h2><a name="SECTION002140000000000000000"></a><a name="httplib2-example"></a> |
| 52 | <br> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 53 | 1.1.4 Examples |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 54 | </h2> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 55 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 56 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 57 | To do a simple <code>GET</code> request just supply the absolute URI |
| 58 | of the resource: |
| 59 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 60 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 69 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 75 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 85 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 86 | Here is an example that connects to a server that |
| 87 | supports the Atom Publishing Protocol. |
| 88 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 89 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 109 | <p> |
jcgregorio | cde4f09 | 2007-03-08 21:59:04 +0000 | [diff] [blame] | 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 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 115 | <p> |
jcgregorio | cde4f09 | 2007-03-08 21:59:04 +0000 | [diff] [blame] | 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> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 126 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 127 | <p> |
joe.gregorio | ab704ef | 2007-10-23 15:26:12 +0000 | [diff] [blame] | 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, 'localhost', 8000)) |
| 135 | r,c = h.request("http://bitworking.org/news/") |
| 136 | </pre></div> |
| 137 | |
| 138 | <p> |
| 139 | |
| 140 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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}"> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 146 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 147 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 148 | <p> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 149 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 150 | <div class="navigation"> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 151 | <div class='online-navigation'> |
| 152 | <p></p><hr /> |
| 153 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 154 | <tr> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 155 | <td class='online-navigation'><a rel="prev" title="1.1.3 response Objects" |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 156 | href="response-objects.html"><img src='previous.png' |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 157 | border='0' height='32' alt='Previous Page' width='32' /></a></td> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 158 | <td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive" |
| 159 | href="module-httplib2.html"><img src='up.png' |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 160 | border='0' height='32' alt='Up one Level' width='32' /></a></td> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 161 | <td class='online-navigation'><a rel="next" title="About this document ..." |
| 162 | href="about.html"><img src='next.png' |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 163 | border='0' height='32' alt='Next Page' width='32' /></a></td> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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' |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 167 | border='0' height='32' alt='Contents' width='32' /></a></td> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 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> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 175 | <a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</a> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 176 | <b class="navlabel">Up:</b> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 177 | <a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</a> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 178 | <b class="navlabel">Next:</b> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 179 | <a class="sectref" rel="next" href="about.html">About this document ...</a> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 180 | </div> |
| 181 | </div> |
| 182 | <hr /> |
jcgregorio | cde4f09 | 2007-03-08 21:59:04 +0000 | [diff] [blame] | 183 | <span class="release-info">Release 0.3, documentation updated on Mar 8, 2007.</span> |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 184 | </div> |
jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame] | 185 | <!--End of Navigation Panel--> |
| 186 | |
jcgregorio | 88ada9a | 2007-05-03 13:20:38 +0000 | [diff] [blame] | 187 | </body> |
| 188 | </html> |