blob: 0cc6254729613e29ff1a52fad4e3c12e591018ae [file] [log] [blame]
joe.gregorio258eefb2008-09-06 01:30:25 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
Joe Gregorio24c916a2011-06-13 15:13:11 -04002 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4<html xmlns="http://www.w3.org/1999/xhtml">
joe.gregorio258eefb2008-09-06 01:30:25 +00005 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Joe Gregorio24c916a2011-06-13 15:13:11 -04007
joe.gregorio258eefb2008-09-06 01:30:25 +00008 <title>httplib2 A comprehensive HTTP client library. &mdash; httplib2 v0.4 documentation</title>
9 <link rel="stylesheet" href="_static/default.css" type="text/css" />
10 <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
11 <script type="text/javascript">
12 var DOCUMENTATION_OPTIONS = {
Joe Gregorio24c916a2011-06-13 15:13:11 -040013 URL_ROOT: '#',
14 VERSION: '0.4',
15 COLLAPSE_MODINDEX: false,
16 FILE_SUFFIX: '.html',
17 HAS_SOURCE: true
joe.gregorio258eefb2008-09-06 01:30:25 +000018 };
19 </script>
20 <script type="text/javascript" src="_static/jquery.js"></script>
joe.gregorio258eefb2008-09-06 01:30:25 +000021 <script type="text/javascript" src="_static/doctools.js"></script>
joe.gregorio258eefb2008-09-06 01:30:25 +000022 <link rel="top" title="httplib2 v0.4 documentation" href="index.html" />
Joe Gregorio24c916a2011-06-13 15:13:11 -040023 <link rel="prev" title="The httplib2 Library" href="index.html" />
joe.gregorio258eefb2008-09-06 01:30:25 +000024 </head>
25 <body>
26 <div class="related">
27 <h3>Navigation</h3>
28 <ul>
29 <li class="right" style="margin-right: 10px">
30 <a href="genindex.html" title="General Index"
31 accesskey="I">index</a></li>
32 <li class="right" >
33 <a href="modindex.html" title="Global Module Index"
34 accesskey="M">modules</a> |</li>
35 <li class="right" >
36 <a href="index.html" title="The httplib2 Library"
37 accesskey="P">previous</a> |</li>
Joe Gregorio24c916a2011-06-13 15:13:11 -040038 <li><a href="index.html">httplib2 v0.4 documentation</a> &raquo;</li>
joe.gregorio258eefb2008-09-06 01:30:25 +000039 </ul>
Joe Gregorio24c916a2011-06-13 15:13:11 -040040 </div>
41
joe.gregorio258eefb2008-09-06 01:30:25 +000042 <div class="document">
43 <div class="documentwrapper">
44 <div class="bodywrapper">
45 <div class="body">
46
joe.gregorio258eefb2008-09-06 01:30:25 +000047 <div class="section" id="module-httplib2">
Joe Gregorio24c916a2011-06-13 15:13:11 -040048<h1><tt class="xref docutils literal"><span class="pre">httplib2</span></tt> A comprehensive HTTP client library.<a class="headerlink" href="#module-httplib2" title="Permalink to this headline"></a></h1>
joe.gregorio258eefb2008-09-06 01:30:25 +000049<p>The <tt class="xref docutils literal"><span class="pre">httplib2</span></tt> module is a comprehensive HTTP client library with the
50following features:</p>
51<dl class="describe">
52<dt>
53<tt class="descname">HTTP and HTTPS</tt></dt>
54<dd>HTTPS support is only available if the socket module was compiled with SSL
55support.</dd></dl>
56
57<dl class="describe">
58<dt>
59<tt class="descname">Keep-Alive</tt></dt>
60<dd>Supports HTTP 1.1 Keep-Alive, keeping the socket open and performing multiple
61requests over the same connection if possible.</dd></dl>
62
63<dl class="describe">
64<dt>
65<tt class="descname">Authentication</tt></dt>
66<dd><p>The following three types of HTTP Authentication are supported. These can be
67used over both HTTP and HTTPS.</p>
68<blockquote>
69<ul class="simple">
70<li>Digest</li>
71<li>Basic</li>
72<li>WSSE</li>
73</ul>
74</blockquote>
75</dd></dl>
76
77<dl class="describe">
78<dt>
79<tt class="descname">Caching</tt></dt>
80<dd>The module can optionally operate with a private cache that understands the
81Cache-Control: header and uses both the ETag and Last-Modified cache validators.</dd></dl>
82
83<dl class="describe">
84<dt>
85<tt class="descname">All Methods</tt></dt>
86<dd>The module can handle any HTTP request method, not just GET and POST.</dd></dl>
87
88<dl class="describe">
89<dt>
90<tt class="descname">Redirects</tt></dt>
91<dd>Automatically follows 3XX redirects on GETs.</dd></dl>
92
93<dl class="describe">
94<dt>
95<tt class="descname">Compression</tt></dt>
96<dd>Handles both <tt class="docutils literal"><span class="pre">deflate</span></tt> and <tt class="docutils literal"><span class="pre">gzip</span></tt> types of compression.</dd></dl>
97
98<dl class="describe">
99<dt>
100<tt class="descname">Lost update support</tt></dt>
101<dd>Automatically adds back ETags into PUT requests to resources we have already
102cached. This implements Section 3.2 of Detecting the Lost Update Problem Using
103Unreserved Checkout</dd></dl>
104
105<p>The <tt class="xref docutils literal"><span class="pre">httplib2</span></tt> module defines the following variables:</p>
106<dl class="data">
107<dt id="httplib2.debuglevel">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400108<tt class="descclassname">httplib2.</tt><tt class="descname">debuglevel</tt><a class="headerlink" href="#httplib2.debuglevel" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000109<dd>The amount of debugging information to print. The default is 0.</dd></dl>
110
111<p>The <tt class="xref docutils literal"><span class="pre">httplib2</span></tt> module may raise the following Exceptions. Note that there
112is an option that turns exceptions into normal responses with an HTTP status
113code indicating an error occured. See
114<a title="httplib2.Http.force_exception_to_status_code" class="reference internal" href="#httplib2.Http.force_exception_to_status_code"><tt class="xref docutils literal"><span class="pre">Http.force_exception_to_status_code</span></tt></a></p>
115<dl class="exception">
116<dt id="httplib2.HttpLib2Error">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400117<em class="property">exception </em><tt class="descclassname">httplib2.</tt><tt class="descname">HttpLib2Error</tt><a class="headerlink" href="#httplib2.HttpLib2Error" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000118<dd>The Base Exception for all exceptions raised by httplib2.</dd></dl>
119
120<dl class="exception">
121<dt id="httplib2.RedirectMissingLocation">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400122<em class="property">exception </em><tt class="descclassname">httplib2.</tt><tt class="descname">RedirectMissingLocation</tt><a class="headerlink" href="#httplib2.RedirectMissingLocation" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000123<dd>A 3xx redirect response code was provided but no Location: header was provided
124to point to the new location.</dd></dl>
125
126<dl class="exception">
127<dt id="httplib2.RedirectLimit">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400128<em class="property">exception </em><tt class="descclassname">httplib2.</tt><tt class="descname">RedirectLimit</tt><a class="headerlink" href="#httplib2.RedirectLimit" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000129<dd>The maximum number of redirections was reached without coming to a final URI.</dd></dl>
130
131<dl class="exception">
132<dt id="httplib2.ServerNotFoundError">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400133<em class="property">exception </em><tt class="descclassname">httplib2.</tt><tt class="descname">ServerNotFoundError</tt><a class="headerlink" href="#httplib2.ServerNotFoundError" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000134<dd>Unable to resolve the host name given.</dd></dl>
135
136<dl class="exception">
137<dt id="httplib2.RelativeURIError">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400138<em class="property">exception </em><tt class="descclassname">httplib2.</tt><tt class="descname">RelativeURIError</tt><a class="headerlink" href="#httplib2.RelativeURIError" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000139<dd>A relative, as opposed to an absolute URI, was passed into request().</dd></dl>
140
141<dl class="exception">
142<dt id="httplib2.FailedToDecompressContent">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400143<em class="property">exception </em><tt class="descclassname">httplib2.</tt><tt class="descname">FailedToDecompressContent</tt><a class="headerlink" href="#httplib2.FailedToDecompressContent" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000144<dd>The headers claimed that the content of the response was compressed but the
145decompression algorithm applied to the content failed.</dd></dl>
146
147<dl class="exception">
148<dt id="httplib2.UnimplementedDigestAuthOptionError">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400149<em class="property">exception </em><tt class="descclassname">httplib2.</tt><tt class="descname">UnimplementedDigestAuthOptionError</tt><a class="headerlink" href="#httplib2.UnimplementedDigestAuthOptionError" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000150<dd>The server requested a type of Digest authentication that we are unfamiliar
151with.</dd></dl>
152
153<dl class="exception">
154<dt id="httplib2.UnimplementedHmacDigestAuthOptionError">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400155<em class="property">exception </em><tt class="descclassname">httplib2.</tt><tt class="descname">UnimplementedHmacDigestAuthOptionError</tt><a class="headerlink" href="#httplib2.UnimplementedHmacDigestAuthOptionError" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000156<dd>The server requested a type of HMACDigest authentication that we are unfamiliar
157with.</dd></dl>
158
159<dl class="class">
160<dt id="httplib2.Http">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400161<em class="property">class </em><tt class="descclassname">httplib2.</tt><tt class="descname">Http</tt><big>(</big><span class="optional">[</span><em>cache=None</em><span class="optional">]</span><span class="optional">[</span>, <em>timeout=None</em><span class="optional">]</span><span class="optional">[</span>, <em>proxy_info=None</em><span class="optional">]</span><span class="optional">[</span>, <em>ca_certs=None</em><span class="optional">]</span><span class="optional">[</span>, <em>disable_ssl_certificate_validation=False</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#httplib2.Http" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000162<dd>The class that represents a client HTTP interface. The <em>cache</em> parameter is
163either the name of a directory to be used as a flat file cache, or it must an
164object that implements the required caching interface. The <em>timeout</em> parameter
Joe Gregorio24c916a2011-06-13 15:13:11 -0400165is the socket level timeout. The <em>ca_certs</em> parameter is the filename of the
166CA certificates to use. If none is given a default set is used. The
167<em>disable_ssl_certificate_validation</em> boolean flag determines if ssl certificate validation
168is done. The <em>proxy_info</em> parameter is an object of type :class:ProxyInfo.</dd></dl>
169
170<dl class="class">
171<dt id="httplib2.ProxyInfo">
172<em class="property">class </em><tt class="descclassname">httplib2.</tt><tt class="descname">ProxyInfo</tt><big>(</big><em>proxy_type</em>, <em>proxy_host</em>, <em>proxy_port</em><span class="optional">[</span>, <em>proxy_rdns=None</em><span class="optional">]</span><span class="optional">[</span>, <em>proxy_user=None</em><span class="optional">]</span><span class="optional">[</span>, <em>proxy_pass=None</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#httplib2.ProxyInfo" title="Permalink to this definition"></a></dt>
173<dd><p>Collect information required to use a proxy.
174The parameter proxy_type must be set to one of socks.PROXY_TYPE_XXX
175constants. For example:</p>
176<p>p = ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, proxy_host=&#8217;localhost&#8217;, proxy_port=8000)</p>
177</dd></dl>
joe.gregorio258eefb2008-09-06 01:30:25 +0000178
179<dl class="class">
180<dt id="httplib2.Response">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400181<em class="property">class </em><tt class="descclassname">httplib2.</tt><tt class="descname">Response</tt><big>(</big><em>info</em><big>)</big><a class="headerlink" href="#httplib2.Response" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000182<dd>Response is a subclass of <tt class="xref docutils literal"><span class="pre">dict</span></tt> and instances of this class are
183returned from calls to Http.request. The <em>info</em> parameter is either an
184<tt class="xref docutils literal"><span class="pre">rfc822.Message</span></tt> or an <tt class="xref docutils literal"><span class="pre">httplib.HTTPResponse</span></tt> object.</dd></dl>
185
186<dl class="class">
187<dt id="httplib2.FileCache">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400188<em class="property">class </em><tt class="descclassname">httplib2.</tt><tt class="descname">FileCache</tt><big>(</big><em>dir_name</em><span class="optional">[</span>, <em>safe=safename</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#httplib2.FileCache" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000189<dd>FileCache implements a Cache as a directory of files. The <em>dir_name</em> parameter
190is the name of the directory to use. If the directory does not exist then
191FileCache attempts to create the directory. The optional <em>safe</em> parameter is a
192funtion which generates the cache filename for each URI. A FileCache object is
193constructed and used for caching when you pass a directory name into the
194constructor of <a title="httplib2.Http" class="reference internal" href="#httplib2.Http"><tt class="xref docutils literal"><span class="pre">Http</span></tt></a>.</dd></dl>
195
196<p>Http objects have the following methods:</p>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400197<div class="section" id="http-objects">
198<span id="id1"></span><h2>Http Objects<a class="headerlink" href="#http-objects" title="Permalink to this headline"></a></h2>
joe.gregorio258eefb2008-09-06 01:30:25 +0000199<dl class="method">
200<dt id="httplib2.Http.request">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400201<tt class="descclassname">Http.</tt><tt class="descname">request</tt><big>(</big><em>uri</em><span class="optional">[</span>, <em>method=&quot;GET&quot;</em>, <em>body=None</em>, <em>headers=None</em>, <em>redirections=DEFAULT_MAX_REDIRECTS</em>, <em>connection_type=None</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#httplib2.Http.request" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000202<dd><p>Performs a single HTTP request. The <em>uri</em> is the URI of the HTTP resource and
203can begin with either <tt class="docutils literal"><span class="pre">http</span></tt> or <tt class="docutils literal"><span class="pre">https</span></tt>. The value of <em>uri</em> must be an
204absolute URI.</p>
205<p>The <em>method</em> is the HTTP method to perform, such as <tt class="docutils literal"><span class="pre">GET</span></tt>, <tt class="docutils literal"><span class="pre">POST</span></tt>,
206<tt class="docutils literal"><span class="pre">DELETE</span></tt>, etc. There is no restriction on the methods allowed.</p>
207<p>The <em>body</em> is the entity body to be sent with the request. It is a string
208object.</p>
209<p>Any extra headers that are to be sent with the request should be provided in the
210<em>headers</em> dictionary.</p>
211<p>The maximum number of redirect to follow before raising an exception is
212<em>redirections</em>. The default is 5.</p>
213<p>The <em>connection_type</em> is the type of connection object to use. The supplied
214class should implement the interface of httplib.HTTPConnection.</p>
215<p>The return value is a tuple of (response, content), the first being and instance
216of the <a title="httplib2.Response" class="reference internal" href="#httplib2.Response"><tt class="xref docutils literal"><span class="pre">Response</span></tt></a> class, the second being a string that contains the
217response entity body.</p>
218</dd></dl>
219
220<dl class="method">
221<dt id="httplib2.Http.add_credentials">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400222<tt class="descclassname">Http.</tt><tt class="descname">add_credentials</tt><big>(</big><em>name</em>, <em>password</em><span class="optional">[</span>, <em>domain=None</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#httplib2.Http.add_credentials" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000223<dd>Adds a name and password that will be used when a request requires
224authentication. Supplying the optional <em>domain</em> name will restrict these
225credentials to only be sent to the specified domain. If <em>domain</em> is not
226specified then the given credentials will be used to try to satisfy every HTTP
227401 challenge.</dd></dl>
228
229<dl class="method">
230<dt id="httplib2.Http.add_certificate">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400231<tt class="descclassname">Http.</tt><tt class="descname">add_certificate</tt><big>(</big><em>key</em>, <em>cert</em>, <em>domain</em><big>)</big><a class="headerlink" href="#httplib2.Http.add_certificate" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000232<dd>Add a <em>key</em> and <em>cert</em> that will be used for an SSL connection to the specified
233domain. <em>keyfile</em> is the name of a PEM formatted file that contains your
234private key. <em>certfile</em> is a PEM formatted certificate chain file.</dd></dl>
235
236<dl class="method">
237<dt id="httplib2.Http.clear_credentials">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400238<tt class="descclassname">Http.</tt><tt class="descname">clear_credentials</tt><big>(</big><big>)</big><a class="headerlink" href="#httplib2.Http.clear_credentials" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000239<dd>Remove all the names and passwords used for authentication.</dd></dl>
240
241<dl class="attribute">
242<dt id="httplib2.Http.follow_redirects">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400243<tt class="descclassname">Http.</tt><tt class="descname">follow_redirects</tt><a class="headerlink" href="#httplib2.Http.follow_redirects" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000244<dd>If <tt class="xref docutils literal"><span class="pre">True</span></tt>, which is the default, safe redirects are followed, where safe means
245that the client is only doing a <tt class="docutils literal"><span class="pre">GET</span></tt> or <tt class="docutils literal"><span class="pre">HEAD</span></tt> on the URI to which it is
246being redirected. If <tt class="xref docutils literal"><span class="pre">False</span></tt> then no redirects are followed. Note that a False
247&#8216;follow_redirects&#8217; takes precedence over a True &#8216;follow_all_redirects&#8217;. Another
248way of saying that is for &#8216;follow_all_redirects&#8217; to have any affect,
249&#8216;follow_redirects&#8217; must be True.</dd></dl>
250
251<dl class="attribute">
252<dt id="httplib2.Http.follow_all_redirects">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400253<tt class="descclassname">Http.</tt><tt class="descname">follow_all_redirects</tt><a class="headerlink" href="#httplib2.Http.follow_all_redirects" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000254<dd>If <tt class="xref docutils literal"><span class="pre">False</span></tt>, which is the default, only safe redirects are followed, where safe
255means that the client is only doing a <tt class="docutils literal"><span class="pre">GET</span></tt> or <tt class="docutils literal"><span class="pre">HEAD</span></tt> on the URI to which it
256is being redirected. If <tt class="xref docutils literal"><span class="pre">True</span></tt> then all redirects are followed. Note that a
257False &#8216;follow_redirects&#8217; takes precedence over a True &#8216;follow_all_redirects&#8217;.
258Another way of saying that is for &#8216;follow_all_redirects&#8217; to have any affect,
259&#8216;follow_redirects&#8217; must be True.</dd></dl>
260
261<dl class="attribute">
262<dt id="httplib2.Http.force_exception_to_status_code">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400263<tt class="descclassname">Http.</tt><tt class="descname">force_exception_to_status_code</tt><a class="headerlink" href="#httplib2.Http.force_exception_to_status_code" title="Permalink to this definition"></a></dt>
joe.gregorio18b269d2008-09-06 15:03:26 +0000264<dd><p>If <tt class="xref docutils literal"><span class="pre">True</span></tt> then no <tt class="xref docutils literal"><span class="pre">httplib2</span></tt> exceptions will be
joe.gregorio258eefb2008-09-06 01:30:25 +0000265thrown. Instead, those error conditions will be turned into <a title="httplib2.Response" class="reference internal" href="#httplib2.Response"><tt class="xref docutils literal"><span class="pre">Response</span></tt></a>
266objects that will be returned normally.</p>
joe.gregorio18b269d2008-09-06 15:03:26 +0000267<p>If <tt class="xref docutils literal"><span class="pre">False</span></tt>, which is the default, then exceptions will be thrown.</p>
joe.gregorio258eefb2008-09-06 01:30:25 +0000268</dd></dl>
269
270<dl class="attribute">
joe.gregorio700f04d2008-09-06 04:46:32 +0000271<dt id="httplib2.Http.optimistic_concurrency_methods">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400272<tt class="descclassname">Http.</tt><tt class="descname">optimistic_concurrency_methods</tt><a class="headerlink" href="#httplib2.Http.optimistic_concurrency_methods" title="Permalink to this definition"></a></dt>
joe.gregorio700f04d2008-09-06 04:46:32 +0000273<dd>By default a list that only contains &#8220;PUT&#8221;, this attribute
274controls which methods will get &#8216;if-match&#8217; headers attached
275to them from cached responses with etags. You can append
276new items to this list to add new methods that should
277get this support, such as &#8220;PATCH&#8221;.</dd></dl>
278
279<dl class="attribute">
joe.gregorio258eefb2008-09-06 01:30:25 +0000280<dt id="httplib2.Http.ignore_etag">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400281<tt class="descclassname">Http.</tt><tt class="descname">ignore_etag</tt><a class="headerlink" href="#httplib2.Http.ignore_etag" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000282<dd>Defaults to <tt class="xref docutils literal"><span class="pre">False</span></tt>. If <tt class="xref docutils literal"><span class="pre">True</span></tt>, then any etags present in the cached
283response are ignored when processing the current request, i.e. httplib2 does
284<strong>not</strong> use &#8216;if-match&#8217; for PUT or &#8216;if-none-match&#8217; when GET or HEAD requests are
285made. This is mainly to deal with broken servers which supply an etag, but
286change it capriciously.</dd></dl>
287
288<p>If you wish to supply your own caching implementation then you will need to pass
289in an object that supports the following methods. Note that the <tt class="xref docutils literal"><span class="pre">memcache</span></tt>
290module supports this interface natively.</p>
291</div>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400292<div class="section" id="cache-objects">
293<span id="id2"></span><h2>Cache Objects<a class="headerlink" href="#cache-objects" title="Permalink to this headline"></a></h2>
joe.gregorio258eefb2008-09-06 01:30:25 +0000294<dl class="method">
295<dt id="httplib2.Cache.get">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400296<tt class="descclassname">Cache.</tt><tt class="descname">get</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#httplib2.Cache.get" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000297<dd>Takes a string <em>key</em> and returns the value as a string.</dd></dl>
298
299<dl class="method">
300<dt id="httplib2.Cache.set">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400301<tt class="descclassname">Cache.</tt><tt class="descname">set</tt><big>(</big><em>key</em>, <em>value</em><big>)</big><a class="headerlink" href="#httplib2.Cache.set" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000302<dd>Takes a string <em>key</em> and <em>value</em> and stores it in the cache.</dd></dl>
303
304<dl class="method">
305<dt id="httplib2.Cache.delete">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400306<tt class="descclassname">Cache.</tt><tt class="descname">delete</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#httplib2.Cache.delete" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000307<dd>Deletes the cached value stored at <em>key</em>. The value of <em>key</em> is a string.</dd></dl>
308
309<p>Response objects are derived from <tt class="xref docutils literal"><span class="pre">dict</span></tt> and map header names (lower case
310with the trailing colon removed) to header values. In addition to the dict
311methods a Response object also has:</p>
312</div>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400313<div class="section" id="response-objects">
314<span id="id3"></span><h2>Response Objects<a class="headerlink" href="#response-objects" title="Permalink to this headline"></a></h2>
joe.gregorio258eefb2008-09-06 01:30:25 +0000315<dl class="attribute">
316<dt id="httplib2.Response.fromcache">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400317<tt class="descclassname">Response.</tt><tt class="descname">fromcache</tt><a class="headerlink" href="#httplib2.Response.fromcache" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000318<dd>If <tt class="docutils literal"><span class="pre">true</span></tt> the the response was returned from the cache.</dd></dl>
319
320<dl class="attribute">
321<dt id="httplib2.Response.version">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400322<tt class="descclassname">Response.</tt><tt class="descname">version</tt><a class="headerlink" href="#httplib2.Response.version" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000323<dd>The version of HTTP that the server supports. A value of 11 means &#8216;1.1&#8217;.</dd></dl>
324
325<dl class="attribute">
326<dt id="httplib2.Response.status">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400327<tt class="descclassname">Response.</tt><tt class="descname">status</tt><a class="headerlink" href="#httplib2.Response.status" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000328<dd>The numerical HTTP status code returned in the response.</dd></dl>
329
330<dl class="attribute">
331<dt id="httplib2.Response.reason">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400332<tt class="descclassname">Response.</tt><tt class="descname">reason</tt><a class="headerlink" href="#httplib2.Response.reason" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000333<dd>The human readable component of the HTTP response status code.</dd></dl>
334
335<dl class="attribute">
336<dt id="httplib2.Response.previous">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400337<tt class="descclassname">Response.</tt><tt class="descname">previous</tt><a class="headerlink" href="#httplib2.Response.previous" title="Permalink to this definition"></a></dt>
joe.gregorio258eefb2008-09-06 01:30:25 +0000338<dd>If redirects are followed then the <a title="httplib2.Response" class="reference internal" href="#httplib2.Response"><tt class="xref docutils literal"><span class="pre">Response</span></tt></a> object returned is just for
339the very last HTTP request and <em>previous</em> points to the previous
340<a title="httplib2.Response" class="reference internal" href="#httplib2.Response"><tt class="xref docutils literal"><span class="pre">Response</span></tt></a> object. In this manner they form a chain going back through
341the responses to the very first response. Will be <tt class="xref docutils literal"><span class="pre">None</span></tt> if there are no
342previous respones.</dd></dl>
343
344<p>The Response object also populates the header <tt class="docutils literal"><span class="pre">content-location</span></tt>, that
345contains the URI that was ultimately requested. This is useful if redirects were
346encountered, you can determine the ultimate URI that the request was sent to.
347All Response objects contain this key value, including <tt class="docutils literal"><span class="pre">previous</span></tt> responses so
348you can determine the entire chain of redirects. If
349<a title="httplib2.Http.force_exception_to_status_code" class="reference internal" href="#httplib2.Http.force_exception_to_status_code"><tt class="xref docutils literal"><span class="pre">Http.force_exception_to_status_code</span></tt></a> is <tt class="xref docutils literal"><span class="pre">True</span></tt> and the number of
350redirects has exceeded the number of allowed number of redirects then the
351<a title="httplib2.Response" class="reference internal" href="#httplib2.Response"><tt class="xref docutils literal"><span class="pre">Response</span></tt></a> object will report the error in the status code, but the
352complete chain of previous responses will still be in tact.</p>
353<p>To do a simple <tt class="docutils literal"><span class="pre">GET</span></tt> request just supply the absolute URI of the resource:</p>
354</div>
355<div class="section" id="examples">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400356<span id="httplib2-example"></span><h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline"></a></h2>
357<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">httplib2</span>
joe.gregorio258eefb2008-09-06 01:30:25 +0000358<span class="n">h</span> <span class="o">=</span> <span class="n">httplib2</span><span class="o">.</span><span class="n">Http</span><span class="p">()</span>
359<span class="n">resp</span><span class="p">,</span> <span class="n">content</span> <span class="o">=</span> <span class="n">h</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&quot;http://bitworking.org/&quot;</span><span class="p">)</span>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400360<span class="k">assert</span> <span class="n">resp</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">200</span>
joe.gregorio258eefb2008-09-06 01:30:25 +0000361<span class="k">assert</span> <span class="n">resp</span><span class="p">[</span><span class="s">&#39;content-type&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;text/html&#39;</span>
362</pre></div>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400363</div>
joe.gregorio258eefb2008-09-06 01:30:25 +0000364<p>Here is more complex example that does a PUT of some text to a resource that
365requires authentication. The Http instance also uses a file cache in the
366directory <tt class="docutils literal"><span class="pre">.cache</span></tt>.</p>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400367<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">httplib2</span>
joe.gregorio258eefb2008-09-06 01:30:25 +0000368<span class="n">h</span> <span class="o">=</span> <span class="n">httplib2</span><span class="o">.</span><span class="n">Http</span><span class="p">(</span><span class="s">&quot;.cache&quot;</span><span class="p">)</span>
369<span class="n">h</span><span class="o">.</span><span class="n">add_credentials</span><span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="s">&#39;password&#39;</span><span class="p">)</span>
370<span class="n">resp</span><span class="p">,</span> <span class="n">content</span> <span class="o">=</span> <span class="n">h</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&quot;https://example.org/chap/2&quot;</span><span class="p">,</span>
371 <span class="s">&quot;PUT&quot;</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="s">&quot;This is text&quot;</span><span class="p">,</span>
372 <span class="n">headers</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;content-type&#39;</span><span class="p">:</span><span class="s">&#39;text/plain&#39;</span><span class="p">}</span> <span class="p">)</span>
373</pre></div>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400374</div>
joe.gregorio258eefb2008-09-06 01:30:25 +0000375<p>Here is an example that connects to a server that supports the Atom Publishing
376Protocol.</p>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400377<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">httplib2</span>
joe.gregorio258eefb2008-09-06 01:30:25 +0000378<span class="n">h</span> <span class="o">=</span> <span class="n">httplib2</span><span class="o">.</span><span class="n">Http</span><span class="p">()</span>
379<span class="n">h</span><span class="o">.</span><span class="n">add_credentials</span><span class="p">(</span><span class="n">myname</span><span class="p">,</span> <span class="n">mypasswd</span><span class="p">)</span>
380<span class="n">h</span><span class="o">.</span><span class="n">follow_all_redirects</span> <span class="o">=</span> <span class="bp">True</span>
381<span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;Content-Type&#39;</span><span class="p">:</span> <span class="s">&#39;application/atom+xml&#39;</span><span class="p">}</span>
382<span class="n">body</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot; ?&gt;</span>
383<span class="s"> &lt;entry xmlns=&quot;http://www.w3.org/2005/Atom&quot;&gt;</span>
384<span class="s"> &lt;title&gt;Atom-Powered Robots Run Amok&lt;/title&gt;</span>
385<span class="s"> &lt;id&gt;urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a&lt;/id&gt;</span>
386<span class="s"> &lt;updated&gt;2003-12-13T18:30:02Z&lt;/updated&gt;</span>
387<span class="s"> &lt;author&gt;&lt;name&gt;John Doe&lt;/name&gt;&lt;/author&gt;</span>
388<span class="s"> &lt;content&gt;Some text.&lt;/content&gt;</span>
389<span class="s">&lt;/entry&gt;</span>
390<span class="s">&quot;&quot;&quot;</span>
391<span class="n">uri</span> <span class="o">=</span> <span class="s">&quot;http://www.example.com/collection/&quot;</span>
392<span class="n">resp</span><span class="p">,</span> <span class="n">content</span> <span class="o">=</span> <span class="n">h</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="n">uri</span><span class="p">,</span> <span class="s">&quot;POST&quot;</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="n">body</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
393</pre></div>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400394</div>
joe.gregorio258eefb2008-09-06 01:30:25 +0000395<p>Here is an example of providing data to an HTML form processor. In this case we
396presume this is a POST form. We need to take our data and format it as
397&#8220;application/x-www-form-urlencoded&#8221; data and use that as a body for a POST
398request.</p>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400399<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">httplib2</span>
joe.gregorio258eefb2008-09-06 01:30:25 +0000400<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urllib</span>
401<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;fred&#39;</span><span class="p">,</span> <span class="s">&#39;address&#39;</span><span class="p">:</span> <span class="s">&#39;123 shady lane&#39;</span><span class="p">}</span>
402<span class="gp">&gt;&gt;&gt; </span><span class="n">body</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
403<span class="gp">&gt;&gt;&gt; </span><span class="n">body</span>
404<span class="go">&#39;name=fred&amp;address=123+shady+lane&#39;</span>
405<span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">httplib2</span><span class="o">.</span><span class="n">Http</span><span class="p">()</span>
406<span class="gp">&gt;&gt;&gt; </span><span class="n">resp</span><span class="p">,</span> <span class="n">content</span> <span class="o">=</span> <span class="n">h</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&quot;http://example.com&quot;</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s">&quot;POST&quot;</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="n">body</span><span class="p">)</span>
407</pre></div>
408</div>
409</div>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400410</div>
joe.gregorio258eefb2008-09-06 01:30:25 +0000411
412
413 </div>
414 </div>
415 </div>
416 <div class="sphinxsidebar">
417 <div class="sphinxsidebarwrapper">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400418 <h3><a href="index.html">Table Of Contents</a></h3>
joe.gregorio258eefb2008-09-06 01:30:25 +0000419 <ul>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400420<li><a class="reference external" href="#"><tt class="docutils literal"><span class="pre">httplib2</span></tt> A comprehensive HTTP client library.</a><ul>
421<li><a class="reference external" href="#http-objects">Http Objects</a></li>
422<li><a class="reference external" href="#cache-objects">Cache Objects</a></li>
423<li><a class="reference external" href="#response-objects">Response Objects</a></li>
joe.gregorio258eefb2008-09-06 01:30:25 +0000424<li><a class="reference external" href="#examples">Examples</a></li>
425</ul>
426</li>
427</ul>
428
429 <h4>Previous topic</h4>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400430 <p class="topless"><a href="index.html"
431 title="previous chapter">The httplib2 Library</a></p>
joe.gregorio258eefb2008-09-06 01:30:25 +0000432 <h3>This Page</h3>
433 <ul class="this-page-menu">
Joe Gregorio24c916a2011-06-13 15:13:11 -0400434 <li><a href="_sources/libhttplib2.txt"
435 rel="nofollow">Show Source</a></li>
joe.gregorio258eefb2008-09-06 01:30:25 +0000436 </ul>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400437 <div id="searchbox" style="display: none">
joe.gregorio258eefb2008-09-06 01:30:25 +0000438 <h3>Quick search</h3>
Joe Gregorio24c916a2011-06-13 15:13:11 -0400439 <form class="search" action="search.html" method="get">
440 <input type="text" name="q" size="18" />
441 <input type="submit" value="Go" />
442 <input type="hidden" name="check_keywords" value="yes" />
443 <input type="hidden" name="area" value="default" />
444 </form>
445 <p class="searchtip" style="font-size: 90%">
446 Enter search terms or a module, class or function name.
447 </p>
448 </div>
449 <script type="text/javascript">$('#searchbox').show(0);</script>
joe.gregorio258eefb2008-09-06 01:30:25 +0000450 </div>
451 </div>
452 <div class="clearer"></div>
453 </div>
454 <div class="related">
455 <h3>Navigation</h3>
456 <ul>
457 <li class="right" style="margin-right: 10px">
458 <a href="genindex.html" title="General Index"
Joe Gregorio24c916a2011-06-13 15:13:11 -0400459 >index</a></li>
joe.gregorio258eefb2008-09-06 01:30:25 +0000460 <li class="right" >
461 <a href="modindex.html" title="Global Module Index"
Joe Gregorio24c916a2011-06-13 15:13:11 -0400462 >modules</a> |</li>
joe.gregorio258eefb2008-09-06 01:30:25 +0000463 <li class="right" >
464 <a href="index.html" title="The httplib2 Library"
Joe Gregorio24c916a2011-06-13 15:13:11 -0400465 >previous</a> |</li>
466 <li><a href="index.html">httplib2 v0.4 documentation</a> &raquo;</li>
joe.gregorio258eefb2008-09-06 01:30:25 +0000467 </ul>
468 </div>
469 <div class="footer">
470 &copy; Copyright 2008, Joe Gregorio.
Joe Gregorio24c916a2011-06-13 15:13:11 -0400471 Last updated on Jun 13, 2011.
472 Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
joe.gregorio258eefb2008-09-06 01:30:25 +0000473 </div>
474 </body>
475</html>