Skip to main content

Client-side toolkit for async sockets.

Project description

Logo

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

AGPLv3

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


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 hashes)

Uploaded Source

Built Distribution

asockit-0.1.1-py3-none-any.whl (18.7 kB view hashes)

Uploaded Python 3

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