1.. % Template for a library manual section. 2.. % PLEASE REMOVE THE COMMENTS AFTER USING THE TEMPLATE 3.. % 4.. % Complete documentation on the extended LaTeX markup used for Python 5.. % documentation is available in ``Documenting Python'', which is part 6.. % of the standard documentation for Python. It may be found online 7.. % at: 8.. % 9.. % http://www.python.org/doc/current/doc/doc.html 10.. % ==== 0. ==== 11.. % Copy this file to <mydir>/lib<mymodule>.tex, and edit that file 12.. % according to the instructions below. 13 14.. % ==== 1. ==== 15.. % The section prologue. Give the section a title and provide some 16.. % meta-information. References to the module should use 17.. % \refbimodindex, \refstmodindex, \refexmodindex or \refmodindex, as 18.. % appropriate. 19 20 21:mod:`httplib2` A comprehensive HTTP client library. 22===================================================== 23 24.. module:: httplib2 25.. moduleauthor:: Joe Gregorio <joe@bitworking.org> 26.. sectionauthor:: Joe Gregorio <joe@bitworking.org> 27 28 29.. % Choose one of these to specify the module module name. If there's 30.. % an underscore in the name, use 31.. % \declaremodule[modname]{...}{mod_name} instead. 32.. % 33.. % not standard, in Python 34.. % Portability statement: Uncomment and fill in the parameter to specify the 35.. % availability of the module. The parameter can be Unix, IRIX, SunOS, Mac, 36.. % Windows, or lots of other stuff. When ``Mac'' is specified, the availability 37.. % statement will say ``Macintosh'' and the Module Index may say ``Mac''. 38.. % Please use a name that has already been used whenever applicable. If this 39.. % is omitted, no availability statement is produced or implied. 40.. % 41.. % \platform{Unix} 42.. % These apply to all modules, and may be given more than once: 43.. % Author of the module code; 44.. % omit if not known. 45.. % Author of the documentation, 46.. % even if not a module section. 47 48 49 50.. % Leave at least one blank line after this, to simplify ad-hoc tools 51.. % that are sometimes used to massage these files. 52 53The :mod:`httplib2` module is a comprehensive HTTP client library with the 54following features: 55 56.. % ==== 2. ==== 57.. % Give a short overview of what the module does. 58.. % If it is platform specific, mention this. 59.. % Mention other important restrictions or general operating principles. 60.. % For example: 61 62.. describe:: HTTP and HTTPS 63 64 HTTPS support is only available if the socket module was compiled with SSL 65 support. 66 67.. describe:: Keep-Alive 68 69 Supports HTTP 1.1 Keep-Alive, keeping the socket open and performing multiple 70 requests over the same connection if possible. 71 72.. describe:: Authentication 73 74 The following three types of HTTP Authentication are supported. These can be 75 used over both HTTP and HTTPS. 76 77 * Digest 78 * Basic 79 * WSSE 80 81.. describe:: Caching 82 83 The module can optionally operate with a private cache that understands the 84 Cache-Control: header and uses both the ETag and Last-Modified cache validators. 85 86.. describe:: All Methods 87 88 The module can handle any HTTP request method, not just GET and POST. 89 90.. describe:: Redirects 91 92 Automatically follows 3XX redirects on GETs. 93 94.. describe:: Compression 95 96 Handles both ``deflate`` and ``gzip`` types of compression. 97 98.. describe:: Lost update support 99 100 Automatically adds back ETags into PUT requests to resources we have already 101 cached. This implements Section 3.2 of Detecting the Lost Update Problem Using 102 Unreserved Checkout 103 104The :mod:`httplib2` module defines the following variables: 105 106.. % ==== 3. ==== 107.. % List the public functions defined by the module. Begin with a 108.. % standard phrase. You may also list the exceptions and other data 109.. % items defined in the module, insofar as they are important for the 110.. % user. 111.. % ---- 3.2. ---- 112.. % Data items are described using a ``datadesc'' block. This has only 113.. % one parameter: the item's name. 114 115 116.. data:: debuglevel 117 118 The amount of debugging information to print. The default is 0. 119 120 121.. data:: RETRIES 122 123 A request will be tried 'RETRIES' times if it fails at the socket/connection level. 124 The default is 2. 125 126The :mod:`httplib2` module may raise the following Exceptions. Note that there 127is an option that turns exceptions into normal responses with an HTTP status 128code indicating an error occured. See 129:attr:`Http.force_exception_to_status_code` 130 131.. % --- 3.3. --- 132.. % Exceptions are described using a ``excdesc'' block. This has only 133.. % one parameter: the exception name. Exceptions defined as classes in 134.. % the source code should be documented using this environment, but 135.. % constructor parameters must be omitted. 136 137 138.. exception:: HttpLib2Error 139 140 The Base Exception for all exceptions raised by httplib2. 141 142 143.. exception:: RedirectMissingLocation 144 145 A 3xx redirect response code was provided but no Location: header was provided 146 to point to the new location. 147 148 149.. exception:: RedirectLimit 150 151 The maximum number of redirections was reached without coming to a final URI. 152 153 154.. exception:: ServerNotFoundError 155 156 Unable to resolve the host name given. 157 158 159.. exception:: RelativeURIError 160 161 A relative, as opposed to an absolute URI, was passed into request(). 162 163 164.. exception:: FailedToDecompressContent 165 166 The headers claimed that the content of the response was compressed but the 167 decompression algorithm applied to the content failed. 168 169 170.. exception:: UnimplementedDigestAuthOptionError 171 172 The server requested a type of Digest authentication that we are unfamiliar 173 with. 174 175 176.. exception:: UnimplementedHmacDigestAuthOptionError 177 178 The server requested a type of HMACDigest authentication that we are unfamiliar 179 with. 180 181.. % ---- 3.4. ---- 182.. % Other standard environments: 183.. % 184.. % classdesc - Python classes; same arguments are funcdesc 185.. % methoddesc - methods, like funcdesc but has an optional parameter 186.. % to give the type name: \begin{methoddesc}[mytype]{name}{args} 187.. % By default, the type name will be the name of the 188.. % last class defined using classdesc. The type name 189.. % is required if the type is implemented in C (because 190.. % there's no classdesc) or if the class isn't directly 191.. % documented (if it's private). 192.. % memberdesc - data members, like datadesc, but with an optional 193.. % type name like methoddesc. 194 195 196.. class:: Http([cache=None], [timeout=None], [proxy_info==ProxyInfo.from_environment], [ca_certs=None], [disable_ssl_certificate_validation=False]) 197 198 The class that represents a client HTTP interface. The *cache* parameter is 199 either the name of a directory to be used as a flat file cache, or it must an 200 object that implements the required caching interface. The *timeout* parameter 201 is the socket level timeout. The *ca_certs* parameter is the filename of the 202 CA certificates to use. If none is given a default set is used. The 203 *disable_ssl_certificate_validation* boolean flag determines if ssl certificate validation 204 is done. The *proxy_info* parameter is an object of type :class:ProxyInfo. 205 206 207.. class:: ProxyInfo(proxy_type, proxy_host, proxy_port, [proxy_rdns=None], [proxy_user=None], [proxy_pass=None]) 208 209 Collect information required to use a proxy. 210 The parameter proxy_type must be set to one of socks.PROXY_TYPE_XXX 211 constants. For example: :: 212 213 p = ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, proxy_host='localhost', proxy_port=8000) 214 215.. class:: Response(info) 216 217 Response is a subclass of :class:`dict` and instances of this class are 218 returned from calls to Http.request. The *info* parameter is either an 219 :class:`rfc822.Message` or an :class:`httplib.HTTPResponse` object. 220 221 222.. class:: FileCache(dir_name, [safe=safename]) 223 224 FileCache implements a Cache as a directory of files. The *dir_name* parameter 225 is the name of the directory to use. If the directory does not exist then 226 FileCache attempts to create the directory. The optional *safe* parameter is a 227 funtion which generates the cache filename for each URI. A FileCache object is 228 constructed and used for caching when you pass a directory name into the 229 constructor of :class:`Http`. 230 231Http objects have the following methods: 232 233.. % If your module defines new object types (for a built-in module) or 234.. % classes (for a module written in Python), you should list the 235.. % methods and instance variables (if any) of each type or class in a 236.. % separate subsection. 237 238.. _http-objects: 239 240Http Objects 241--------------- 242 243.. method:: Http.request(uri, [method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS, connection_type=None]) 244 245 Performs a single HTTP request. The *uri* is the URI of the HTTP resource and 246 can begin with either ``http`` or ``https``. The value of *uri* must be an 247 absolute URI. 248 249 The *method* is the HTTP method to perform, such as ``GET``, ``POST``, 250 ``DELETE``, etc. There is no restriction on the methods allowed. 251 252 The *body* is the entity body to be sent with the request. It is a string 253 object. 254 255 Any extra headers that are to be sent with the request should be provided in the 256 *headers* dictionary. 257 258 The maximum number of redirect to follow before raising an exception is 259 *redirections*. The default is 5. 260 261 The *connection_type* is the type of connection object to use. The supplied 262 class should implement the interface of httplib.HTTPConnection. 263 264 The return value is a tuple of (response, content), the first being and instance 265 of the :class:`Response` class, the second being a string that contains the 266 response entity body. 267 268 269.. method:: Http.add_credentials(name, password, [domain=None]) 270 271 Adds a name and password that will be used when a request requires 272 authentication. Supplying the optional *domain* name will restrict these 273 credentials to only be sent to the specified domain. If *domain* is not 274 specified then the given credentials will be used to try to satisfy every HTTP 275 401 challenge. 276 277 278.. method:: Http.add_certificate(key, cert, domain) 279 280 Add a *key* and *cert* that will be used for an SSL connection to the specified 281 domain. *keyfile* is the name of a PEM formatted file that contains your 282 private key. *certfile* is a PEM formatted certificate chain file. 283 284 285.. method:: Http.clear_credentials() 286 287 Remove all the names and passwords used for authentication. 288 289 290.. attribute:: Http.follow_redirects 291 292 If ``True``, which is the default, safe redirects are followed, where safe means 293 that the client is only doing a ``GET`` or ``HEAD`` on the URI to which it is 294 being redirected. If ``False`` then no redirects are followed. Note that a False 295 'follow_redirects' takes precedence over a True 'follow_all_redirects'. Another 296 way of saying that is for 'follow_all_redirects' to have any affect, 297 'follow_redirects' must be True. 298 299 300.. attribute:: Http.follow_all_redirects 301 302 If ``False``, which is the default, only safe redirects are followed, where safe 303 means that the client is only doing a ``GET`` or ``HEAD`` on the URI to which it 304 is being redirected. If ``True`` then all redirects are followed. Note that a 305 False 'follow_redirects' takes precedence over a True 'follow_all_redirects'. 306 Another way of saying that is for 'follow_all_redirects' to have any affect, 307 'follow_redirects' must be True. 308 309 310.. attribute:: Http.forward_authorization_headers 311 312 If ``False``, which is the default, then Authorization: headers are 313 stripped from redirects. If ``True`` then Authorization: headers are left 314 in place when following redirects. This parameter only applies if following 315 redirects is turned on. Note that turning this on could cause your credentials 316 to leak, so carefully consider the consequences. 317 318 319.. attribute:: Http.force_exception_to_status_code 320 321 If ``True`` then no :mod:`httplib2` exceptions will be 322 thrown. Instead, those error conditions will be turned into :class:`Response` 323 objects that will be returned normally. 324 325 If ``False``, which is the default, then exceptions will be thrown. 326 327 328.. attribute:: Http.optimistic_concurrency_methods 329 330 By default a list that only contains "PUT", this attribute 331 controls which methods will get 'if-match' headers attached 332 to them from cached responses with etags. You can append 333 new items to this list to add new methods that should 334 get this support, such as "PATCH". 335 336.. attribute:: Http.ignore_etag 337 338 Defaults to ``False``. If ``True``, then any etags present in the cached 339 response are ignored when processing the current request, i.e. httplib2 does 340 **not** use 'if-match' for PUT or 'if-none-match' when GET or HEAD requests are 341 made. This is mainly to deal with broken servers which supply an etag, but 342 change it capriciously. 343 344If you wish to supply your own caching implementation then you will need to pass 345in an object that supports the following methods. Note that the :mod:`memcache` 346module supports this interface natively. 347 348 349.. _cache-objects: 350 351Cache Objects 352-------------- 353 354.. method:: Cache.get(key) 355 356 Takes a string *key* and returns the value as a string. 357 358 359.. method:: Cache.set(key, value) 360 361 Takes a string *key* and *value* and stores it in the cache. 362 363 364.. method:: Cache.delete(key) 365 366 Deletes the cached value stored at *key*. The value of *key* is a string. 367 368Response objects are derived from :class:`dict` and map header names (lower case 369with the trailing colon removed) to header values. In addition to the dict 370methods a Response object also has: 371 372 373.. _response-objects: 374 375Response Objects 376------------------ 377 378 379.. attribute:: Response.fromcache 380 381 If ``true`` the the response was returned from the cache. 382 383 384.. attribute:: Response.version 385 386 The version of HTTP that the server supports. A value of 11 means '1.1'. 387 388 389.. attribute:: Response.status 390 391 The numerical HTTP status code returned in the response. 392 393 394.. attribute:: Response.reason 395 396 The human readable component of the HTTP response status code. 397 398 399.. attribute:: Response.previous 400 401 If redirects are followed then the :class:`Response` object returned is just for 402 the very last HTTP request and *previous* points to the previous 403 :class:`Response` object. In this manner they form a chain going back through 404 the responses to the very first response. Will be ``None`` if there are no 405 previous respones. 406 407The Response object also populates the header ``content-location``, that 408contains the URI that was ultimately requested. This is useful if redirects were 409encountered, you can determine the ultimate URI that the request was sent to. 410All Response objects contain this key value, including ``previous`` responses so 411you can determine the entire chain of redirects. If 412:attr:`Http.force_exception_to_status_code` is ``True`` and the number of 413redirects has exceeded the number of allowed number of redirects then the 414:class:`Response` object will report the error in the status code, but the 415complete chain of previous responses will still be in tact. 416 417To do a simple ``GET`` request just supply the absolute URI of the resource: 418 419.. % ==== 4. ==== 420.. % Now is probably a good time for a complete example. (Alternatively, 421.. % an example giving the flavor of the module may be given before the 422.. % detailed list of functions.) 423 424.. _httplib2-example: 425 426Examples 427--------- 428 429:: 430 431 import httplib2 432 h = httplib2.Http() 433 resp, content = h.request("http://bitworking.org/") 434 assert resp.status == 200 435 assert resp['content-type'] == 'text/html' 436 437Here is more complex example that does a PUT of some text to a resource that 438requires authentication. The Http instance also uses a file cache in the 439directory ``.cache``. :: 440 441 import httplib2 442 h = httplib2.Http(".cache") 443 h.add_credentials('name', 'password') 444 resp, content = h.request("https://example.org/chap/2", 445 "PUT", body="This is text", 446 headers={'content-type':'text/plain'} ) 447 448Here is an example that connects to a server that supports the Atom Publishing 449Protocol. :: 450 451 import httplib2 452 h = httplib2.Http() 453 h.add_credentials(myname, mypasswd) 454 h.follow_all_redirects = True 455 headers = {'Content-Type': 'application/atom+xml'} 456 body = """<?xml version="1.0" ?> 457 <entry xmlns="http://www.w3.org/2005/Atom"> 458 <title>Atom-Powered Robots Run Amok</title> 459 <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> 460 <updated>2003-12-13T18:30:02Z</updated> 461 <author><name>John Doe</name></author> 462 <content>Some text.</content> 463 </entry> 464 """ 465 uri = "http://www.example.com/collection/" 466 resp, content = h.request(uri, "POST", body=body, headers=headers) 467 468Here is an example of providing data to an HTML form processor. In this case we 469presume this is a POST form. We need to take our data and format it as 470"application/x-www-form-urlencoded" data and use that as a body for a POST 471request. 472 473 474:: 475 476 >>> import httplib2 477 >>> import urllib 478 >>> data = {'name': 'fred', 'address': '123 shady lane'} 479 >>> body = urllib.urlencode(data) 480 >>> body 481 'name=fred&address=123+shady+lane' 482 >>> h = httplib2.Http() 483 >>> resp, content = h.request("http://example.com", method="POST", body=body) 484