Library to work with socket clients and servers.
Project description
SocketLib
Helper library to implement socket client and servers as well as services to process, store, or send data received or send trough sockets.
Installation
Create a virtual environment and activate it:
python3.11 -m venv venv
source venv/bin/activate
Install the latest version:
pip install pysocklib
Contents
Client and Servers
This package includes different socket clients and servers. The following classes are included:
-
ClientReceiver: A client that receives messages from a server
-
ClientSender: A client that sends data to a server
-
Client: A client that receives and sends data simultaneously to a server
-
ServerReceiver: A server that receives messages from a client
-
ServerSender: A server that sends data to a client
-
Server: A server that receives and sends data simultaneously to a client.
Services
This module main class is the AbstractService. This abstract base class is a blueprint to easily create other services that communicate with each other trough queues. Very useful for processing, storing, etc. the data received trough sockets.
Examples
Sample usage of a client that sends receives data from a server. The client.py
program
will use a custom MessageLogger
service to log all the data it receives, while the
server.py
program whill use a service MessageGenerator
to generate messages continuously
and send them to the client.
# client.py
from socketlib import ClientReceiver
from socketlib.services.samples import MessageLogger
from socketlib.utils.logger import get_module_logger
if __name__ == "__main__":
address = ("localhost", 12345)
client = ClientReceiver(address, reconnect=True)
logger = get_module_logger(__name__, "dev")
msg_logger = MessageLogger(client.received, logger)
with client:
client.connect()
client.start()
msg_logger.start()
try:
client.join()
except KeyboardInterrupt:
client.shutdown()
msg_logger.shutdown()
# server.py
from socketlib import ServerSender
from socketlib.services.samples import MessageGenerator
if __name__ == "__main__":
address = ("localhost", 12345)
server = ServerSender(address)
msg_gen = MessageGenerator(server.to_send)
with server:
server.start()
msg_gen.start()
try:
server.join()
except KeyboardInterrupt:
server.shutdown()
msg_gen.shutdown()
Developing
Developed in Python 3.11.4
Installing the development environment:
git clone https://github.com/Daniel-Ibarrola/MServ
python3.11 -m venv venv
source venv/bin/activate
pip install -r requirements/dev.txt
pip install -e .
License
pysocklib
was created by Daniel Ibarrola. It is licensed under the terms
of the MIT license.
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
Built Distribution
Hashes for pysocklib-0.3.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a078d2a529b8e05944af8beabff0e2b902a03403c1026668d3ce04cea94bd172 |
|
MD5 | a4eb963e2dd0a99ffb18797b2c3126f5 |
|
BLAKE2b-256 | 770e0b879a797c771369d3833fddda4365546968dd1893df0f2b74a3a81e53c4 |