Skip to main content

Packet system for python TCP sockets.

Project description

coolpackets

A python framework for sending and receiving custom packets

This is used for an extension to Ableton Live. I will add features as I need them for that project.

Usage example:

import socket
import time
from typing import Union, Optional, List

from coolpackets.packet import Packet, Connection, PacketManager


class MidiPacket(Packet):
    _packet_group = "mrs"
    _alias = "\x00"

    a = int
    b = Union[int, float]
    c_is_optional = Optional[int]

    def on_recv(self):
        print("RECEIVED MidiPacket!", self.a, self.b, self.c_is_optional)
        self.respond(UselessPacket(cool=["1", "2", "3"]))


class UselessPacket(Packet):
    _alias = "useless"
    # _dump = False

    cool = List[str]

    def on_recv(self):
        print("RECEIVED UselessPacket!")

    def encode(self) -> bytes:
        return b""

    @classmethod
    def decode(cls, data: bytes):
        return cls(cool=["nice", "cool"])


PacketManager.dump_packets("./packet_dumps/demo_packet_dump.py")


# setup
srv = socket.socket()
srv.bind(('', 9056))
srv.listen(1)

cli = socket.socket()
cli.connect(('localhost', 9056))
peer, addr = srv.accept()

conn1 = Connection(cli)
conn2 = Connection(peer, addr, {'mrs'})

# main logic
pck = MidiPacket(a=3, b=7.4)
conn1.send(pck, lambda p: print(f"RECEIVED RESPONSE: {p}"))

# time to process packets. Usually we wouldn't just close the sockets
time.sleep(.1)

# cleanup
conn1.close()
conn2.close()
srv.close()

print("Done")

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

coolpackets-0.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

coolpackets-0.0.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file coolpackets-0.0.2.tar.gz.

File metadata

  • Download URL: coolpackets-0.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for coolpackets-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4643eb8922864087ff603742440ded26393e96290e128272aa07e823e793d2ca
MD5 e099494fa3a095a4f462eb1f8902dd29
BLAKE2b-256 12b02dfe2705052994a596af556b3910a7a10304f0740bbe36d37ac285a8ac74

See more details on using hashes here.

File details

Details for the file coolpackets-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: coolpackets-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for coolpackets-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b9548705165509ea56f75457d63e72d5187ad0a5f471e6dd5db317763b246754
MD5 23c5d60206a9e02be819940904ac912e
BLAKE2b-256 288ca4fdf6d4dddb982a9fab773840a7bb400102e232a642e5b8f3339c952e63

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