A unified Python SDK for P2P networking with integrated DHT and NAT capabilities
Project description
Lattica Python SDK
A unified Python SDK for P2P networking with integrated DHT and NAT capabilities, built on top of libp2p.
Features
- Distributed Hash Table (DHT): Store and retrieve key-value pairs across the network
- Remote Procedure Call (RPC): Execute remote functions with support for complex data types
- Streaming RPC: Handle large data transfers with streaming capabilities
- NAT Traversal: Automatic NAT traversal with UPnP support
- Peer Discovery: mDNS and rendezvous-based peer discovery
- High Performance: Built with Rust for optimal performance
Installation
You can install the released versions
pip install lattica
Or you can install from source using:
pip install "git+https://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@github.com/OpenEdgeHQ/p2p-lattica.git#subdirectory=bindings/python" --force-reinstall
Quick Start
Basic Usage
from lattica import Lattica
# Create a Lattica instance
lattica = Lattica.builder().build()
# Get your peer ID
peer_id = lattica.peer_id()
print(f"My peer ID: {peer_id}")
Examples
1. DHT Operations
The DHT example demonstrates basic key-value storage and retrieval with subkey support.
from lattica import Lattica
# Create client1 as bootstrap node
lattica = Lattica.builder()
.build()
# Create client2 with bootstrap nodes
lattica = Lattica.builder() \
.with_bootstraps(["/ip4/127.0.0.1/tcp/54282/p2p/QmServerPeerId"]) \
.build()
# Store a simple key-value pair with default expiration 10 minute
lattica.store("name", "alice")
# get the value
result = lattica.get("name")
if result:
print(f"Value: {result.value}")
print(f"Expires: {result.expiration_time}")
# Store values with subkeys (useful for voting, user data, etc.)
key = "peer_list"
peers = ["alice", "bob", "carol"]
# Each peer stores their vote with a subkey
lattica.store(key, "yes", expiration_time, subkey="alice")
lattica.store(key, "no", expiration_time, subkey="bob")
lattica.store(key, "maybe", expiration_time, subkey="carol")
# get all votes
votes_result = lattica.get(key)
if votes_result:
for peer, vote_info in votes_result.value.items():
print(f"{peer}: {vote_info.value}")
2. RPC Operations
The RPC example demonstrates remote procedure calls with support for complex data types and streaming.
from lattica import Lattica, rpc_method, rpc_stream, rpc_stream_iter, ConnectionHandler
class MyService(ConnectionHandler):
@rpc_method
def add(self, a: int, b: int) -> int:
"""Simple addition"""
return a + b
@rpc_stream
def process_data(self, data: list) -> list:
return data
@rpc_stream_iter
def stream_rpc_iter(self):
while True:
text = "hello world"
yield text
# Create client1 as RPC server and bootstrap node
lattica = Lattica.builder()
.build()
service = MyService(lattica)
# Create client2 with bootstrap nodes
lattica = Lattica.builder() \
.with_bootstraps(["/ip4/127.0.0.1/tcp/54282/p2p/QmServerPeerId"]) \
.build()
client_service = MyService(client_lattica)
# Make RPC calls
stub = client_service.get_stub(server_peer_id)
result = stub.add(10, 20) # Returns 30
# Handle complex data types
num_floats = int(2 * 1024 * 1024 * 1024) // 8 #2GB
test_data = [random.random() for _ in range(num_floats)]
result = stub.process_data(test_data)
# stream iter call
for text in stub.stream_rpc_iter():
print(f"recv: {text}")
Configuration
Builder Pattern
lattica = Lattica.builder() \
.with_bootstraps([
"/ip4/127.0.0.1/tcp/8080/p2p/QmBootstrap1",
"/ip4/127.0.0.1/tcp/8081/p2p/QmBootstrap2"
]) \
.with_listen_addrs(["/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/udp/0/quic-v1"])
.with_external_addrs(["/ip4/0.0.0.0/tcp/0"])
.with_mdns(True) \
.with_upnp(True) \
.build()
Configuration Options
with_bootstraps(nodes): Set bootstrap nodes for network discoverywith_listen_addrs(addrs): Set listening addresswith_mdns(enabled): Enable/disable mDNS peer discoverywith_upnp(enabled): Enable/disable UPnP NAT traversalwith_relay_servers(servers): Set relay servers for network relaywith_autonat(enabled): Enable/disable AutoNAT detect[need relay servers]with_dcutr(enabled): Enable/disable TCP/QUIC NAT travelsal[need relay servers]with_external_addrs(addrs): Set external addresswith_storage_path: Persistent storage pathwith_key_path: Set Keypair path
Development
Building from Source
# Install maturin
pip install maturin
# Build the package
cd bindings/python
pip install .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lattica-1.0.10-cp311-abi3-win_amd64.whl.
File metadata
- Download URL: lattica-1.0.10-cp311-abi3-win_amd64.whl
- Upload date:
- Size: 7.7 MB
- Tags: CPython 3.11+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf681aa63ea8cbcf3298f736235fbea71c4bf88671ac2938dd6be77f7b2984b5
|
|
| MD5 |
6953c08a9b154fce9ccba7b14b48a9da
|
|
| BLAKE2b-256 |
6762cfbdf344faa0e00e973c2ae0dd30e5acd3b201aec709c055934f0006fe3f
|
File details
Details for the file lattica-1.0.10-cp311-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: lattica-1.0.10-cp311-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 10.7 MB
- Tags: CPython 3.11+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bb3dddb13a24b0118cbc35a2e1b63f41ddd7ff54e0ea33aad1e856d24f8324c
|
|
| MD5 |
f69162d717d9300a8a78f64a494ef9fa
|
|
| BLAKE2b-256 |
baab400e58ada89fd877f6e2c22afba5d8389565bed76a84ebc43ce462033088
|
File details
Details for the file lattica-1.0.10-cp311-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: lattica-1.0.10-cp311-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.2 MB
- Tags: CPython 3.11+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2d242c9e338b3da6ec0e621344bdcffc99451966bd21e7efae93e8612fbce3c
|
|
| MD5 |
0f3a866221a05b7a6d35adfb2d797236
|
|
| BLAKE2b-256 |
f46cd726bdac578aa38dbceaf0ba9cecf2518d77841d1dfe9c0206a91acb056e
|
File details
Details for the file lattica-1.0.10-cp311-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: lattica-1.0.10-cp311-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 8.4 MB
- Tags: CPython 3.11+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a40e499ff25e7c922f7811123d20060e2af91bd1c41cbcb2e9b98f262bf00a67
|
|
| MD5 |
c10ff5e8c049f13d6f5f39d071ee9d88
|
|
| BLAKE2b-256 |
4aa52d31d69ef94ee7a0b679b48c2cbf3cf24bfcaea8a3be60fc82d019927d7d
|