README: update for new Python API

This commit is contained in:
Adrien Béraud committed 2025-09-02 23:51:36 -04:00
1 parent cda5b0afa2
commit b45f32e8f8
2 files changed
+79 -8

No files matched your search

+39 -3
View File
@@ -13,15 +13,23 @@ OpenDHT is a lightweight Distributed Hash Table implementation providing an easy
Every node in the network can read and write values to the store. Values are distributed over the network with redundancy.
## Install
Install on Linux, macOS and Windows with:
```sh
pip install opendht
```
## Quick Example
Using the simple, blocking API:
```python
import opendht as dht
node = dht.DhtRunner()
node.run()
# Join the network through any running node,
# Join the DHT network through any running node,
# here using a known bootstrap node.
node.bootstrap("bootstrap.jami.net", "4222")
@@ -29,8 +37,36 @@ node.bootstrap("bootstrap.jami.net", "4222")
node.put(dht.InfoHash.get("unique_key"), dht.Value(b'some binary data'))
results = node.get(dht.InfoHash.get("unique_key"))
for r in results:
print(r)
for value in results:
print(value)
```
Or using asyncio:
```python
import opendht.aio as dht
async with dht.DhtRunner() as node:
node.bootstrap("bootstrap.jami.net", "4222")
# put data, waiting for completion
await node.put(dht.InfoHash.get("unique_key"), dht.Value(b'tata data'))
# get all values at key
results = await node.getAll(dht.InfoHash.get("unique_key"))
for value in results:
print(value)
# same operation, but stream values as they come from the network
with node.get(dht.InfoHash.get("unique_key")) as results:
async for value in results:
print(value)
# listen for changes of values at key
with node.listen(dht.InfoHash.get("unique_key")) as values:
async for value, expired in values:
print(value)
if value.data == b'tata data':
break
```
## Documentation