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
Release history Release notifications | RSS feed
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.1.tar.gz
(9.0 kB
view hashes)
Built Distribution
Close
Hashes for coolpackets-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a194c40da019e2c56fa40888abcd8547f264a73f6ff2c46b06d61147de030d00 |
|
MD5 | 00d76a61603b07405ba55aac9613dec4 |
|
BLAKE2b-256 | 4256ff6f9429fa4a808f76d2b715f6992e83da3044e210974ec01c5b441480e2 |