Skip to main content

iofree

Build Status Documentation Status Python Version Version Format License codecov

iofree is an easy-to-use and powerful library to help you implement network protocols and binary parsers.

Installation

pip install iofree

Advantages

Using iofree, you can:

  • define network protocols and file format in a clear and precise manner
  • parse both binary streams and files

Documentation

Basic Usage

>>> from iofree import schema
>>> schema.uint8(1)
b'\x01'
>>> schema.uint32be(3)
b'\x00\x00\x00\x03'
>>> schema.uint32be.parse(b'\x00\x00\x00\x03')
3

Tutorial 1: a simple parser

>>> class Simple(schema.BinarySchema):
...     a = schema.uint8
...     b = schema.uint32be # "be" for big-endian
...
>>> Simple(1, 3).binary
b'\x01\x00\x00\x00\x03'
>>> binary = _
>>> Simple.parse(binary)
<Simple(a=1, b=3)>

Built-in units:

commonly used number units:

  • int8 uint8
  • int16 int16be uint16 uint16be
  • int24 int24be uint24 uint24be
  • int32 int32be uint32 uint32be
  • int64 int64be uint64 uint64be
  • float16 float16be
  • float32 float32be
  • float64 float64be

simple units:

  • Bytes
  • String
  • EndWith

composite units:

  • LengthPrefixedBytes
  • LengthPrefixedString
  • LengthPrefixedObjectList
  • LengthPrefixedObject
  • MustEqual
  • Switch
  • SizedIntEnum
  • Convert
  • Group

API docs

Here is a real life example definition of socks5 client request, you can see the following code snippet:

class Socks5ClientRequest(schema.BinarySchema):
    ver = schema.MustEqual(schema.uint8, 5)
    cmd = schema.SizedIntEnum(schema.uint8, Cmd)
    rsv = schema.MustEqual(schema.uint8, 0)
    addr = Addr

Tutorial 2: define socks5 address format

In [1]: import socket
   ...: from iofree import schema
   ...:
   ...:
   ...: class Addr(schema.BinarySchema):
   ...:     atyp: int = schema.uint8
   ...:     host: str = schema.Switch(
   ...:         "atyp",
   ...:         {
   ...:             1: schema.Convert(
   ...:                 schema.Bytes(4), encode=socket.inet_aton, decode=socket.inet_ntoa
   ...:
   ...:             ),
   ...:             4: schema.Convert(
   ...:                 schema.Bytes(16),
   ...:                 encode=lambda x: socket.inet_pton(socket.AF_INET6, x),
   ...:                 decode=lambda x: socket.inet_ntop(socket.AF_INET6, x),
   ...:             ),
   ...:             3: schema.LengthPrefixedString(schema.uint8),
   ...:         },
   ...:     )
   ...:     port: int = schema.uint16be
   ...:

In [2]: addr = Addr(1, '172.16.1.20', 80)

In [3]: addr
Out[3]: <Addr(atyp=1, host='172.16.1.20', port=80)>

In [4]: addr.binary
Out[4]: b'\x01\xac\x10\x01\x14\x00P'

In [5]: Addr.parse(addr.binary)
Out[5]: <Addr(atyp=1, host='172.16.1.20', port=80)>

A complete socks5 Addr definition

Projects using iofree

References

iofree parser is inspired by project ohneio.

Metadata

Release files for iofree 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iofree 0.2.5
File Size Uploaded
iofree-0.2.5.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iofree 0.2.5
File Interpreter ABI Platform
iofree-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 22.1 kB

Release files / iofree-0.2.5.tar.gz

Download URL iofree-0.2.5.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
139f7ff1e1a5e65d594c4d01743a1ad69cdf9d8008db2ff5fce058d6b7b6cb6b
BLAKE2b-256 checksum
How to use checksums
5e87979d679af798b09a2bc45690fdc771bdd688936ff594ad3c2ae03ea4ca01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.8

Release files / iofree-0.2.5-py3-none-any.whl

Download URL iofree-0.2.5-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d5970e42e11892dbf58976ea9b5097715594c2b6ab14c89ccd075c079449507
BLAKE2b-256 checksum
How to use checksums
762b0f8b1d653a23ffcb0891671eb11054867e5ec0b052b8913ce9e967f24387
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.8

Release history Release notifications | RSS feed

This release

0.2.5 This release

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page