Skip to main content

Simple TCP socket client and server with Pydantic models

Project description

pydantic_socket - Simple socket client and server with Pydantic models

PyPI version shields.io PyPI pyversions PyPI license

Examples

Output examples are provided for simultaneous run of Server and Client examples

Server

import asyncio
import logging

import pydantic_socket
from pydantic_socket.types import Action
from pydantic_socket.websocket import (
    Server,
    ServerClient,
)

server = Server()


class TestResponse(pydantic_socket.types.BaseModel):
    foo: int = 1
    bar: str = "bar"
    input: str | None = None


# Register handler for certain action type    
@server.action_handler("hello")
async def hello_handler(s: Server, client: ServerClient, action: Action):
    logging.info(f"Hello, {action.payload.get('name', 'World')}!")
    # Will print:
    # INFO:root:Hello, pydantic_socket!


async def test_handler(s: Server, client: ServerClient, action: Action):
    return TestResponse(foo=1, bar="bar", input=action.payload.get('input'))


# Another way to register handler for certain action type
server.set_action_handler("test", test_handler)

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    asyncio.run(server.run())

Client

import asyncio
import logging

import pydantic

from pydantic_socket.types import Action
from pydantic_socket.websocket import Client

client = Client("http://127.0.0.1:8080", auto_reconnect=False, client_id=1)


class SomeActionInputPayload(pydantic.BaseModel):
    input: str


async def main():
    # Establishing connection to server
    asyncio.create_task(client.run())
    # Wait some time until connection is done
    await asyncio.sleep(2)
    # Send action
    await client.send(Action(type='hello', payload={"name": "pydantic_socket"}))
    # Send and wait for response
    response = await client.request(Action(type='test', payload={"input": "Some Input"}))

    logging.info(f"Payload is: {response.payload}")
    # Will print: 
    # INFO:root:Payload is: {'foo': 1, 'bar': 'bar', 'input': 'Some Input'}

    await client.close()


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

LICENSE

This project 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

pydantic_socket-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

pydantic_socket-0.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_socket-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic_socket-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.11.0 Linux/5.15.0-1034-azure

File hashes

Hashes for pydantic_socket-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1ed0ba9cbb06d1100d7a7cbc871faf0ce21c656130d6e5e769fa0720410f9509
MD5 fd41318ec5cb2865abb0efe2f3038ebd
BLAKE2b-256 d0253451dcae399cc55ebdfe716c00840557d7430645d71076c464ff34e2bbcc

See more details on using hashes here.

File details

Details for the file pydantic_socket-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_socket-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.11.0 Linux/5.15.0-1034-azure

File hashes

Hashes for pydantic_socket-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5d940be140fef78a92758fefb64b61f8320f7c652accd426520bd3a0ca12d33d
MD5 4844be0cb323253038b1842e35d4fac7
BLAKE2b-256 8c5fae17d2eb7e9794fe703027b214a90de05925c297f448f95c92f983b96108

See more details on using hashes here.

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