| 1 |
Introduction |
| 2 |
============ |
| 3 |
|
| 4 |
httplib2 is a comprehensive HTTP client library, httplib2.py supports many |
| 5 |
features left out of other HTTP libraries. |
| 6 |
|
| 7 |
If you want to help this project by bug report or code change, [contribution guidelines](contributing.md) may contain useful information. |
| 8 |
|
| 9 |
### HTTP and HTTPS |
| 10 |
|
| 11 |
HTTPS support is only available if the socket module was |
| 12 |
compiled with SSL support. |
| 13 |
|
| 14 |
### Keep-Alive |
| 15 |
|
| 16 |
Supports HTTP 1.1 Keep-Alive, keeping the socket open and |
| 17 |
performing multiple requests over the same connection if |
| 18 |
possible. |
| 19 |
|
| 20 |
### Authentication |
| 21 |
|
| 22 |
The following three types of HTTP Authentication are |
| 23 |
supported. These can be used over both HTTP and HTTPS. |
| 24 |
|
| 25 |
* Digest |
| 26 |
* Basic |
| 27 |
* WSSE |
| 28 |
|
| 29 |
### Caching |
| 30 |
|
| 31 |
The module can optionally operate with a private cache that |
| 32 |
understands the Cache-Control: header and uses both the ETag |
| 33 |
and Last-Modified cache validators. |
| 34 |
|
| 35 |
### All Methods |
| 36 |
|
| 37 |
The module can handle any HTTP request method, not just GET |
| 38 |
and POST. |
| 39 |
|
| 40 |
### Redirects |
| 41 |
|
| 42 |
Automatically follows 3XX redirects on GETs. |
| 43 |
|
| 44 |
### Compression |
| 45 |
|
| 46 |
Handles both 'deflate' and 'gzip' types of compression. |
| 47 |
|
| 48 |
### Lost update support |
| 49 |
|
| 50 |
Automatically adds back ETags into PUT requests to resources |
| 51 |
we have already cached. This implements Section 3.2 of |
| 52 |
Detecting the Lost Update Problem Using Unreserved Checkout. |
| 53 |
|
| 54 |
### Unit Tested |
| 55 |
|
| 56 |
A large and growing set of unit tests. |
| 57 |
|
| 58 |
|
| 59 |
Installation |
| 60 |
============ |
| 61 |
|
| 62 |
|
| 63 |
$ pip install httplib2 |
| 64 |
|
| 65 |
|
| 66 |
Strings vs bytes |
| 67 |
================ |
| 68 |
|
| 69 |
To successfully use http2lib, you absolutely must understand the following sentence: |
| 70 |
|
| 71 |
**The headers are strings, but the content body is bytes.** |
| 72 |
|
| 73 |
|
| 74 |
Usage |
| 75 |
===== |
| 76 |
|
| 77 |
A simple retrieval: |
| 78 |
|
| 79 |
```python |
| 80 |
import httplib2 |
| 81 |
h = httplib2.Http(".cache") |
| 82 |
(resp_headers, content) = h.request("http://example.org/", "GET") |
| 83 |
``` |
| 84 |
|
| 85 |
The 'content' is the content retrieved from the URL. The content |
| 86 |
is already decompressed or unzipped if necessary. |
| 87 |
|
| 88 |
To PUT some content to a server that uses SSL and Basic authentication: |
| 89 |
|
| 90 |
```python |
| 91 |
import httplib2 |
| 92 |
h = httplib2.Http(".cache") |
| 93 |
h.add_credentials('name', 'password') |
| 94 |
(resp, content) = h.request("https://example.org/chapter/2", |
| 95 |
"PUT", body="This is text", |
| 96 |
headers={'content-type':'text/plain'} ) |
| 97 |
``` |
| 98 |
|
| 99 |
Use the Cache-Control: header to control how the caching operates. |
| 100 |
|
| 101 |
```python |
| 102 |
import httplib2 |
| 103 |
h = httplib2.Http(".cache") |
| 104 |
(resp, content) = h.request("http://bitworking.org/", "GET") |
| 105 |
... |
| 106 |
(resp, content) = h.request("http://bitworking.org/", "GET", |
| 107 |
headers={'cache-control':'no-cache'}) |
| 108 |
``` |
| 109 |
|
| 110 |
The first request will be cached and since this is a request |
| 111 |
to bitworking.org it will be set to be cached for two hours, |
| 112 |
because that is how I have my server configured. Any subsequent |
| 113 |
GET to that URI will return the value from the on-disk cache |
| 114 |
and no request will be made to the server. You can use the |
| 115 |
Cache-Control: header to change the caches behavior and in |
| 116 |
this example the second request adds the Cache-Control: |
| 117 |
header with a value of 'no-cache' which tells the library |
| 118 |
that the cached copy must not be used when handling this request. |
| 119 |
|
| 120 |
More example usage can be found at: |
| 121 |
|
| 122 |
* https://github.com/httplib2/httplib2/wiki/Examples |
| 123 |
* https://github.com/httplib2/httplib2/wiki/Examples-Python3 |
| 124 |
|
| 125 |
|
| 126 |
### Decompression Limits |
| 127 |
|
| 128 |
To mitigate denial-of-service risks from maliciously crafted compressed responses, the library enforces configurable limits during decompression. Limits are checked in fixed order: **hard limit** → **safe limit** → **ratio**. |
| 129 |
|
| 130 |
- **hard limit** |
| 131 |
Absolute maximum decompressed output size (bytes). Exceeding it raises `DecodeLimitError`. Default: `10 GiB`. |
| 132 |
- **safe limit** |
| 133 |
Output size below which the ratio check is skipped (avoids false positives on small payloads). Default: `10 MiB`. |
| 134 |
- **ratio** |
| 135 |
Maximum allowed inflation factor (`output_bytes ÷ consumed_input_bytes`). Once output exceeds `safe_limit`, the ratio is enforced. Default: `100`. |
| 136 |
- **chunk size** |
| 137 |
Internal processing chunk size in bytes (affects granularity of limit checks). Default: `65536` (64 KiB). |
| 138 |
|
| 139 |
Configuration priority (highest first): |
| 140 |
1. `Http()` constructor arguments: `decode_limit_hard`, `decode_limit_safe`, `decode_limit_ratio`, `decode_limit_chunk` |
| 141 |
2. Environment variables: `httplib2_decode_limit_hard`, `httplib2_decode_limit_safe`, `httplib2_decode_limit_ratio`, `httplib2_decode_limit_chunk` (case-insensitive, uppercase also accepted) |
| 142 |
3. Library defaults (listed above) |
| 143 |
|
| 144 |
Example: |
| 145 |
```python |
| 146 |
h = Http(decode_limit_hard=50_000_000, decode_limit_ratio=50) |