blob: 756942b054e28fb930220700983c85a7aafe3f59 [file] [log] [blame]
jcgregorio26c0cd72006-07-03 17:36:17 +00001<!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" />
jcgregorio88ada9a2007-05-03 13:20:38 +000012<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
jcgregorio26c0cd72006-07-03 17:36:17 +000013<meta name='aesop' content='information' />
14<title>1.1.4 Examples </title>
15</head>
16<body>
jcgregorio88ada9a2007-05-03 13:20:38 +000017<div class="navigation">
jcgregorio26c0cd72006-07-03 17:36:17 +000018<div id='top-navigation-panel' xml:id='top-navigation-panel'>
19<table align="center" width="100%" cellpadding="0" cellspacing="2">
20<tr>
jcgregorio88ada9a2007-05-03 13:20:38 +000021<td class='online-navigation'><a rel="prev" title="1.1.3 response Objects"
jcgregorio26c0cd72006-07-03 17:36:17 +000022 href="response-objects.html"><img src='previous.png'
jcgregorio88ada9a2007-05-03 13:20:38 +000023 border='0' height='32' alt='Previous Page' width='32' /></a></td>
jcgregorio26c0cd72006-07-03 17:36:17 +000024<td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive"
25 href="module-httplib2.html"><img src='up.png'
jcgregorio88ada9a2007-05-03 13:20:38 +000026 border='0' height='32' alt='Up one Level' width='32' /></a></td>
jcgregorio26c0cd72006-07-03 17:36:17 +000027<td class='online-navigation'><a rel="next" title="About this document ..."
28 href="about.html"><img src='next.png'
jcgregorio88ada9a2007-05-03 13:20:38 +000029 border='0' height='32' alt='Next Page' width='32' /></a></td>
jcgregorio26c0cd72006-07-03 17:36:17 +000030<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'
jcgregorio88ada9a2007-05-03 13:20:38 +000033 border='0' height='32' alt='Contents' width='32' /></a></td>
jcgregorio26c0cd72006-07-03 17:36:17 +000034<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>
jcgregorio88ada9a2007-05-03 13:20:38 +000041<a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</a>
jcgregorio26c0cd72006-07-03 17:36:17 +000042<b class="navlabel">Up:</b>
jcgregorio88ada9a2007-05-03 13:20:38 +000043<a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</a>
jcgregorio26c0cd72006-07-03 17:36:17 +000044<b class="navlabel">Next:</b>
jcgregorio88ada9a2007-05-03 13:20:38 +000045<a class="sectref" rel="next" href="about.html">About this document ...</a>
jcgregorio26c0cd72006-07-03 17:36:17 +000046</div>
47<hr /></div>
jcgregorio88ada9a2007-05-03 13:20:38 +000048</div>
jcgregorio26c0cd72006-07-03 17:36:17 +000049<!--End of Navigation Panel-->
50
jcgregorio88ada9a2007-05-03 13:20:38 +000051<h2><a name="SECTION002140000000000000000"></a><a name="httplib2-example"></a>
52<br>
jcgregorio26c0cd72006-07-03 17:36:17 +0000531.1.4 Examples
jcgregorio88ada9a2007-05-03 13:20:38 +000054</h2>
jcgregorio26c0cd72006-07-03 17:36:17 +000055
jcgregorio88ada9a2007-05-03 13:20:38 +000056<p>
jcgregorio26c0cd72006-07-03 17:36:17 +000057To do a simple <code>GET</code> request just supply the absolute URI
58of the resource:
59
jcgregorio88ada9a2007-05-03 13:20:38 +000060<p>
jcgregorio26c0cd72006-07-03 17:36:17 +000061<div class="verbatim"><pre>
62import httplib2
63h = httplib2.Http()
64resp, content = h.request("http://bitworking.org/")
65assert resp.status == 200
66assert resp['content-type'] == 'text/html'
67</pre></div>
68
jcgregorio88ada9a2007-05-03 13:20:38 +000069<p>
jcgregorio26c0cd72006-07-03 17:36:17 +000070Here is more complex example that does a PUT
71of some text to a resource that requires authentication.
72The Http instance also uses a file cache
73in the directory <code>.cache</code>.
74
jcgregorio88ada9a2007-05-03 13:20:38 +000075<p>
jcgregorio26c0cd72006-07-03 17:36:17 +000076<div class="verbatim"><pre>
77import httplib2
78h = httplib2.Http(".cache")
79h.add_credentials('name', 'password')
80resp, content = h.request("https://example.org/chap/2",
81 "PUT", body="This is text",
82 headers={'content-type':'text/plain'} )
83</pre></div>
84
jcgregorio88ada9a2007-05-03 13:20:38 +000085<p>
jcgregorio26c0cd72006-07-03 17:36:17 +000086Here is an example that connects to a server that
87supports the Atom Publishing Protocol.
88
jcgregorio88ada9a2007-05-03 13:20:38 +000089<p>
jcgregorio26c0cd72006-07-03 17:36:17 +000090<div class="verbatim"><pre>
91import httplib2
92h = httplib2.Http()
93h.add_credentials(myname, mypasswd)
94h.follow_all_redirects = True
95headers = {'Content-Type': 'application/atom+xml'}
96body = """&lt;?xml version="1.0" ?&gt;
97 &lt;entry xmlns="http://www.w3.org/2005/Atom"&gt;
98 &lt;title&gt;Atom-Powered Robots Run Amok&lt;/title&gt;
99 &lt;id&gt;urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a&lt;/id&gt;
100 &lt;updated&gt;2003-12-13T18:30:02Z&lt;/updated&gt;
101 &lt;author&gt;&lt;name&gt;John Doe&lt;/name&gt;&lt;/author&gt;
102 &lt;content&gt;Some text.&lt;/content&gt;
103&lt;/entry&gt;
104"""
105uri = "http://www.example.com/collection/"
106resp, content = h.request(uri, "POST", body=body, headers=headers)
107</pre></div>
108
jcgregorio88ada9a2007-05-03 13:20:38 +0000109<p>
jcgregoriocde4f092007-03-08 21:59:04 +0000110Here is an example of providing data to an HTML form processor.
111In this case we presume this is a POST form. We need to take our
112data and format it as "application/x-www-form-urlencoded" data and use that as a
113body for a POST request.
114
jcgregorio88ada9a2007-05-03 13:20:38 +0000115<p>
jcgregoriocde4f092007-03-08 21:59:04 +0000116<div class="verbatim"><pre>
117&gt;&gt;&gt; import httplib2
118&gt;&gt;&gt; import urllib
119&gt;&gt;&gt; data = {'name': 'fred', 'address': '123 shady lane'}
120&gt;&gt;&gt; body = urllib.urlencode(data)
121&gt;&gt;&gt; body
122'name=fred&amp;address=123+shady+lane'
123&gt;&gt;&gt; h = httplib2.Http()
124&gt;&gt;&gt; resp, content = h.request("http://example.com", method="POST", body=body)
125</pre></div>
jcgregorio26c0cd72006-07-03 17:36:17 +0000126
jcgregorio88ada9a2007-05-03 13:20:38 +0000127<p>
joe.gregorioab704ef2007-10-23 15:26:12 +0000128Here is an example of using a proxy server:
129<div class="verbatim"><pre>
130import httplib2
131import socks
132
133httplib2.debuglevel=4
134h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'localhost', 8000))
135r,c = h.request("http://bitworking.org/news/")
136</pre></div>
137
138<p>
139
140<p>
jcgregorio26c0cd72006-07-03 17:36:17 +0000141<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}">
jcgregorio88ada9a2007-05-03 13:20:38 +0000146<p>
jcgregorio26c0cd72006-07-03 17:36:17 +0000147
jcgregorio88ada9a2007-05-03 13:20:38 +0000148<p>
jcgregorio26c0cd72006-07-03 17:36:17 +0000149
jcgregorio88ada9a2007-05-03 13:20:38 +0000150<div class="navigation">
jcgregorio26c0cd72006-07-03 17:36:17 +0000151<div class='online-navigation'>
152<p></p><hr />
153<table align="center" width="100%" cellpadding="0" cellspacing="2">
154<tr>
jcgregorio88ada9a2007-05-03 13:20:38 +0000155<td class='online-navigation'><a rel="prev" title="1.1.3 response Objects"
jcgregorio26c0cd72006-07-03 17:36:17 +0000156 href="response-objects.html"><img src='previous.png'
jcgregorio88ada9a2007-05-03 13:20:38 +0000157 border='0' height='32' alt='Previous Page' width='32' /></a></td>
jcgregorio26c0cd72006-07-03 17:36:17 +0000158<td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive"
159 href="module-httplib2.html"><img src='up.png'
jcgregorio88ada9a2007-05-03 13:20:38 +0000160 border='0' height='32' alt='Up one Level' width='32' /></a></td>
jcgregorio26c0cd72006-07-03 17:36:17 +0000161<td class='online-navigation'><a rel="next" title="About this document ..."
162 href="about.html"><img src='next.png'
jcgregorio88ada9a2007-05-03 13:20:38 +0000163 border='0' height='32' alt='Next Page' width='32' /></a></td>
jcgregorio26c0cd72006-07-03 17:36:17 +0000164<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'
jcgregorio88ada9a2007-05-03 13:20:38 +0000167 border='0' height='32' alt='Contents' width='32' /></a></td>
jcgregorio26c0cd72006-07-03 17:36:17 +0000168<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>
jcgregorio88ada9a2007-05-03 13:20:38 +0000175<a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</a>
jcgregorio26c0cd72006-07-03 17:36:17 +0000176<b class="navlabel">Up:</b>
jcgregorio88ada9a2007-05-03 13:20:38 +0000177<a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</a>
jcgregorio26c0cd72006-07-03 17:36:17 +0000178<b class="navlabel">Next:</b>
jcgregorio88ada9a2007-05-03 13:20:38 +0000179<a class="sectref" rel="next" href="about.html">About this document ...</a>
jcgregorio26c0cd72006-07-03 17:36:17 +0000180</div>
181</div>
182<hr />
jcgregoriocde4f092007-03-08 21:59:04 +0000183<span class="release-info">Release 0.3, documentation updated on Mar 8, 2007.</span>
jcgregorio88ada9a2007-05-03 13:20:38 +0000184</div>
jcgregorio26c0cd72006-07-03 17:36:17 +0000185<!--End of Navigation Panel-->
186
jcgregorio88ada9a2007-05-03 13:20:38 +0000187</body>
188</html>