Quickstart
Reading from a socket¶
Define the Delegate¶
A SocketReaderDelegate
is the receiver of lines that the reader will read. It's a protocol, so no need for subclassing anything:
class MySocketReaderDelegate:
def on_message(self, message: str) -> None:
print(f'[SocketReaderDelegate] Received message "{message}"')
Open a Connection¶
Create a ReadableConnection
using the AsyncioReadableConnection
, which is a handy wrapper over the asyncio.StreamReader
:
import asockit
async def main() -> None:
stream_reader, _ = await asyncio.open_connection("localhost", port=3000)
connection = asockit.AsyncioReadableConnection(reader=stream_reader)
Create the Reader¶
A SocketReader
reads text lines from the ReadableConnection
and "feeds" them to the SocketReaderDelegate
:
import asockit
async def main() -> None:
...
reader = asockit.SocketReader(connection=connection)
delegate = MySocketReaderDelegate()
reader.set_delegate(delegate)
Start reading¶
Await the start
coroutine to start reading lines until the stop
coroutine is awaited, or the connection closes.
import asockit
async def main() -> None:
...
try:
await reader.start()
except asockit.ConnectionClosedError:
print("The connection has closed.")
$ nc -lnp 3000 -c 'echo "Hello\nWorld!\n"'
# In a different shell session
$ python3 main.py
[SocketReaderDelegate] Received message "Hello"
[SocketReaderDelegate] Received message "World!"
The connection has closed.
Writing to a socket¶
Open a Connection¶
Open a WritableConnection
by creating an AsyncioWritableConnection
, which is a useful wrapper over the asyncio.StreamWriter
:
import asockit
async def main() -> None:
_, stream_writer = await asyncio.open_connection("localhost", port=3000)
connection = asockit.AsyncioWritableConnection(writer=stream_writer)
Create the Writer¶
A SocketWriter
writes text lines to the WritableConnection
:
import asockit
async def main() -> None:
...
writer = asockit.SocketWriter(connection=connection)
Write a line¶
Pass your payload to the write
coroutine to send it over the connection:
async def main() -> None:
...
await writer.write("Hello world!\n")
$ python3 main.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!
To learn more, see the API Documentation.