https://github.com/shahaf-f-s/socketsio
A python wrapper around socket for generalized communication protocols, unified socket interface, utility methods, and modular protocol swapping capeabilities. Including a socket based Pub/Sub system.
https://github.com/shahaf-f-s/socketsio
pubsub socket socket-io socket-programming
Last synced: 6 months ago
JSON representation
A python wrapper around socket for generalized communication protocols, unified socket interface, utility methods, and modular protocol swapping capeabilities. Including a socket based Pub/Sub system.
- Host: GitHub
- URL: https://github.com/shahaf-f-s/socketsio
- Owner: Shahaf-F-S
- Created: 2023-09-09T07:20:09.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-19T12:44:31.000Z (over 1 year ago)
- Last Synced: 2025-03-12T10:18:13.941Z (7 months ago)
- Topics: pubsub, socket, socket-io, socket-programming
- Language: Python
- Homepage:
- Size: 86.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# sockets-io
> A python wrapper around the builtin socket module, for generalized communication protocols, unified socket interface, utility methods, and modular protocol swapping capeabilities. Including a socket based Pub/Sub system.
## Installation
```
pip install sockets-io
```## examples
basic server socket threading based
```python
from socketsio import Server, Socket, BHP, TCP, UDPfrom looperation import Handler, Operator
def action(server: Server, client: Socket) -> None:
with Handler(
exception_handler=print,
cleanup_callback=lambda h: client.close()
):
while not (client.closed or server.closed):
received, address = client.receive()
if not received:
continue
print("server:", (received, address))
sent = (
f"server received from "
f"{address}: ".encode() + received
)
client.send(sent)HOST = "127.0.0.1"
PROTOCOL = 'TCP'
PORT = 5000if PROTOCOL == 'UDP':
protocol = UDP()elif PROTOCOL == 'TCP':
protocol = BHP(TCP())else:
raise ValueError(f"Invalid protocol type: {PROTOCOL}")server = Server(protocol)
server.bind((HOST, PORT))service = Operator(
operation=lambda: server.handle(action=action)
)
service.run()
```basic client socket
```python
from socketsio import Client, BHP, TCP, UDPHOST = "127.0.0.1"
PROTOCOL = 'TCP'
PORT = 5000if PROTOCOL == 'UDP':
protocol = UDP()elif PROTOCOL == 'TCP':
protocol = BHP(TCP())else:
raise ValueError(f"Invalid protocol type: {PROTOCOL}")client = Client(protocol)
client.connect((HOST, PORT))for _ in range(2):
client.send((", ".join(["hello world"] * 3)).encode())
print("client:", client.receive())```
pubsub server with authentication
```python
import time
import randomfrom looperation import Operator
from socketsio import Serverfrom socketsio.pubsub import DataStore, Data, SubscriptionStreamer, Authorization
IP = "127.0.0.1"
PORT = 5080DELAY = 0.00001
AUTHORIZED = [
{'name': 'abc', 'password': '123'}
]class Producer:
ACTION = "action"
BUY = "buy"
SELL = "sell"NAMES = ['AAPL', "AMZN", "GOOG", "TSLA", "META"]
BUY_DATA = {ACTION: BUY}
SELL_DATA = {ACTION: SELL}def next(self) -> Data:
return Data(
name=random.choice(self.NAMES),
data=random.choice((self.BUY_DATA, self.SELL_DATA)),
time=time.time()
)storage = DataStore()
producer = Producer()
screener = Operator(
operation=lambda: storage.insert(producer.next()),
delay=DELAY
)streamer = SubscriptionStreamer(
storage=storage,
authenticate=lambda controller, data: Authorization(data.data in AUTHORIZED),
on_unauthenticated=lambda controller, data: (time.sleep(0.5), controller.close()),
on_join=lambda controller: print(f"client connected: {controller.socket.address}"),
on_disconnect=lambda controller: print(f"client disconnected: {controller.socket.address}"),
)server = Server()
server.bind((IP, PORT))service = Operator(
operation=lambda: server.handle(
action=lambda _, socket: streamer.controller(
socket=socket, exception_handler=print
).run(send=True, receive=True, block=True)
),
termination=lambda: (
print("disconnecting server"),
server.close(),
print("server disconnected")
)
)screener.run(block=False)
service.run(block=True)
```pubsub client with authentication
```python
from looperation import Handler
from socketsio import Clientfrom socketsio.pubsub import ClientSubscriber, DataStore, Data
IP = "127.0.0.1"
PORT = 5080storage = DataStore()
client = Client()
client.connect((IP, PORT))subscriber = ClientSubscriber(socket=client, storage=storage)
subscriber.queue_socket.run(block=False)subscriber.authenticate({'name': 'abc', 'password': '123'})
print(Data.load(Data.decode(client.receive()[0])))
subscriber.subscribe(['AAPL', "AMZN", "GOOG"])
with Handler(
exception_callback=lambda h: client.close(),
exception_handler=print
):
while True:
print(subscriber.data())
```