http_fs¶
HTTP(S) FileSystem implementation using requests.
This module provides HttpFs, a FileSystem backend for http:// and https:// URLs. Reads are served through the block-cache machinery using Range requests when the server supports them, while writes are staged locally and uploaded with a single PUT request when the file is closed. Directory listing is parsed from the anchor links of an HTML index page.
HttpReader
¶
Reader fetching data from an HTTP server through a requests session.
Source code in py_misc_utils/fs/http_fs.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 | |
tag(head)
classmethod
¶
Build a cache tag from an HTTP HEAD response.
The ETag header is preferred; size and last-modified time are used as a fallback when the server does not provide an ETag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head
|
HEAD response for the URL. |
required |
Returns:
| Type | Description |
|---|---|
|
The cache tag for the URL. |
Source code in py_misc_utils/fs/http_fs.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
support_blocks()
¶
Return True if the server supports Range requests.
Source code in py_misc_utils/fs/http_fs.py
83 84 85 | |
read_block(bpath, offset, size)
¶
Read data into a local cache file.
When the server supports Range requests a single ranged GET of size bytes is issued, otherwise the whole file is streamed into the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bpath
|
Local path of the cache file to write the contents to. |
required | |
offset
|
Offset in the file where reading starts. |
required | |
size
|
Number of bytes to read. |
required |
Returns:
| Type | Description |
|---|---|
|
The number of bytes written into the cache file. |
Source code in py_misc_utils/fs/http_fs.py
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 | |
HttpFs
¶
Bases: FsBase
FileSystem implementation for http:// and https:// URLs.
A single requests session is shared by all the operations, configured with the constructor keyword arguments such as headers or authentication. Writes are staged into a temporary file and uploaded on close through a PUT request, so writeback semantics apply.
See py_misc_utils.fs for the shared backend contract.
Example
import http.server
import threading
from py_misc_utils import cached_file as chf
from py_misc_utils.fs import http_fs as hf
# A local HTTP server, so that the example is runnable as-is.
class H(http.server.BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def do_HEAD(self):
body = self.server.routes.get(self.path)
if body is None:
return self.send_error(404)
self.send_response(200)
self.send_header('ETag', '"t1"')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
def do_GET(self):
body = self.server.routes.get(self.path)
if body is None:
return self.send_error(404)
self.send_response(200)
self.send_header('ETag', '"t1"')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
H.server_class = type('S', (http.server.ThreadingHTTPServer,), {'routes': {}})
server = H.server_class(('127.0.0.1', 0), H)
server.routes['/data/file.bin'] = b'0123456789' * 100
threading.Thread(target=server.serve_forever, daemon=True).start()
url = f'http://127.0.0.1:{server.server_address[1]}'
iface = chf.CacheInterface('/tmp/gfs-cache')
fs = hf.HttpFs(cache_dir='/tmp/gfs-cache', cache_iface=iface)
with fs.open(url + '/data/file.bin', mode='rb') as f:
print(f.read(16))
server.shutdown()
server.server_close()
Source code in py_misc_utils/fs/http_fs.py
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
stat(url)
¶
Stat a URL from the headers of a HEAD request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
URL to stat. |
required |
Returns:
| Type | Description |
|---|---|
|
The DirEntry built from the content-length, last-modified and ETag |
|
|
headers. |
Source code in py_misc_utils/fs/http_fs.py
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 | |
open(url, mode, **kwargs)
¶
Open a URL for reading or writing.
Read modes are served through the block-cache interface, while write modes stage the data into a temporary file uploaded with a PUT request when the stream is closed.
Since HTTP has no append operation, append modes download the existing content - if any - into the staging file and upload the whole result on close: concurrent writers are not merged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
URL to open. |
required | |
mode
|
Open mode string. |
required | |
**kwargs
|
Extra keyword arguments forwarded to the cache interface. |
{}
|
Returns:
| Type | Description |
|---|---|
|
A file-like object, wrapped into a TextIOWrapper in text modes. |
Source code in py_misc_utils/fs/http_fs.py
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 | |
remove(url)
¶
Remove a URL with a DELETE request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
URL of the resource to remove. |
required |
Source code in py_misc_utils/fs/http_fs.py
293 294 295 296 297 298 299 | |
rename(src_url, dest_url)
¶
Rename a URL downloading and re-uploading its contents.
Since HTTP has no rename operation, the contents are downloaded from the source and uploaded to the destination, then the source is removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
URL of the source resource. |
required | |
dest_url
|
URL of the destination resource. |
required |
Source code in py_misc_utils/fs/http_fs.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
mkdir(url, mode=None)
¶
No-op since HTTP has no directories.
Source code in py_misc_utils/fs/http_fs.py
318 319 | |
makedirs(url, mode=None, exist_ok=None)
¶
No-op since HTTP has no directories.
Source code in py_misc_utils/fs/http_fs.py
321 322 | |
rmdir(url)
¶
No-op since HTTP has no directories.
Source code in py_misc_utils/fs/http_fs.py
324 325 | |
rmtree(url, ignore_errors=None)
¶
No-op since HTTP has no directories.
Source code in py_misc_utils/fs/http_fs.py
327 328 | |
list(url)
¶
Enumerate the entries linked from an HTML directory page.
The URL must be an HTML page holding the anchor links of its children - plain file URLs or pages without links enumerate nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
URL of the directory page to enumerate. |
required |
Yields:
| Type | Description |
|---|---|
|
A DirEntry for every relative anchor of the page, as returned by |
|
|
stat() on the linked URL. Absolute or unknown links are ignored and |
|
|
entries which cannot be statted are skipped with a debug log. |
Source code in py_misc_utils/fs/http_fs.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
put_file(url, data_gen)
¶
Upload data chunks to a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
Destination URL of the upload. |
required | |
data_gen
|
Iterable of bytes chunks to upload. |
required |
Source code in py_misc_utils/fs/http_fs.py
422 423 424 425 426 427 428 429 | |
get_file(url)
¶
Read the contents of a URL as chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
URL to read. |
required |
Yields:
| Type | Description |
|---|---|
|
The file contents as bytes chunks. |
Source code in py_misc_utils/fs/http_fs.py
431 432 433 434 435 436 437 438 439 440 | |
as_local(url, **kwargs)
¶
Return a local file with the contents of a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
URL of the file to access. |
required | |
**kwargs
|
Extra keyword arguments forwarded to the cache interface. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The path of the local file, downloaded through the cache interface. |
Source code in py_misc_utils/fs/http_fs.py
442 443 444 445 446 447 448 449 450 451 452 453 454 | |
link(src_url, dest_url)
¶
Copy the resource, since HTTP has no hard link support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
URL of the source resource. |
required | |
dest_url
|
URL of the destination resource. |
required |
Source code in py_misc_utils/fs/http_fs.py
456 457 458 459 460 461 462 463 464 | |
symlink(src_url, dest_url)
¶
Copy the resource, since HTTP has no symbolic link support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
URL of the source resource. |
required | |
dest_url
|
URL of the destination resource. |
required |
Source code in py_misc_utils/fs/http_fs.py
466 467 468 469 470 471 472 473 | |