Skip to main content

Library to simplify working with socket

Project description

Welcome to PySocketLib quick start

You will create simple echo-server

Creating project

Create files with following structure:

.
├── Client
│   └── Main.py
└── Server
    └── Main.py

Server

First, create class EchoServer, which is overloading TCPServer:

from PySocketLib.Server import TCPServer
from PySocketLib.Client import ConnectedClient
import socket

class EchoServer(TCPServer):
    def connect_client(self):
        ...

    def disconnect_client(self):
        ...

    def on_receive(self):
        ...

    def on_send(self):
        ...

if __name__ == '__main__':
    server = EchoServer(port=5678)
    
    while True:
        server.proceed()

Methods: connect_client - Actions to do when client connects, disconnect_client - Actions to do when client disconnects, on_receive - Actions to do when server receives message from client, on_send - Actions to do when server sends message to client,

Added some functionality:

from PySocketLib.Server import TCPServer
from PySocketLib.Client import ConnectedClient
import socket

class EchoServer(TCPServer):
    client_messages = dict()
    
    def connect_client(self, sock: socket.socket) -> ConnectedClient:
        new_client = super().connect_client(sock)
        print(f'[INFO] Connected client with address: {new_client.addr}')

    def disconnect_client(self, client: ConnectedClient):
        print(f'[INFO] Disconnect client with address: {client.addr}')
        return super().disconnect_client(client)

    def on_receive(self, data: bytes, client: ConnectedClient):
        self.client_messages[client] = data
        print(f'[INFO] Received {data}')
        return super().on_receive(data, client)
    
    def on_send(self, client: ConnectedClient):
        message = self.client_messages.get(client)
        if (message != None):
            print(f'[INFO] Sent data {message}')
            self.client_messages[client] = None
            return message
        else:
            return b''

if __name__ == '__main__':
    server = EchoServer(port=5678)
    
    while True:
        server.proceed()

Client

Let's create simple client, which input something from user and send it to server:

from PySocketLib.Client import Client

class MyClient(Client):
    def send(self, data: bytes):
        data = input('You: ')

        return super().send(data)
    
    def on_receive(self, data: bytes):
        print('Server: ' + str(data))

        return super().on_receive(data)
    
if __name__ == '__main__':
    cl = MyClient(hostname='127.0.0.1', port=5678)
    while True:
        cl.send(b'')
        cl.proceed()

Result

Server output:

[INFO] Connected client with address: ('127.0.0.1', 39652)
[INFO] Received b'Test message'
[INFO] Sent data b'Test message'
[INFO] Received b'1234'
[INFO] Sent data b'1234'
[INFO] Received b'Hi'
[INFO] Sent data b'Hi'
[INFO] Disconnect client with address: ('127.0.0.1', 39652)
^C

Client output:

You: Test message
Server: b'Test message'
You: 1234
Server: b'1234'
You: Hi
Server: b'Hi'
You: ^C

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

PySocketLib-0.1.1.tar.gz (5.6 kB view hashes)

Uploaded Source

Built Distribution

PySocketLib-0.1.1-py3-none-any.whl (6.8 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