From RFC 4880 [1]:
Scalar numbers are unsigned and are always stored in big-endian
format. Using n[k] to refer to the kth octet being interpreted, the
value of a two-octet scalar is ((n[0] << 8) + n[1]). The value of a
four-octet scalar is ((n[0] << 24) + (n[1] << 16) + (n[2] << 8) +
n[3]).
The struct big-endian byte-order character is '>' [2].
[1]: http://tools.ietf.org/search/rfc4880#section-3.1
[2]: http://docs.python.org/3/library/struct.html#byte-order-size-and-alignment
if not length_bytes:
raise NotImplementedError(
'old-format packet of indeterminate length')
+ length_format = '>{}'.format(length_type)
+ length_data = data[1: 1 + length_bytes]
+ self['length'] = _struct.unpack(length_format, length_data)[0]
def to_bytes(self):
pass