• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 comprehensive"
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 Objects</a>
42<b class="navlabel">Up:</b>
43<a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</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>
531.1.4 Examples
54</h2>
55
56<p>
57To do a simple <code>GET</code> request just supply the absolute URI
58of the resource:
59
60<p>
61<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
69<p>
70Here 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
75<p>
76<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
85<p>
86Here is an example that connects to a server that
87supports the Atom Publishing Protocol.
88
89<p>
90<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
109<p>
110Here 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
115<p>
116<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>
126
127<p>
128Here 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>
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 comprehensive"
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 Objects</a>
176<b class="navlabel">Up:</b>
177<a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</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.</span>
184</div>
185<!--End of Navigation Panel-->
186
187</body>
188</html>
189