Improve example code snippets

master
Viktor Villainov 2019-02-04 23:50:19 -05:00
parent acee746783
commit 4ba5c04e90
No known key found for this signature in database
GPG Key ID: 8EB38B46F33BAF2F
2 changed files with 20 additions and 16 deletions

View File

@ -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

View File

@ -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