Skip to main content

A lightweight WebSocket-based communication library

Project description

Lazy Socket

An attempt at creating a websocket based communication system that handles all the threading and async stuff in the background.

Installation

pip install lazy-socket

Usage

Server

The server can be created by subclassing LazyServer and implementing the process_message method. This is an async method that will be called whenever a message is received from a client. To respond to the client, use the send method of the client object passed to the method. A list of clients is available via the clients attribute of the server.

import asyncio
from lazy_socket.server import LazyServer


class Server(LazyServer):

    async def process_message(self, client, message):
        await client.send(f"Received: {message}")

if __name__ == "__main__":
    server = Server(name="TestServer", host="0.0.0.0", port=5000, version="1.0")

    try:
        server.start()
    except KeyboardInterrupt:
        print("Shutting down server...")

Client

The client can be created by instantiating the LazyClient class. The client has a queue attribute that can be used to receive messages from the server. The client must be started using the start method. Messages can be sent to the server using the send method.

A message lazy_client:connected:{uri} is sent to the queue when the client successfully connects to the server.

from lazy_socket.client import LazyClient

client = LazyClient()
client.start()

while True:
    if client.queue.empty():
        continue

    message = client.queue.get()
    if message.startswith("lazy_client:connected:"): 
        client.send("Hello, Lazy Server!")

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

lazy_socket-0.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazy_socket-0.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file lazy_socket-0.0.1.tar.gz.

File metadata

  • Download URL: lazy_socket-0.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for lazy_socket-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e9042b410f97a3d952dffab9c0508a56f8f501ae0d1b20b33fcf777b3c6bb8b1
MD5 68a9ef8f5c0513c2089a9d342d6b8405
BLAKE2b-256 fd95dc2d153227d4c6c22d2c6d244ce132e848e30d57614979419c31e87c6393

See more details on using hashes here.

File details

Details for the file lazy_socket-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: lazy_socket-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for lazy_socket-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 422396f244c4f07dd7a40e4859f7dc26e2cec06f1b5bda11a19c78b8d6fda014
MD5 875cdaff47c8e481d07fed271eeaac81
BLAKE2b-256 4de2dd75357f6b51c7b85eb177e92cbc1bb81c5d285016f4e6e9c8d95195cf3c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page