views:

67

answers:

2

There is nice one for java - MINA.

Once I've heard that there is something similar for python. But can't remind.

EDIT: to be more specific, I would like to have a tool which would help me to create a coded for some binary stream.

EDIT2: I'd like to list solutions here (thanks Scott for related topics) Listed in order i'd use it.

+5  A: 

python has pack/unpack in the standard lib that can be used to interpret binary data and map them to structs

see "11.3. Working with Binary Data Record Layouts" here http://docs.python.org/tutorial/stdlib2.html

or here http://docs.python.org/library/struct.html

Nikolaus Gradwohl
+2  A: 

Have you tried the bitstring module? (Full disclosure: I wrote it).

It's designed to make constructing and parsing binary data as simple as possible. Take a look at a few examples to see if it's anything like you need.

This snippet does some parsing of a H.264 video file:

    from bitstring import Bits
    s = Bits(filename='somefile.h264')
    profile_idc = s.read('uint:8')
    # Multiple reads in one go returns a list:
    constraint_flags = s.readlist('4*uint:1')
    reserved_zero_4bits = s.read('bin:4')
    level_idc = s.read('uint:8')
    seq_parameter_set_id = s.read('ue')
    if profile_idc in [100, 110, 122, 244, 44, 83, 86]:
        chroma_format_idc = s.read('ue')
        if chroma_format_idc == 3:
            separate_colour_plane_flag = s.read('uint:1')
        bit_depth_luma_minus8 = s.read('ue')
        bit_depth_chroma_minus8 = s.read('ue')
        ...
Scott Griffiths
Your examples link only works on your machine....
Amoss
@Amoss: Thanks, fixed it!
Scott Griffiths
Looks like an interesting library. I'm going to have a more extensive play with it when I find some time.
Amoss