Skip to main content

Frame Streams implementation in Python

Project description

Testing Build Pypi

Frame Streams implementation in Python

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

python 3.12.x python 3.11.x python 3.10.x python 3.9.x python 3.8.x

This module can be installed from pypi website

pip install fstrm

Example

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

import fstrm
import asyncio

class FstrmServerProtocol(asyncio.Protocol):
    def __init__(self, handshake, data_recv, content_type=b"plaintext"):
        self.fstrm = fstrm.FstrmCodec()
        self.content_type = content_type
        self.data_recv = data_recv
        self.handshake = handshake
        self.handshake_accept_done = False

    def connection_made(self, transport):
        self.transport = transport

    def data_received(self, data):
        if not self.handshake.done():
            if not self.handshake_accept_done:
                if self.fstrm.is_ctrlready(data):
                    self.transport.write(self.fstrm.encode_ctrlaccept(self.content_type))
                    self.handshake_accept_done = True
            else:
                if self.fstrm.is_ctrlstart(data):
                    self.handshake.set_result(True)
        else:
            payload = self.fstrm.is_data(data)
            # do someting with the payload...
            if payload:
                self.data_recv.set_result(payload)        

class FstrmClientProtocol(asyncio.Protocol):
    def __init__(self, handshake, content_type=b"plaintext"):
        self.fstrm = fstrm.FstrmCodec()
        self.content_type = content_type
        self.handshake = handshake
        self.transport = None

    def connection_made(self, transport):
        self.transport = transport
        self.transport.write(self.fstrm.encode_ctrlready(self.content_type))

    def data_received(self, data):
        if not self.handshake.done():
            if self.fstrm.is_ctrlaccept(data):
                self.transport.write(self.fstrm.encode_ctrlstart(self.content_type))
                self.handshake.set_result(True)

    def send_data(self, data):
        payload = self.fstrm.encode_data(data)
        self.transport.write(payload)

async def run(loop):
    # Create server and client
    data_recv = loop.create_future()
    hanshake_server = loop.create_future()
    server = await loop.create_server(lambda: FstrmServerProtocol(hanshake_server, data_recv), 'localhost', 8000)

    hanshake_client = loop.create_future()
    transport, client =  await loop.create_connection(lambda: FstrmClientProtocol(hanshake_client), 'localhost', 8000)

    # check handshake
    try:
        await asyncio.wait_for(hanshake_server, timeout=5)
    except asyncio.TimeoutError:
        raise Exception("handshake server failed")

    try:
        await asyncio.wait_for(hanshake_client, timeout=0.5)
    except asyncio.TimeoutError:
       raise Exception("handshake client failed")

    # ok, the client send some data
    data = b"some data..."
    client.send_data(data)

    # wait data on server side
    try:
        await asyncio.wait_for( data_recv, timeout=0.5)
    except asyncio.TimeoutError:
        raise Exception("data expected but failed")

    # Shut down server and client
    server.close()
    transport.close()

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(loop))

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

fstrm-0.6.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

fstrm-0.6.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file fstrm-0.6.1.tar.gz.

File metadata

  • Download URL: fstrm-0.6.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for fstrm-0.6.1.tar.gz
Algorithm Hash digest
SHA256 81bcb01ec14a37c2a74712796411b68f93e5afcc679e72315d39365fca671529
MD5 7ab4b700661e5aa4b5aeaf0e2f014614
BLAKE2b-256 3f9429fb167a5c03a583b56d8402e42c0994a3fb010ebd23f857690ca153b989

See more details on using hashes here.

File details

Details for the file fstrm-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: fstrm-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for fstrm-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 448376be491ef9c4600b397d6d56d01cf6a4bf58256c8688f93c5efa856216f9
MD5 0c435efe1a7bcb921ee1bb5514997a93
BLAKE2b-256 7e77a7f01189ae8c55650798b918172d3cf456cf038bff48f0ab388163df08d2

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