Metadata-Version: 2.1
Name: fstrm
Version: 0.2.0
Summary: Frame Streams implementation in Python
Home-page: https://github.com/dmachard/python-framestream
Author: Denis MACHARD
Author-email: d.machard@gmail.com
License: UNKNOWN
Keywords: framestreams fstrm encoder decoder
Platform: any
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Libraries
Description-Content-Type: text/markdown
License-File: LICENSE

# Frame Streams implementation in Python

![Testing](https://github.com/dmachard/python-framestream/workflows/Testing/badge.svg) ![Build](https://github.com/dmachard/python-framestream/workflows/Build/badge.svg) ![Pypi](https://github.com/dmachard/python-framestream/workflows/PyPI/badge.svg)

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fstrm)

Frame Streams is a lightweight, binary-clean protocol that allows for the transport of arbitrarily encoded data payload sequences with minimal framing overhead.

This package provides a pure Python implementation based on https://github.com/farsightsec/fstrm/.

## Installation

This module can be installed from [pypi](https://pypi.org/project/fstrm/) website

```python
pip install fstrm
```

## Example

The example shows how to read raw data and decode-it with the fstrsm library.

```python
import fstrm
import asyncio

def do_something(payload):
    print(payload)
    
async def on_connect():
    f = fstrm.FstrmCodec()

    while data := await reader.read(f.pending_nb_bytes()) 
        f.append(data=data)
        
        # process the buffer and check if we have enough data
        if f.process():
            # a frame is complete, decode-it
            ctrl, ct, payload  = f.decode()
            
            # data frame ?
            if ctrl == fstrm.FSTRM_DATA_FRAME:
                do_something(data=payload)
                
            # or control frame for handshake ?
            if ctrl == fstrm.FSTRM_CONTROL_READY:
                accept = f.encode(ctrl=fstrm.FSTRM_CONTROL_ACCEPT, ct=[b"protobuf:dnstap.Dnstap"])
                writer.write(accept)
                await writer.drain()
                    
            # control frame ?
            if ctrl == fstrm.FSTRM_CONTROL_START:
                pass
                
            # control frame ?
            if ctrl == fstrm.FSTRM_CONTROL_STOP:
                f.reset()  

loop = asyncio.get_event_loop()                
coro = asyncio.start_server(on_connect, '127.0.0.1', 8888, loop=loop)
server = loop.run_until_complete(coro)
loop.close()
```


