# nobodd.tftp

Defines the data structures used by the Trivial File Transfer Protocol (TFTP). You should never need these directly; use the classes in `nobodd.tftpd` to construct a TFTP server instead.

## Enumerations

class nobodd.tftp.OpCode(*values)

  Enumeration of op-codes for the Trivial File Transfer Protocol (TFTP). These appear at the start of any TFTP packet to indicate what sort of packet it is.

class nobodd.tftp.Error(*values)

  Enumeration of error status for the Trivial File Transfer Protocol (TFTP). These are used in packets with `OpCode` `ERROR` to indicate the sort of error that has occurred.

## Constants

nobodd.tftp.TFTP_BLKSIZE

nobodd.tftp.TFTP_MIN_BLKSIZE

nobodd.tftp.TFTP_DEF_BLKSIZE

nobodd.tftp.TFTP_MAX_BLKSIZE

  Constants defining the `blksize` TFTP option; the name of the option, its minimum, default, and maximum values.

nobodd.tftp.TFTP_TIMEOUT

nobodd.tftp.TFTP_UTIMEOUT

nobodd.tftp.TFTP_MIN_TIMEOUT_NS

nobodd.tftp.TFTP_DEF_TIMEOUT_NS

nobodd.tftp.TFTP_MAX_TIMEOUT_NS

  Constants defining the `timeout` and `utimeout` TFTP options; the name of the options, the minimum, default, and maximum values, in units of nano-seconds.

nobodd.tftp.TFTP_BINARY

nobodd.tftp.TFTP_NETASCII

nobodd.tftp.TFTP_MODES

  Constants defining the available transfer modes.

nobodd.tftp.TFTP_TSIZE

  Constant defining the name of the `tsize` TFTP option.

nobodd.tftp.TFTP_OPTIONS

  Constant defining the TFTP options available for negotiation.

## Packets

class nobodd.tftp.Packet

  Abstract base class for all TFTP packets. This provides the class method `Packet.from_bytes()` which constructs and returns the appropriate concrete sub-class for the `OpCode` found at the beginning of the packet’s data.

  Instances of the concrete classes may be converted back to `bytes` simply by calling `bytes` on them:

      >>> b = b'\x00\x01config.txt\0octet\0'
      >>> r = Packet.from_bytes(b)
      >>> r
      RRQPacket(filename='config.txt', mode='octet', options=FrozenDict({}))
      >>> bytes(r)
      b'\x00\x01config.txt\x00octet\x00'

  Concrete classes can also be constructed directly, for conversion into `bytes` during transfer:

      >>> bytes(ACKPacket(block=10))
      b'\x00\x04\x00\n'
      >>> bytes(RRQPacket('foo', 'netascii', {'tsize': 0}))
      b'\x00\x01foo.txt\x00netascii\x00tsize\x000\x00'

  classmethod from_bytes(s)

    Given a `bytes`-string s, checks the `OpCode` at the front, and constructs one of the concrete packet types defined below, returning (instead of `Packet` which is abstract):

        >>> Packet.from_bytes(b'\x00\x01config.txt\0octet\0')
        RRQPacket(filename='config.txt', mode='octet', options=FrozenDict({}))

  classmethod from_data(data)

    Constructs an instance of the packet class with the specified data (which is everything in the `bytes`-string passed to `from_bytes()` minus the header). This method is not implemented in `Packet` but is expected to be implemented in any concrete descendant.

class nobodd.tftp.RRQPacket(filename, mode, options=None)

  Concrete type for `RRQ` (read request) packets.

  These packets are sent by a client to initiate a transfer. They include the filename to be sent, the mode to send it (one of the strings “octet” or “netascii”), and any options the client wishes to negotiate.

  classmethod from_data(data)

    Constructs an instance of the packet class with the specified data (which is everything in the `bytes`-string passed to `from_bytes()` minus the header). This method is not implemented in `Packet` but is expected to be implemented in any concrete descendant.

class nobodd.tftp.WRQPacket(filename, mode, options=None)

  Concrete type for `WRQ` (write request) packets.

  These packets are sent by a client to initiate a transfer to the server. They include the filename to be sent, the mode to send it (one of the strings “octet” or “netascii”), and any options the client wishes to negotiate.

class nobodd.tftp.DATAPacket(block, data)

  Concrete type for `DATA` packets.

  These are sent in response to `RRQ`, `WRQ`, or `ACK` packets and each contains a block of the file to transfer, data (by default, 512 bytes long unless this is the final `DATA` packet), and the block number.

  classmethod from_data(data)

    Constructs an instance of the packet class with the specified data (which is everything in the `bytes`-string passed to `from_bytes()` minus the header). This method is not implemented in `Packet` but is expected to be implemented in any concrete descendant.

class nobodd.tftp.ACKPacket(block)

  Concrete type for `ACK` packets.

  These are sent in response to `DATA` packets, and acknowledge the successful receipt of the specified block.

  classmethod from_data(data)

    Constructs an instance of the packet class with the specified data (which is everything in the `bytes`-string passed to `from_bytes()` minus the header). This method is not implemented in `Packet` but is expected to be implemented in any concrete descendant.

class nobodd.tftp.ERRORPacket(error, message=None)

  Concrete type for `ERROR` packets.

  These are sent by either end of a transfer to indicate a fatal error condition. Receipt of an `ERROR` packet immediately terminates a transfer without further acknowledgment.

  The `ERROR` packet contains the error code (an `Error` value) and a descriptive message.

  classmethod from_data(data)

    Constructs an instance of the packet class with the specified data (which is everything in the `bytes`-string passed to `from_bytes()` minus the header). This method is not implemented in `Packet` but is expected to be implemented in any concrete descendant.

class nobodd.tftp.OACKPacket(options)

  Concrete type for `OACK` packets.

  This is sent by the server instead of an initial `DATA` packet, when the client includes options in the `RRQ` packet. The content of the packet is all the options the server accepts, and their (potentially revised) values.

  classmethod from_data(data)

    Constructs an instance of the packet class with the specified data (which is everything in the `bytes`-string passed to `from_bytes()` minus the header). This method is not implemented in `Packet` but is expected to be implemented in any concrete descendant.