Skip to main content

An io-free stream parser which helps implementing network protocols in the `Sans-IO` way

Project description

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
  • 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iofree-0.2.4.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

iofree-0.2.4-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file iofree-0.2.4.tar.gz.

File metadata

  • Download URL: iofree-0.2.4.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.7

File hashes

Hashes for iofree-0.2.4.tar.gz
Algorithm Hash digest
SHA256 53c2f14daae1ea64ac5b6eebb6d346e613950b9074d6242a962cb50aa2604dd3
MD5 d05425f35ed3ba953ef71990d5ad8ad0
BLAKE2b-256 423b8eb60da76d9c5212382acaf59239d016541468b903c49886a0ae17b16412

See more details on using hashes here.

File details

Details for the file iofree-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: iofree-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.7

File hashes

Hashes for iofree-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 56e871f2974fa7ce9e4b5bc75199292b4cb435537847a3fb588e0bf434d2c7e0
MD5 69fa4b9346db452421413330e8412737
BLAKE2b-256 9385a9400672b5c31d36c20710b069df2f011a8aef32564121f5ed325a9db3ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page