asockit 0.1.1

Last updated:

0 purchases

asockit 0.1.1 Image
asockit 0.1.1 Images
Add to Cart

Description:

asockit 0.1.1

Client-side toolkit for async sockets.

Documentation


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/.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.