pydantic_socket - Simple socket client and server with Pydantic models
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.
Metadata
Release files for pydantic-socket 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_socket-0.1.1.tar.gz | 7.9 kB | Without provenance | Details for pydantic_socket-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_socket-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for pydantic_socket-0.1.1-py3-none-any.whl |
Total release size: 19.8 kB
Release files / pydantic_socket-0.1.1.tar.gz
| Download URL | pydantic_socket-0.1.1.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08a08194dd481bcee415a10af05abe98a7f47c116fed65b4ba31a06f085fd08b
|
|
BLAKE2b-256 checksum How to use checksums |
48d25aed07511ff65dde49cbe7f95b6c98f368bcd91a76fc53cf95af0c92b790
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.11.0 Linux/5.15.0-1034-azure
|
Release files / pydantic_socket-0.1.1-py3-none-any.whl
| Download URL | pydantic_socket-0.1.1-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
08dc9a3c302575ddaaa43b02a4d2eb975a4b95e1c4c8647f145b43fb2a802fa4
|
|
BLAKE2b-256 checksum How to use checksums |
ea794e0b2faa21bb62255f39f55287b29cd733a4633296ba2962457994d1eb7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.11.0 Linux/5.15.0-1034-azure
|
Security for pydantic-socket 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.