Skip to main content

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

socketlib

This module 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 MessageGeneratorto 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
cd src
python setup.py develop

License

socketlib was created by Daniel Ibarrola. It is licensed under the terms of the MIT license.

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

pysocklib-0.2.0.tar.gz (10.7 kB view hashes)

Uploaded Source

Built Distribution

pysocklib-0.2.0-py3-none-any.whl (15.9 kB view hashes)

Uploaded Python 3

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