FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Construct_(python_library)

Construct (Python library)

Construct is a Python library for the construction and deconstruction of data structures in a declarative fashion. In this context, construction, or building, refers to the process of converting (serializing) a programmatic object into a binary representation.

Deconstruction, or parsing, refers to the opposite process of converting (deserializing) binary data into a programmatic object. Being declarative means that user code defines the data structure, instead of the convention of writing procedural code to accomplish the goal. Construct can work seamlessly with bit- and byte-level data granularity and various byte-ordering.

ethernet = Struct( "destination" / Bytes(6), "source" / Bytes(6), "type" / Enum(Int16ub, IPv4=0x0800, ARP=0x0806, RARP=0x8035, X25=0x0805, IPX=0x8137, IPv6=0x86DD, ), )

Next, the IP header (layer 3):

layer4tcp = Struct( tcp, # ... payload ) layer3ip = Struct( ip, "next" / Switch(this.protocol, { "TCP" : layer4tcp, } ), ) layer2ethernet = Struct( ethernet, "next" / Switch(this.type, { "IP" : layer3ip, } ), )

At this point, the code can parse captured TCP/IP frames into "packet" objects and build these packet objects back into binary representation.

  1. ↑ "Data::ParseBinary - Yet Another parser for binary structures - metacpan.org". metacpan.org. Retrieved 2023-08-29.
  2. ↑

View original on Wikipedia | Back | FazBrowse Home