You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i2plib is a modern asynchronous library for building I2P applications.
Installing
pip install i2plib
Requirements:
Python version >= 3.5
I2P router with SAM API enabled
Connecting to a remote I2P destination
importasyncioimporti2plibasyncdefconnect_test(destination):
session_name="test-connect"# create a SAM stream sessionawaiti2plib.create_session(session_name)
# connect to a destinationreader, writer=awaiti2plib.stream_connect(session_name, destination)
# write data to a socketwriter.write(b"PING")
# asynchronously receive datadata=awaitreader.read(4096)
print(data.decode())
# close the connectionwriter.close()
# run event looploop=asyncio.get_event_loop()
loop.run_until_complete(connect_test("dummy.i2p"))
loop.stop()
Accept connections in I2P
importasyncioimporti2plibasyncdefaccept_test():
session_name="test-accept"# create a SAM stream sessionawaiti2plib.create_session(session_name)
# accept a connectionreader, writer=awaiti2plib.stream_accept(session_name)
# first string on a client connection always contains clients I2P destinationdest=awaitreader.readline()
remote_destination=i2plib.Destination(dest.decode().strip())
# read for the actual incoming data from the clientdata=awaitreader.read(4096)
print(data.decode())
# send data backwriter.write(b"PONG")
# close the connectionwriter.close()
# run event looploop=asyncio.get_event_loop()
loop.run_until_complete(accept_test())
loop.stop()
Server tunnel
Expose a local service to I2P like that:
importasyncioimporti2plibloop=asyncio.get_event_loop()
# making your local web server available in the I2P networktunnel=i2plib.ServerTunnel(("127.0.0.1", 80))
asyncio.ensure_future(tunnel.run())
try:
loop.run_forever()
exceptKeyboardInterrupt:
passfinally:
loop.close()
Client tunnel
Bind a remote I2P destination to a port on your local host: