Skip to main content

Async Python client for the I2P SAM (Simple Anonymous Messaging) protocol.

Project description

i2psam

Async Python client for the I2P SAM (Simple Anonymous Messaging) protocol.

i2psam provides:

  • connection management with retry/backoff
  • SAM handshake, session creation, and command helpers
  • stream connect/accept/forward helpers
  • datagram send/receive helpers
  • optional package logging

Requirements

  • Python >=3.10
  • Running I2P router with SAM enabled
    • default SAM TCP port: 7656
    • default SAM UDP port: 7655

Installation

From pip

pip install i2psam

For UV

uv add i2psam

From source:

pip install .

For development:

pip install -e .

Quick Start

import asyncio
from i2psam import SAMClient


async def main():
    sam = SAMClient()

    # Ask SAM bridge to generate destination keys
    keys = await sam.dest_generate()
    print("Public destination:", keys.pub)

    # Create STREAM session
    async with sam.session("my-session", destination=keys.priv) as sess:
        print("Session created:", sess.session_id)


asyncio.run(main())

Configuration

from i2psam import SAMClient, SAMConfig

cfg = SAMConfig(
    host="127.0.0.1",
    port=7656,
    udp_port=7655,
    connection_timeout=10,
    connection_retries=3,
    exponential_backoff=False,
)

sam = SAMClient(cfg)

STREAM Example

Accept inbound connection

async with sam.session("listener", destination=keys.priv) as sess:
    async with sess.accept_stream() as (peer_dest, conn):
        data = await conn.read_line()
        print("from", peer_dest, data)

Connect to peer

peer_dest = "<peer public destination>"

async with sam.session("dialer", destination="TRANSIENT") as sess:
    async with sess.connect_stream(peer_dest) as conn:
        await conn.write("hello from i2psam")

Full chat example: examples/chat.py

DATAGRAM Example

async with sam.session("dgram", destination=keys.priv) as sess:
    async with sess.create_datagram() as dg:
        dg.sendto("<peer destination>", b"ping")
        sender, payload = await dg.recv()
        print(sender, payload)

Low-level Command API

You can send command models directly:

from i2psam import SAMClient
from i2psam.commands.commands import HelloVersion

reply = await SAMClient().request(HelloVersion(MIN="3.0", MAX="3.3"))
print(reply.version)

Or send raw SAM lines:

raw = await SAMClient().raw_request("HELLO VERSION MIN=3.0 MAX=3.3")
print(raw)

Logging

Library logging is silent by default (NullHandler).

Enable logs:

from i2psam import configure_logging

configure_logging("DEBUG")

Create named logger if needed:

from i2psam import get_logger

log = get_logger("custom")
log.info("hello")

Errors

Common exceptions:

  • SAMError
  • SAMClientError
  • SAMSessionError
  • SAMStreamError
  • SAMDatagramError

Use them to handle bridge/network/protocol failures.

Running Tests

pytest -q

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

i2psam-1.0.0.tar.gz (33.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

i2psam-1.0.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file i2psam-1.0.0.tar.gz.

File metadata

  • Download URL: i2psam-1.0.0.tar.gz
  • Upload date:
  • Size: 33.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for i2psam-1.0.0.tar.gz
Algorithm Hash digest
SHA256 750671226e1387a283cee85d0f45e0b93e3d8b0cb19a894758bb503050f0528d
MD5 2dde624c68b1bd1cfbad884d0a02d2e3
BLAKE2b-256 077ba2309019209e10d2683b5f419cac3b05092836cddcb2448a74a5b043fdba

See more details on using hashes here.

Provenance

The following attestation bundles were made for i2psam-1.0.0.tar.gz:

Publisher: release.yml on AstralMortem/i2psam

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file i2psam-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: i2psam-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for i2psam-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53a3821e5115b55ca2165ae8c10ea04e3beecd33764d72f8e33374113a1a6d80
MD5 64faa624693a5642906b41a5f0601f7c
BLAKE2b-256 98d2a886a09c271a35d24d885635ab7fac77f9c0277c0d8cbb0f8c875a8085be

See more details on using hashes here.

Provenance

The following attestation bundles were made for i2psam-1.0.0-py3-none-any.whl:

Publisher: release.yml on AstralMortem/i2psam

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page