HTTP client¶
The HTTP layer makes raw socket-level GET requests with no third-party HTTP library. TLS, chunked transfer-encoding, redirect following, and response caching are all handled here.
HTTPClient¶
A simple, cache-aware HTTP client using raw sockets.
All requests are made with HTTP/1.1 GET. TLS is supported for https://
URLs. Responses are optionally cached in a :class:~go2web.cache.store.CacheStore.
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_TIMEOUT |
Socket timeout in seconds (default |
|
MAX_REDIRECTS |
Maximum number of redirects to follow before raising
:class: |
|
BUFFER_SIZE |
Read buffer size in bytes (default |
Example
Fetching a URL with caching disabled:
client = HTTPClient(use_cache=False) response = client.get("https://example.com") print(response.status) 200
Using a custom cache location:
from pathlib import Path from go2web.cache.store import CacheStore cache = CacheStore(cache_file=Path("/tmp/my_cache.json")) client = HTTPClient(cache=cache)
Source code in src/go2web/http/client.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
__init__(cache=None, use_cache=True)
¶
Initialise the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache
|
CacheStore | None
|
A :class: |
None
|
use_cache
|
bool
|
Set to |
True
|
Source code in src/go2web/http/client.py
get(url, *, follow_redirects=True)
¶
Perform an HTTP GET request, following redirects by default.
The cache is checked before issuing a network request. On a cache
miss, the live response is stored (unless caching is disabled or the
server sends Cache-Control: no-store).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to fetch. A missing |
required |
follow_redirects
|
bool
|
When |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
Response
|
class: |
Raises:
| Type | Description |
|---|---|
HTTPError
|
On redirect loops, too many redirects, unsupported
schemes, or a redirect with no |
Example
client = HTTPClient() response = client.get("example.com") # https:// added automatically response.status 200
Source code in src/go2web/http/client.py
Response¶
A parsed HTTP response.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
int
|
The HTTP status code (e.g. |
reason |
str
|
The human-readable status phrase (e.g. |
headers |
dict[str, str]
|
Response headers with lower-cased keys for easy lookup. |
body |
str
|
The decoded response body as a string. |
Source code in src/go2web/http/client.py
get_content_type()
¶
Return the value of the Content-Type header.
Returns:
| Type | Description |
|---|---|
str
|
The content-type string (e.g. |
str
|
or an empty string if the header is absent. |
Source code in src/go2web/http/client.py
is_redirect()
¶
Return True if the status code indicates a redirect.
The following codes are treated as redirects: 301, 302, 303, 307, 308.