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/.
Release files for asockit 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 | |
|---|---|---|---|
| asockit-0.1.1.tar.gz | 22.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asockit-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 41.4 kB
Release files / asockit-0.1.1.tar.gz
| Download URL | asockit-0.1.1.tar.gz |
|---|---|
| Size | 22.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a274baf4ddda5fc2aa1132ca0a084603e07fc3947385eea8873658c342a1fa4c
|
|
BLAKE2b-256 checksum How to use checksums |
9431e61670d1d0c4782a3480f5e1ea020dd7d52e5404ba789a86e830718fa35d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Release files / asockit-0.1.1-py3-none-any.whl
| Download URL | asockit-0.1.1-py3-none-any.whl |
|---|---|
| Size | 18.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b800f10860e38989ed6726f055a1e0a21cd1f3ceeb12e07332b27ef01a47feda
|
|
BLAKE2b-256 checksum How to use checksums |
71349b18c42ad1467bf186a3deaee7ecf8084eb7c15dacab7cc2e6d60f0ed44a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|