Standard library
nox.transfer
Stream HTTP bodies to and from files without holding them in memory.
Text
import nox.transferCapability: network and filesystem (unavailable in freestanding profiles). It is built on nox.http's streams and nox.fs's file handles; use those
directly when you need control over the chunks. The security note of nox.fs applies to path.
| Function | Description |
|---|---|
download_to_file(url, path, headers, timeout_ms=0) |
downloads url into path and returns the HTTP status |
upload_file(method, url, path, headers, timeout_ms=0) |
uploads the file at path as the request body (POST, PUT or PATCH) and returns the HttpResponse |
Memory use is bounded by a 64 KiB chunk, so the file can be many gigabytes.
download_to_filewrites topath + ".nox-part"and renames it overpathonly after a 2xx response arrived completely. For any other status, or when the transfer fails (connection error, timeout, truncated body),pathis left untouched, the temporary file is removed, and the status is returned (orHttpError/HttpTimeoutErrorraised for a failure).upload_filesends a fixedContent-Lengthtaken from the file size, reading the file in chunks. The response body is read into memory; for a large response open a stream withnox.http.stream_uploadyourself. A file that shrinks while it is being sent raisesHttpError.timeout_ms > 0is the idle timeout per operation described innox.http.
Text
import nox.transfer
from nox.http import HttpResponse
status: int = nox.transfer.download_to_file("https://example.com/big.iso", "big.iso", {})
if status != 200:
print("download failed:", status)
r: HttpResponse = nox.transfer.upload_file("PUT", "https://example.com/files/big.iso", "big.iso", {"Content-Type": "application/octet-stream"})
print(r.status)