Giter VIP home page Giter VIP logo

i2plib's Introduction

i2plib

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

import asyncio
import i2plib

async def connect_test(destination):
    session_name = "test-connect"

    # create a SAM stream session
    await i2plib.create_session(session_name)

    # connect to a destination
    reader, writer = await i2plib.stream_connect(session_name, destination)

    # write data to a socket
    writer.write(b"PING")

    # asynchronously receive data
    data = await reader.read(4096)
    print(data.decode())

# run event loop
loop = asyncio.get_event_loop()
loop.run_until_complete(connect_test("dummy.i2p"))
loop.stop()

Accept connections in I2P

import asyncio
import i2plib

async def accept_test():
    session_name = "test-accept"

    # create a SAM stream session
    await i2plib.create_session(session_name)

    # accept a connection
    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())

    # 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)

    print(data.decode())

    # send data to the client
    writer.write(b"PONG")
    writer.close()

# run event loop
loop = asyncio.get_event_loop()
loop.run_until_complete(accept_test())
loop.stop()

Server tunnel

Expose a local service to I2P like that:

import asyncio
import i2plib

loop = asyncio.get_event_loop()
# making your local web server available in the I2P network
tunnel = i2plib.ServerTunnel(("127.0.0.1", 80))
asyncio.ensure_future(tunnel.run())

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    loop.close()

Client tunnel

Bind a remote I2P destination to a port on your local host:

import asyncio
import i2plib

loop = asyncio.get_event_loop()
# bind irc.echelon.i2p to 127.0.0.1:6669
tunnel = i2plib.ClientTunnel("irc.echelon.i2p", ("127.0.0.1", 6669))
asyncio.ensure_future(tunnel.run())

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    loop.close()

More examples

You can see more demo applications in docs/examples directory of the source repository.

Resources

Aknowledgments

i2plib's People

Contributors

42b avatar l-n-s avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.