From 4ba5c04e90519e941cffb5010f3291af57a7ef9d Mon Sep 17 00:00:00 2001 From: Viktor Villainov Date: Mon, 4 Feb 2019 23:50:19 -0500 Subject: [PATCH] Improve example code snippets --- README.rst | 18 ++++++++++-------- docs/quickstart.rst | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/README.rst b/README.rst index df3040b..ef20a39 100644 --- a/README.rst +++ b/README.rst @@ -47,6 +47,9 @@ Connecting to a remote I2P destination data = await reader.read(4096) print(data.decode()) + # close the connection + writer.close() + # run event loop loop = asyncio.get_event_loop() loop.run_until_complete(connect_test("dummy.i2p")) @@ -70,19 +73,18 @@ Accept connections in I2P reader, writer = await i2plib.stream_accept(session_name) # first string on a client connection always contains clients I2P destination - incoming = await reader.read(4096) - dest, data = incoming.split(b"\n", 1) - remote_destination = i2plib.Destination(dest.decode()) + dest = await reader.readline() + remote_destination = i2plib.Destination(dest.decode().strip()) - # destination and data may come in one chunck, if not - we wait for the actual - # incoming data - if not data: - data = await reader.read(4096) + # read for the actual incoming data from the client + data = await reader.read(4096) print(data.decode()) - # send data to the client + # send data back writer.write(b"PONG") + + # close the connection writer.close() # run event loop diff --git a/docs/quickstart.rst b/docs/quickstart.rst index a7f98c3..fb5c442 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -37,6 +37,9 @@ Connecting to a remote I2P destination data = await reader.read(4096) print(data.decode()) + # close the connection + writer.close() + # run event loop loop = asyncio.get_event_loop() loop.run_until_complete(connect_test("dummy.i2p")) @@ -60,19 +63,18 @@ Accept connections in I2P reader, writer = await i2plib.stream_accept(session_name) # first string on a client connection always contains clients I2P destination - incoming = await reader.read(4096) - dest, data = incoming.split(b"\n", 1) - remote_destination = i2plib.Destination(dest.decode()) + dest = await reader.readline() + remote_destination = i2plib.Destination(dest.decode().strip()) - # destination and data may come in one chunck, if not - we wait for the actual - # incoming data - if not data: - data = await reader.read(4096) + # read for the actual incoming data from the client + data = await reader.read(4096) print(data.decode()) - # send data to the client + # send data back writer.write(b"PONG") + + # close the connection writer.close() # run event loop