def encode(self):
        tftpassert(self.filename, "filename required in initial packet")
        tftpassert(self.mode, "mode required in initial packet")

        ptype = None
        if self.opcode == 1: ptype = "RRQ"
        else:                ptype = "WRQ"
        log.debug("Encoding %s packet, filename = %s, mode = %s",
                     ptype, self.filename, self.mode)
        for key in self.options:
            log.debug("    Option %s = %s", key, self.options[key])

        format = "!H"
        format += "%dsx" % len(self.filename)
        if self.mode == "octet":
            format += "5sx"
        else:
            raise AssertionError, "Unsupported mode: %s" % mode
        # Add options.
        options_list = []
        if self.options.keys() > 0:
            log.debug("there are options to encode")
            for key in self.options:
                # Populate the option name
                format += "%dsx" % len(key)
                options_list.append(key)
                # Populate the option value
                format += "%dsx" % len(str(self.options[key]))
                options_list.append(str(self.options[key]))

        log.debug("format is %s", format)
        log.debug("options_list is %s", options_list)
        log.debug("size of struct is %d", struct.calcsize(format))

        self.buffer = struct.pack(format,
                                  self.opcode,
                                  self.filename,
                                  self.mode,
                                  *options_list)

        log.debug("buffer is %s", repr(self.buffer))
        return self
Encode the packet's buffer from the instance variables.
dataset/ETHPy150Open msoulier/tftpy/tftpy/TftpPacketTypes.py/TftpPacketInitial.encode