tftp.client
tftp.client
The clients: :class:TFTPClient (blocking) and :class:AsyncTFTPClient (asyncio).
Siblings over one private base: the same arguments, the same options and the
same results, one waiting on sockets and one on the event loop. The one-shot
functions :func:download and :func:upload use the blocking client.
AsyncSink
Bases: Protocol
Where a download writes to: await write(data) takes one chunk.
AsyncSource
Bases: Protocol
Where an upload reads from: await read(n) returns up to n bytes, b"" at the end.
AsyncTFTPClient(host, port=69, *, timeout=1.0, retries=5, blksize=1428, windowsize=None, tsize=True, rollover=None, timeout_option=True, family=0, src=None, fallback=True, dally=False, backoff=2.0, max_timeout=None, deadline=None, max_size=None, strict_source=True, utimeout=False, extra_options=None, registry=None, on_negotiated=None, trace=None)
Bases: _ClientBase
:class:tftp.TFTPClient for asyncio: the same arguments, coroutine methods.
A source or destination is a path (or bytes, to upload) -- local file
I/O happens on the loop, which is fine for files and wrong for anything
slow -- or an asynchronous stream: an object with async read(n) to
upload from, an object with async write(data) to download into. Those
are driven with backpressure: a slow sink slows the transfer instead of
filling memory. The client never closes a stream it was given.
Source code in src/tftp/client/_core.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 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 | |
download(filename, dst, *, mode='octet', progress=None, max_size=None)
async
Fetch filename into dst: a path, or an object with async write(data).
As :meth:tftp.TFTPClient.download: a path is replaced only when the
transfer succeeds, and max_size overrides the client's.
Source code in src/tftp/client/_asyncio.py
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 | |
listdir(dirname='', *, max_size=LISTING_LIMIT)
async
:meth:tftp.TFTPClient.listdir, as a coroutine.
Source code in src/tftp/client/_asyncio.py
295 296 297 298 299 300 301 302 303 304 305 | |
size(filename, *, mode='octet')
async
:meth:tftp.TFTPClient.size, without blocking the loop (it runs in the executor).
Source code in src/tftp/client/_asyncio.py
283 284 285 286 287 | |
stat(filename, *, mode='octet')
async
:meth:tftp.TFTPClient.stat, without blocking the loop (it runs in the executor).
Source code in src/tftp/client/_asyncio.py
289 290 291 292 293 | |
stream(filename, *, mode='octet', buffer=1 << 20)
async
Yield filename's contents as it arrives, with backpressure.
At most buffer bytes are held: half in the transfer's own buffer
and at most as much again waiting for the consumer. When the consumer
is slow, acknowledgements are held back and the server waits. Closing
the generator early ends the transfer (the server is sent ERROR 0).
Source code in src/tftp/client/_asyncio.py
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 | |
upload(filename, src, *, mode='octet', progress=None)
async
Send src: a path, bytes, or an object with async read(n).
Source code in src/tftp/client/_asyncio.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
RemoteStat
Bases: NamedTuple
What :meth:TFTPClient.stat learnt about a name on the server.
size is None when the server reports none (and for a directory);
mtime (seconds since the epoch) needs a server speaking x-mtime;
is_dir needs one speaking x-list -- other servers report a
directory as not found.
TFTPClient(host, port=69, *, timeout=1.0, retries=5, blksize=1428, windowsize=None, tsize=True, rollover=None, timeout_option=True, family=0, src=None, fallback=True, dally=False, backoff=2.0, max_timeout=None, deadline=None, max_size=None, strict_source=True, utimeout=False, extra_options=None, registry=None, on_negotiated=None, trace=None)
Bases: _ClientBase
A TFTP client bound to one server.
:param host: server name or address: a string ("name", "10.0.0.1",
"[v6]:port"), an ipaddress address or interface, or a
netimps.Host. Only a string can carry a port.
:param port: server port.
:param timeout: seconds before a retransmission. Also requested from the
server, as timeout when whole and as utimeout when fractional,
unless timeout_option is false.
:param retries: retransmissions of one packet before giving up.
:param blksize: requested DATA size, 8..65464. None asks for nothing,
leaving RFC 1350's 512. The default 1428 fits an Ethernet frame on
IPv4 and IPv6 without fragmenting. "mtu" sizes it to the MTU of
the interface the route to the server uses (falling back to 1428).
:param windowsize: requested RFC 7440 window. None asks for nothing
(one packet per ACK).
:param tsize: request the transfer size (RRQ) or announce it (WRQ).
:param rollover: request block-number rollover to 0 or 1. None asks
for nothing; wrapping to 0 is assumed and a server wrapping to 1 is
followed.
:param family: socket.AF_INET / AF_INET6 to force one, 0 for
whatever host resolves to first.
:param src: (address, port) to send from; the address as for host.
:param fallback: when the server answers a request that carries options
with ERROR 8, 4 or 0 (RFC 2347: it "may" be repeated without them),
retry once without any.
:param dally: after acknowledging the last DATA of a download, keep
answering a retransmitted last DATA for one timeout (RFC 1350
section 6). Costs that much time on every download.
:param backoff: each consecutive retransmission waits this many times
longer (RFC 1123 4.2.3.2); progress resets it to timeout.
:param max_timeout: ceiling for the backed-off wait, at least timeout;
None is eight times timeout.
:param deadline: seconds a whole transfer may take, or None. A
relative duration, unlike the engine's deadline attribute.
:param max_size: octets a download may bring, or None for no bound. A
server announcing more, or sending more than it announced or than
this, is sent ERROR 3 and ends the download with
:class:TransferTooLargeError (or :class:TFTPProtocolError for
more than it announced).
:param utimeout: send a fractional timeout as tftp-hpa's utimeout
(otherwise a fractional timeout is not requested at all).
:param extra_options: further options to request, verbatim (extensions
such as blksize2/cookie, or custom ones). Built-in ones are
validated in the OACK; custom ones land in negotiated.extra.
:param registry: the :class:OptionRegistry used to validate the OACK.
:param trace: trace(PacketEvent) for every datagram this client sends
or receives (role="client", one session id per transfer).
:param on_negotiated: called as on_negotiated(negotiated, peer) once
the server has answered the request (OACK, first DATA or ACK 0),
before any data moves -- the peer is the server's transfer address.
:param strict_source: the first answer must come from the address the
request was sent to. False accepts any address, for multi-homed
servers that answer from another one (the transfer then locks on to
whichever answered first).
A client is not thread-safe; use one per thread.
Source code in src/tftp/client/_core.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 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 | |
download(filename, dst, *, mode='octet', progress=None, max_size=None)
Fetch filename into dst (a path or a writable binary file).
A path is written to a temporary file beside it, which replaces it when
the transfer succeeds: a download that fails leaves what was there. A
path that exists and is not a regular file (a device, a pipe) is
written in place. max_size overrides the client's.
Raises :class:RemoteError, :class:TransferTimeoutError,
:class:TransferTooLargeError or :class:TFTPProtocolError;
OSError for local failures.
Source code in src/tftp/client/_sync.py
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 | |
get(filename, *, mode='octet', max_size=None)
Fetch filename and return its contents.
Source code in src/tftp/client/_sync.py
149 150 151 152 153 | |
listdir(dirname='', *, max_size=LISTING_LIMIT)
The entries of directory dirname ("" is the server's root).
A listing longer than max_size octets (16 MiB) raises
:class:TransferTooLargeError.
Needs a server speaking pytftp's x-list extension (tftp.TFTPServer
allowing :data:LISTING_OPTIONS); there is no standard way to list
in TFTP. Raises NotADirectoryError when the name is a file (the
transfer is abandoned at once), and :class:FileNotFound when it does
not exist -- which is also what a server without listing support
answers for a directory.
Source code in src/tftp/client/_sync.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
path(*segments, mode='octet')
A :class:tftp.path.TFTPPath on this server (needs the path extra).
Source code in src/tftp/client/_sync.py
143 144 145 146 147 | |
put(filename, data, *, mode='octet')
Upload data as filename.
Source code in src/tftp/client/_sync.py
172 173 174 | |
size(filename, *, mode='octet')
The size of filename on the server, without transferring it.
Sends an RRQ asking only for tsize and abandons the transfer as
soon as the server answers (ERROR 8 to an OACK, as RFC 2347 lets a
client refuse one). Returns None when the server does not report
sizes -- unless the whole file fits in the first block, whose length
then is the size. Raises :class:RemoteError (FileNotFound...)
like a download would.
Source code in src/tftp/client/_sync.py
233 234 235 236 237 238 239 240 241 242 243 | |
stat(filename, *, mode='octet')
Size, modification time and kind of filename, in one probe.
Like :meth:size, with x-mtime and x-list asked for as well:
a pytftp server allowing them reports the time and recognises a
directory; any other server ignores them (RFC 2347). A server that
refuses the request over its options (ERROR 8) is probed again for
the size alone, when fallback is on.
Source code in src/tftp/client/_sync.py
245 246 247 248 249 250 251 252 253 254 | |
upload(filename, src, *, mode='octet', progress=None)
Send src (a path, a readable binary file or bytes) as filename.
Source code in src/tftp/client/_sync.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__getattr__(name)
Bind :class:AsyncTFTPClient on first use, so importing the package does not import asyncio.
Source code in src/tftp/client/__init__.py
34 35 36 37 38 39 40 41 | |
download(host, filename, dst, /, *, port=69, mode='octet', progress=None, **client_options)
One-shot :meth:TFTPClient.download; client_options go to :class:TFTPClient.
Source code in src/tftp/client/_sync.py
472 473 474 475 476 477 478 479 480 481 482 483 484 | |
upload(host, filename, src, /, *, port=69, mode='octet', progress=None, **client_options)
One-shot :meth:TFTPClient.upload; client_options go to :class:TFTPClient.
Source code in src/tftp/client/_sync.py
487 488 489 490 491 492 493 494 495 496 497 498 499 | |