Client-side toolkit for async sockets.
Project description
Simple example
Reading from a socket
# reading.py
import asyncio
from asockit import AsyncioReadableConnection, ConnectionClosedError, SocketReader
class SocketReaderDelegate:
def on_message(self, message: str) -> None:
print(f'[SocketReaderDelegate] Received message "{message}"')
async def main() -> None:
stream_reader, _ = await asyncio.open_connection("localhost", port=3000)
reader = SocketReader(
connection=AsyncioReadableConnection(reader=stream_reader)
)
delegate = SocketReaderDelegate()
reader.set_delegate(delegate)
try:
await reader.start()
except ConnectionClosedError:
print("The connection has closed.")
if __name__ == "__main__":
asyncio.run(main())
$ nc -lnp 3000 -c 'echo -n "Hello\nWorld!\n"'
# In a different shell session
$ python3 reading.py
[SocketReaderDelegate] Received message "Hello"
[SocketReaderDelegate] Received message "World!"
The connection has closed.
Writing to a socket
# writing.py
import asyncio
from asockit import AsyncioWritableConnection, SocketWriter
async def main() -> None:
_, stream_writer = await asyncio.open_connection("localhost", port=3000)
writer = SocketWriter(
connection=AsyncioWritableConnection(writer=stream_writer)
)
await writer.write("Hello world!\n")
if __name__ == "__main__":
asyncio.run(main())
$ python3 writing.py
# Started before running the script
$ nc -lvnp 3000
listening on [any] 3000 ...
connect to [127.0.0.1] from (UNKNOWN) [127.0.0.1] 41560
Hello world!
Installation
Asockit is available as asockit
on PyPI:
pip install asockit
Usage
For detailed quickstart and API reference, visit the Documentation.
License
Copyright (C) 2023 tombartk
This program is free software: you can redistribute it and/or modify it under the terms
of the GNU Affero General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program.
If not, see https://www.gnu.org/licenses/.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
asockit-0.1.1.tar.gz
(22.7 kB
view details)
Built Distribution
asockit-0.1.1-py3-none-any.whl
(18.7 kB
view details)
File details
Details for the file asockit-0.1.1.tar.gz
.
File metadata
- Download URL: asockit-0.1.1.tar.gz
- Upload date:
- Size: 22.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a274baf4ddda5fc2aa1132ca0a084603e07fc3947385eea8873658c342a1fa4c |
|
MD5 | 3c35a63c3ba619bec367f38b1310db74 |
|
BLAKE2b-256 | 9431e61670d1d0c4782a3480f5e1ea020dd7d52e5404ba789a86e830718fa35d |
File details
Details for the file asockit-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: asockit-0.1.1-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b800f10860e38989ed6726f055a1e0a21cd1f3ceeb12e07332b27ef01a47feda |
|
MD5 | 6bfa0393f9881b7c59cfce25f0f5e410 |
|
BLAKE2b-256 | 71349b18c42ad1467bf186a3deaee7ecf8084eb7c15dacab7cc2e6d60f0ed44a |