librats 3.0.5.dev2__py3-none-macosx_11_0_universal2.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- librats-3.0.5.dev2.dist-info/METADATA +309 -0
- librats-3.0.5.dev2.dist-info/RECORD +21 -0
- librats-3.0.5.dev2.dist-info/WHEEL +6 -0
- librats-3.0.5.dev2.dist-info/licenses/LICENSE +21 -0
- librats-3.0.5.dev2.dist-info/top_level.txt +1 -0
- librats_py/__init__.py +48 -0
- librats_py/callbacks.py +52 -0
- librats_py/core.py +743 -0
- librats_py/ctypes_wrapper.py +274 -0
- librats_py/enums.py +181 -0
- librats_py/examples/__init__.py +3 -0
- librats_py/examples/basic_client.py +92 -0
- librats_py/examples/file_transfer.py +122 -0
- librats_py/examples/gossipsub_chat.py +125 -0
- librats_py/exceptions.py +85 -0
- librats_py/librats.dylib +0 -0
- librats_py/tests/__init__.py +3 -0
- librats_py/tests/conftest.py +31 -0
- librats_py/tests/test_abi_layout.py +90 -0
- librats_py/tests/test_client.py +173 -0
- librats_py/tests/test_integration.py +181 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: librats
|
|
3
|
+
Version: 3.0.5.dev2
|
|
4
|
+
Summary: Python bindings for librats P2P networking library
|
|
5
|
+
Author: librats contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/librats/librats/tree/master/python
|
|
8
|
+
Project-URL: Documentation, https://librats.com
|
|
9
|
+
Project-URL: Repository, https://github.com/librats/librats
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/librats/librats/issues
|
|
11
|
+
Keywords: p2p,networking,file-transfer,gossipsub,pubsub,dht,mdns,noise
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Operating System :: MacOS
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Internet
|
|
25
|
+
Classifier: Topic :: System :: Networking
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
33
|
+
Requires-Dist: black; extra == "dev"
|
|
34
|
+
Requires-Dist: flake8; extra == "dev"
|
|
35
|
+
Requires-Dist: mypy; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# librats — Python bindings
|
|
39
|
+
|
|
40
|
+
Python bindings for [librats](../README.md). They call the library's C ABI
|
|
41
|
+
(`src/librats/bindings/rats.h`) through `ctypes`, so there is nothing to compile
|
|
42
|
+
on the Python side — only the librats shared library itself.
|
|
43
|
+
|
|
44
|
+
## The model
|
|
45
|
+
|
|
46
|
+
A `RatsNode` is one librats node. On its own it gives you an encrypted
|
|
47
|
+
connection to any peer you dial (Noise XX, over UDP or TCP on the same port), a
|
|
48
|
+
peer id derived from the node's key, and raw messages on named channels.
|
|
49
|
+
Discovery, NAT traversal, pub/sub, JSON messages, file transfer, ping and
|
|
50
|
+
reconnection are subsystems you switch on one by one.
|
|
51
|
+
|
|
52
|
+
Three rules follow:
|
|
53
|
+
|
|
54
|
+
- **Enable subsystems and register callbacks before `start()`.** Enabling after
|
|
55
|
+
start raises `RatsAlreadyStartedError`; registering a peer or channel callback
|
|
56
|
+
after start does too. Using a subsystem you did not enable raises
|
|
57
|
+
`RatsNotEnabledError`.
|
|
58
|
+
- **Callbacks run on a librats thread.** Keep them short. An exception raised in
|
|
59
|
+
one is printed to stderr; it never reaches the library.
|
|
60
|
+
- **Errors are exceptions.** Each `rats_error_t` code has its own `RatsError`
|
|
61
|
+
subclass (table below). Peer ids are 64-character lowercase hex strings.
|
|
62
|
+
|
|
63
|
+
## Installing
|
|
64
|
+
|
|
65
|
+
Python 3.8 or newer, and the librats **shared** library:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
cd python
|
|
69
|
+
python build.py --build-native # CMake, -DRATS_SHARED_LIBRARY=ON, into build/python,
|
|
70
|
+
# then copies the library next to the package
|
|
71
|
+
pip install -e .
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The loader tries, in order: the directories in `LIBRATS_LIBRARY_PATH`, the
|
|
75
|
+
package directory, `build/lib`, `build/bin` and `build/` of the repository, and
|
|
76
|
+
finally the system loader by bare name. The file is `rats.dll` or `librats.dll`
|
|
77
|
+
on Windows, `librats.dylib` on macOS, `librats.so` elsewhere. A MinGW build of
|
|
78
|
+
the DLL also needs `libstdc++-6.dll`, `libgcc_s_seh-1.dll` and
|
|
79
|
+
`libwinpthread-1.dll` beside it: Python does not search `PATH` for a DLL's
|
|
80
|
+
dependencies.
|
|
81
|
+
|
|
82
|
+
## Quick start
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from librats_py import RatsNode
|
|
86
|
+
|
|
87
|
+
with RatsNode(8080, protocol="myapp/1.0", data_dir="./state") as node:
|
|
88
|
+
node.on_peer_connected(lambda peer: print("+", peer))
|
|
89
|
+
node.on_peer_disconnected(lambda peer, why: print("-", peer, why.name))
|
|
90
|
+
node.on("chat", lambda peer, data: print(peer[:8], data.decode()))
|
|
91
|
+
node.enable_mdns() # find peers on the local network
|
|
92
|
+
|
|
93
|
+
node.start()
|
|
94
|
+
print("id", node.local_id, "port", node.listen_port)
|
|
95
|
+
node.connect("192.168.1.100", 8081) # or wait for discovery
|
|
96
|
+
node.broadcast("chat", b"hello")
|
|
97
|
+
input("Enter to quit\n")
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`with` destroys the node on exit. Without it, call `destroy()` (or let the
|
|
101
|
+
garbage collector do it).
|
|
102
|
+
|
|
103
|
+
## Configuration
|
|
104
|
+
|
|
105
|
+
Every keyword of `RatsNode(...)` maps to a field of `rats_config_t`; leave one
|
|
106
|
+
out and the library default applies.
|
|
107
|
+
|
|
108
|
+
| Keyword | Default | Meaning |
|
|
109
|
+
| --- | --- | --- |
|
|
110
|
+
| `listen_port` | 0 | port both transports listen on; 0 picks a free one |
|
|
111
|
+
| `protocol` | `"librats/1.0"` | app id bound into the handshake; peers with a different one cannot connect |
|
|
112
|
+
| `data_dir` | none | keeps the identity (and DHT table, reconnect list) across runs; without it the id is new every run |
|
|
113
|
+
| `security` | `Security.NOISE` | `Security.PLAINTEXT` turns encryption off |
|
|
114
|
+
| `bind_address` | `"::"` | interface to listen on (dual-stack by default) |
|
|
115
|
+
| `dial_only` | `False` | no listener at all |
|
|
116
|
+
| `max_peers` | 0 | cap on established peers, enforced on inbound; 0 = none. Also a settable property |
|
|
117
|
+
| `transports` | both | `TransportMask.TCP`, `TransportMask.UDP`, or both |
|
|
118
|
+
| `preferred_transport` | `Transport.UDP` | which one a dial tries first |
|
|
119
|
+
| `transport_fallback_ms` | 1200 | how long the first transport dials alone before the other joins; `FALLBACK_NEVER` disables it |
|
|
120
|
+
| `send_queue_limit` | 8 MiB | bytes a peer's queue may hold before a sender that keeps going loses the peer (see Backpressure) |
|
|
121
|
+
| `path_sample_ms` | off | how often `path_stats()` is refreshed; an `on_peer_path` callback turns it on at 100 ms |
|
|
122
|
+
| `congestion_control` | `Congestion.BBR` | `Congestion.RENO` for a loss-based controller (UDP only) |
|
|
123
|
+
| `udp_receive_window` | 16 MiB | ceiling of one UDP connection's flow-control window |
|
|
124
|
+
| `udp_receive_budget` | 64 MiB | out-of-order bytes all UDP connections together may hold |
|
|
125
|
+
| `udp_offload` | `True` | kernel UDP segmentation offload where available |
|
|
126
|
+
| `udp_idle_timeout_ms` | 45 s | silence after which a UDP peer is dropped as lost (`CloseReason.IDLE_TIMEOUT`) |
|
|
127
|
+
| `reactor_threads` | 1 | I/O threads |
|
|
128
|
+
| `network_monitor` | `True` | watch the host for interface and address changes |
|
|
129
|
+
|
|
130
|
+
An impossible combination — no transport, or a preferred transport you turned
|
|
131
|
+
off — raises `RatsInvalidArgError` from the constructor.
|
|
132
|
+
|
|
133
|
+
## Messages
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
node.send(peer_id, "chat", b"hi") # one peer
|
|
137
|
+
node.broadcast("chat", b"hi everyone") # every connected peer
|
|
138
|
+
node.send(peer_id, "video", frame, lifetime_ms=150)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
A send returns once the message is queued; it does not wait for delivery.
|
|
142
|
+
Without `lifetime_ms` a message is reliable and arrives in order with
|
|
143
|
+
everything else the node sends on its channels. With `lifetime_ms` it is a
|
|
144
|
+
**live** message: it is worth delivering only within that many milliseconds, is
|
|
145
|
+
repaired while it can still make it, and is dropped whole once it cannot. Live
|
|
146
|
+
messages are never delivered partly or twice and are not ordered, so number them
|
|
147
|
+
if order matters. They are meant for audio and video frames; the limits are
|
|
148
|
+
`LIVE_MAX_LIFETIME_MS` (30 s) and `LIVE_MAX_PAYLOAD` (4 MiB). The receiving
|
|
149
|
+
`on()` handler is the same for both kinds.
|
|
150
|
+
|
|
151
|
+
### Backpressure
|
|
152
|
+
|
|
153
|
+
`send()` accepts a message of any size, but a peer whose queue is already past
|
|
154
|
+
`send_queue_limit` when you send more is dropped with
|
|
155
|
+
`CloseReason.SLOW_CONSUMER`. To avoid that when sending in bulk:
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
node.send(peer_id, "bulk", chunk)
|
|
159
|
+
if not node.peer_writable(peer_id):
|
|
160
|
+
# stop here; resume when on_peer_writable(peer_id) fires
|
|
161
|
+
...
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Path statistics
|
|
165
|
+
|
|
166
|
+
For a sender that sizes its output to the link (a live encoder),
|
|
167
|
+
`path_stats(peer_id)` returns a `PathStats`: the congestion controller's
|
|
168
|
+
`bandwidth_bps`, `srtt_us`, `queued_bytes`, `loss`, and counts of live messages
|
|
169
|
+
sent and expired. Over TCP most figures come from the kernel and exist only on
|
|
170
|
+
Linux. `on_peer_path(cb)` delivers the same samples periodically.
|
|
171
|
+
|
|
172
|
+
## Peers
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
node.peer_count, node.peer_ids
|
|
176
|
+
info = node.peer_info(peer_id) # None if not connected
|
|
177
|
+
info.transport # Transport.UDP / TCP / RELAY
|
|
178
|
+
info.direction # Direction.OUTBOUND / INBOUND
|
|
179
|
+
info.address # "203.0.113.5:8080", "" until known
|
|
180
|
+
info.transports # what the peer says it accepts
|
|
181
|
+
node.disconnect(peer_id)
|
|
182
|
+
node.on_dial_failed(lambda address: print("unreachable", address))
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
`on_peer_disconnected` passes a `CloseReason`. A peer that left can be redialed;
|
|
186
|
+
`SLOW_CONSUMER` means this node sent faster than the link drained, and redialing
|
|
187
|
+
only repeats that.
|
|
188
|
+
|
|
189
|
+
## Subsystems
|
|
190
|
+
|
|
191
|
+
All of these are enabled before `start()`.
|
|
192
|
+
|
|
193
|
+
| Subsystem | Enable | Then |
|
|
194
|
+
| --- | --- | --- |
|
|
195
|
+
| DHT discovery | `enable_dht(dht_port=0, discovery_key=None)` | peers found are dialed automatically; `dht_set_keys([...])` changes the keys, also while running |
|
|
196
|
+
| mDNS discovery | `enable_mdns()` | — |
|
|
197
|
+
| Peer exchange | `enable_peer_exchange()` | peers share the addresses of peers they hold |
|
|
198
|
+
| Port mapping | `enable_port_mapping(upnp=True, natpmp=True)` | — |
|
|
199
|
+
| Hole punching | `enable_hole_punch(serve_rendezvous=True)` | `punch_peer(peer_id)`, `nat_mapping`, `observed_addresses` |
|
|
200
|
+
| Relaying | `enable_relay(serve=False)` | `connect_via_relay(peer_id)` |
|
|
201
|
+
| Pub/sub | `enable_pubsub()` | `subscribe(topic, cb)`, `unsubscribe(topic)`, `publish(topic, data)` |
|
|
202
|
+
| JSON messages | `enable_json()` | `on_json`, `once_json`, `off_json`, `send_json`, `broadcast_json` |
|
|
203
|
+
| File transfer | `enable_file_transfer(temp_dir=None)` | see below |
|
|
204
|
+
| Ping | `enable_ping()` | `peer_rtt_ms(peer_id)` — `None` until measured |
|
|
205
|
+
| Reconnection | `enable_reconnect()` | `add_reconnect(host, port)`, `remove_reconnect(host, port)` |
|
|
206
|
+
| Reconnection, lost peers too | `enable_reconnect_lost(lost_for_ms)` | as above; a peer lost (not one that left) is redialed where it was last seen for `lost_for_ms` |
|
|
207
|
+
| BitTorrent | `enable_bittorrent(...)` | `bt_add_magnet`, `bt_add_torrent_file`, `bt_remove_torrent` — only in libraries built with `RATS_SEARCH_FEATURES` |
|
|
208
|
+
|
|
209
|
+
### NAT traversal
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
node.enable_port_mapping() # ask the router first
|
|
213
|
+
node.enable_hole_punch() # punch through a peer both sides share
|
|
214
|
+
node.enable_relay() # last resort: route through such a peer
|
|
215
|
+
node.start()
|
|
216
|
+
...
|
|
217
|
+
node.punch_peer(peer_id) # success arrives as on_peer_connected
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Hole punching needs every node involved to have it enabled, and some node in
|
|
221
|
+
the mesh to pass rendezvous messages along (`serve_rendezvous=True`, the
|
|
222
|
+
default). It cannot work from behind a symmetric NAT
|
|
223
|
+
(`node.nat_mapping == NatMapping.ENDPOINT_DEPENDENT`); a relay can. A relayed
|
|
224
|
+
peer stays encrypted end to end and `peer_info()` reports `Transport.RELAY`.
|
|
225
|
+
With both enabled, a punch that cannot work falls back to a relay, and a relayed
|
|
226
|
+
peer is moved to a direct connection when a later punch succeeds.
|
|
227
|
+
`enable_relay(serve=True)` also carries other peers' connections, which costs
|
|
228
|
+
this node their bandwidth; nobody does it unless asked.
|
|
229
|
+
|
|
230
|
+
`punch_peer` and `connect_via_relay` raise `RatsDeclinedError` when there is
|
|
231
|
+
nothing to do or nothing to try with (already connected, already trying, no peer
|
|
232
|
+
to help).
|
|
233
|
+
|
|
234
|
+
### JSON messages
|
|
235
|
+
|
|
236
|
+
```python
|
|
237
|
+
node.enable_json()
|
|
238
|
+
node.on_json("greeting", lambda peer, value: print(value["hi"]))
|
|
239
|
+
node.start()
|
|
240
|
+
node.broadcast_json("greeting", {"hi": "there"})
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Values are encoded with `json.dumps` and handed to handlers parsed. `on_json`
|
|
244
|
+
adds a handler and may be called while running; `once_json` removes it after
|
|
245
|
+
its first message.
|
|
246
|
+
|
|
247
|
+
### File transfer
|
|
248
|
+
|
|
249
|
+
The sender offers, the receiver accepts or rejects.
|
|
250
|
+
|
|
251
|
+
```python
|
|
252
|
+
node.enable_file_transfer(temp_dir="./partial")
|
|
253
|
+
node.on_file_offer(lambda peer, tid, name, size, is_dir:
|
|
254
|
+
node.accept_file(peer, tid, f"./downloads/{name}"))
|
|
255
|
+
node.on_file_progress(lambda p: print(p.transfer_id, p.bytes_transferred, p.total_bytes,
|
|
256
|
+
p.status.name))
|
|
257
|
+
node.on_file_complete(lambda peer, tid, ok, path: print(tid, ok, path))
|
|
258
|
+
node.start()
|
|
259
|
+
|
|
260
|
+
tid = node.send_file(peer_id, "report.pdf") # or send_directory()
|
|
261
|
+
node.pause_file(peer_id, tid); node.resume_file(peer_id, tid); node.cancel_file(peer_id, tid)
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
A transfer is named by the pair (peer, transfer id). The offer, progress and
|
|
265
|
+
complete callbacks are one each; registering again replaces the previous one.
|
|
266
|
+
`on_file_progress` receives a `FileProgress` with direction, status, byte and
|
|
267
|
+
file counts and a smoothed rate.
|
|
268
|
+
|
|
269
|
+
## Module functions
|
|
270
|
+
|
|
271
|
+
`set_log_level(LogLevel)`, `set_log_file(path or None)`, `version()`,
|
|
272
|
+
`version_info()`, `git_describe()`, `abi()`, `error_str(code)`.
|
|
273
|
+
|
|
274
|
+
## Errors
|
|
275
|
+
|
|
276
|
+
| Exception | `ErrorCode` | Raised when |
|
|
277
|
+
| --- | --- | --- |
|
|
278
|
+
| `RatsInvalidArgError` | `INVALID_ARG` | malformed peer id, bad JSON, impossible config |
|
|
279
|
+
| `RatsNotStartedError` | `NOT_STARTED` | `connect()` before `start()` |
|
|
280
|
+
| `RatsAlreadyStartedError` | `ALREADY_STARTED` | enabling or registering a callback after `start()` |
|
|
281
|
+
| `RatsNotEnabledError` | `NOT_ENABLED` | using a subsystem that was not enabled |
|
|
282
|
+
| `RatsNoSuchPeerError` | `NO_SUCH_PEER` | the peer is not connected |
|
|
283
|
+
| `RatsNotFoundError` | `NOT_FOUND` | no such file transfer, or a file to offer that cannot be read |
|
|
284
|
+
| `RatsDeclinedError` | `DECLINED` | a punch or relay attempt has nothing to do |
|
|
285
|
+
| `RatsBindError` | `BIND` | `start()` could not open its sockets |
|
|
286
|
+
|
|
287
|
+
All derive from `RatsError`, whose `error_code` holds the code.
|
|
288
|
+
|
|
289
|
+
## Examples
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
python -m librats_py.examples.basic_client 8080
|
|
293
|
+
python -m librats_py.examples.file_transfer 8080 ./downloads
|
|
294
|
+
python -m librats_py.examples.gossipsub_chat 8080 alice general
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## Tests
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
pip install pytest
|
|
301
|
+
python -m pytest librats_py/tests
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Tests that need the shared library are marked `native` and skipped when it
|
|
305
|
+
cannot be loaded.
|
|
306
|
+
|
|
307
|
+
## License
|
|
308
|
+
|
|
309
|
+
MIT — see [LICENSE](../LICENSE).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
librats-3.0.5.dev2.dist-info/RECORD,,
|
|
2
|
+
librats-3.0.5.dev2.dist-info/WHEEL,sha256=_7lYIIGR1zvFG3roSVA6rxurggE2B2lmaTuznUQbYG0,138
|
|
3
|
+
librats-3.0.5.dev2.dist-info/top_level.txt,sha256=_TZbGWdyEN77fja2WXIyK_NwhHM2mcI4rtP95pLNk4I,11
|
|
4
|
+
librats-3.0.5.dev2.dist-info/METADATA,sha256=ouV9uZsNbZiDiUW5rXGa7Nm0Kn0lFTwZBPKpJH-VuDE,13821
|
|
5
|
+
librats-3.0.5.dev2.dist-info/licenses/LICENSE,sha256=tilr0GgNhAJZpTZ6pDkIiiRjViLah6_9cXNw_-WYCDc,1077
|
|
6
|
+
librats_py/ctypes_wrapper.py,sha256=RYVdnXP2s1MlFbjXXd6Vp8rI52G8URbSG3s81coi864,9996
|
|
7
|
+
librats_py/librats.dylib,sha256=0fQqRxLoV2QsBZWVNr8eXuJHH267oQepvAFsA3JaWBw,4839216
|
|
8
|
+
librats_py/enums.py,sha256=9iRTs0HaV8pgwXT1ZLuLDw0KwrgMiiouF_AwErST0YI,6847
|
|
9
|
+
librats_py/__init__.py,sha256=CKpRKluGV6AkwswFH0pTuDpaEhMwFIX0HH44QXRez1Q,2010
|
|
10
|
+
librats_py/core.py,sha256=mfnR03fDR5PdZUzC9ZeXufwGVgmByFCgi7bN0uWqgIM,34709
|
|
11
|
+
librats_py/callbacks.py,sha256=857mSOQkGINRXVHU3gkVFp9fb9WbyzPFYxbdUIT5-IE,2796
|
|
12
|
+
librats_py/exceptions.py,sha256=DRlc-zLdZ0uSEtGGdEmFordzSC2iW69X7B3nlzAQr28,2560
|
|
13
|
+
librats_py/tests/conftest.py,sha256=FjWEkT-x2GoicPXMi4c_8dwcnUrCBb-XE2jsZ-vp5Sg,887
|
|
14
|
+
librats_py/tests/__init__.py,sha256=Wd1oYrNUcV0rU2YoEZWIwjF859ZKmURBlc9RCxu9I0I,48
|
|
15
|
+
librats_py/tests/test_abi_layout.py,sha256=Q9zcMbWhQ3x1sx7JWFueNUY16IBgmgY63FvJ0oyfKJg,3441
|
|
16
|
+
librats_py/tests/test_integration.py,sha256=ceDztAK19X1UcU69CHqOEgkQF6buyz5Gvo_AAtLwKzI,5667
|
|
17
|
+
librats_py/tests/test_client.py,sha256=WlqJHsKKmh0iLWxe_LHLcDuAlk0WKyxTHD464WC-xQk,5494
|
|
18
|
+
librats_py/examples/file_transfer.py,sha256=UZ0fcnVWtBpjXYB1Lrz7FTIBqhvFzqMFujynrQa_ddo,4581
|
|
19
|
+
librats_py/examples/__init__.py,sha256=l0for57FMJggKh5KPN1-i74cqciVqFZwyJ2lxs1evoo,46
|
|
20
|
+
librats_py/examples/gossipsub_chat.py,sha256=t7rb_Rv4BjLdVTApQi6ULkr69wS_S9Y0mRQ5UYlPp-k,4018
|
|
21
|
+
librats_py/examples/basic_client.py,sha256=qX3fN77dyBvrfegBku_0s-6eyy_q3fwgrYh2myzX6L4,3122
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 librats contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
librats_py
|
librats_py/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
librats_py — Python bindings for librats, over its C ABI through ctypes.
|
|
3
|
+
|
|
4
|
+
A :class:`RatsNode` on its own is secure transport (Noise XX over UDP or TCP)
|
|
5
|
+
plus raw messaging on named channels. Discovery, NAT traversal, pub/sub, JSON
|
|
6
|
+
messaging, file transfer, ping and reconnection are subsystems: enable each with
|
|
7
|
+
its ``enable_*()`` before :meth:`RatsNode.start`.
|
|
8
|
+
|
|
9
|
+
from librats_py import RatsNode
|
|
10
|
+
|
|
11
|
+
with RatsNode(8080, protocol="myapp/1.0") as node:
|
|
12
|
+
node.on_peer_connected(lambda peer: print("+", peer))
|
|
13
|
+
node.on("chat", lambda peer, data: print(peer, data.decode()))
|
|
14
|
+
node.enable_mdns()
|
|
15
|
+
node.start()
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from .core import (
|
|
19
|
+
RatsNode, set_log_level, set_log_file, version, version_info, git_describe, abi,
|
|
20
|
+
error_str,
|
|
21
|
+
)
|
|
22
|
+
from .enums import (
|
|
23
|
+
RatsError as ErrorCode, Security, Transport, TransportMask, Congestion, Direction,
|
|
24
|
+
CloseReason, NatMapping, LogLevel, FileTransferStatus, BtEncryption,
|
|
25
|
+
PeerInfo, PathStats, FileProgress, VersionInfo,
|
|
26
|
+
FALLBACK_NEVER, LIVE_MAX_LIFETIME_MS, LIVE_MAX_PAYLOAD,
|
|
27
|
+
)
|
|
28
|
+
from .exceptions import (
|
|
29
|
+
RatsError, RatsInvalidArgError, RatsNotStartedError, RatsAlreadyStartedError,
|
|
30
|
+
RatsNotEnabledError, RatsNoSuchPeerError, RatsNotFoundError, RatsDeclinedError,
|
|
31
|
+
RatsBindError,
|
|
32
|
+
)
|
|
33
|
+
from .ctypes_wrapper import LibratsNotFoundError
|
|
34
|
+
|
|
35
|
+
__version__ = "3.0.5.dev2"
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"RatsNode",
|
|
39
|
+
"set_log_level", "set_log_file", "version", "version_info", "git_describe", "abi",
|
|
40
|
+
"error_str",
|
|
41
|
+
"ErrorCode", "Security", "Transport", "TransportMask", "Congestion", "Direction",
|
|
42
|
+
"CloseReason", "NatMapping", "LogLevel", "FileTransferStatus", "BtEncryption",
|
|
43
|
+
"PeerInfo", "PathStats", "FileProgress", "VersionInfo",
|
|
44
|
+
"FALLBACK_NEVER", "LIVE_MAX_LIFETIME_MS", "LIVE_MAX_PAYLOAD",
|
|
45
|
+
"RatsError", "RatsInvalidArgError", "RatsNotStartedError", "RatsAlreadyStartedError",
|
|
46
|
+
"RatsNotEnabledError", "RatsNoSuchPeerError", "RatsNotFoundError", "RatsDeclinedError",
|
|
47
|
+
"RatsBindError", "LibratsNotFoundError",
|
|
48
|
+
]
|
librats_py/callbacks.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Callback types of the librats Python bindings.
|
|
3
|
+
|
|
4
|
+
The ``*Type`` names are the raw ``CFUNCTYPE`` prototypes matching the typedefs
|
|
5
|
+
in ``src/librats/bindings/rats.h``; every C callback takes ``void* user`` first.
|
|
6
|
+
Struct pointers (path stats, file progress) arrive as ``c_void_p`` and are read
|
|
7
|
+
in ``core.py``. The other names are the Pythonic signatures
|
|
8
|
+
:class:`~librats_py.core.RatsNode` calls once ids, payloads and structs are
|
|
9
|
+
decoded.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from typing import Any, Callable
|
|
13
|
+
from ctypes import CFUNCTYPE, c_void_p, c_char_p, c_size_t, c_int, c_uint64
|
|
14
|
+
|
|
15
|
+
from .enums import CloseReason, FileProgress, PathStats
|
|
16
|
+
|
|
17
|
+
# --- Raw C prototypes (must mirror rats.h exactly) ---
|
|
18
|
+
|
|
19
|
+
# rats_peer_cb(user, peer_id)
|
|
20
|
+
PeerCallbackType = CFUNCTYPE(None, c_void_p, c_char_p)
|
|
21
|
+
# rats_disconnect_cb(user, peer_id, reason)
|
|
22
|
+
DisconnectCallbackType = CFUNCTYPE(None, c_void_p, c_char_p, c_int)
|
|
23
|
+
# rats_dial_failed_cb(user, address)
|
|
24
|
+
DialFailedCallbackType = CFUNCTYPE(None, c_void_p, c_char_p)
|
|
25
|
+
# rats_message_cb(user, peer_id, data, len)
|
|
26
|
+
MessageCallbackType = CFUNCTYPE(None, c_void_p, c_char_p, c_void_p, c_size_t)
|
|
27
|
+
# rats_path_cb(user, peer_id, const rats_path_stats_t*)
|
|
28
|
+
PathCallbackType = CFUNCTYPE(None, c_void_p, c_char_p, c_void_p)
|
|
29
|
+
# rats_topic_cb(user, peer_id, topic, data, len)
|
|
30
|
+
TopicCallbackType = CFUNCTYPE(None, c_void_p, c_char_p, c_char_p, c_void_p, c_size_t)
|
|
31
|
+
# rats_json_cb(user, peer_id, json)
|
|
32
|
+
JsonCallbackType = CFUNCTYPE(None, c_void_p, c_char_p, c_char_p)
|
|
33
|
+
# rats_file_offer_cb(user, peer_id, transfer_id, name, size, is_directory)
|
|
34
|
+
FileOfferCallbackType = CFUNCTYPE(None, c_void_p, c_char_p, c_uint64, c_char_p, c_uint64, c_int)
|
|
35
|
+
# rats_file_progress_cb(user, const rats_file_progress_t*)
|
|
36
|
+
FileProgressCallbackType = CFUNCTYPE(None, c_void_p, c_void_p)
|
|
37
|
+
# rats_file_complete_cb(user, peer_id, transfer_id, success, path)
|
|
38
|
+
FileCompleteCallbackType = CFUNCTYPE(None, c_void_p, c_char_p, c_uint64, c_int, c_char_p)
|
|
39
|
+
|
|
40
|
+
# --- Pythonic signatures ---
|
|
41
|
+
|
|
42
|
+
PeerCallback = Callable[[str], None] # (peer_id)
|
|
43
|
+
DisconnectCallback = Callable[[str, CloseReason], None] # (peer_id, reason)
|
|
44
|
+
DialFailedCallback = Callable[[str], None] # (address "ip:port")
|
|
45
|
+
MessageCallback = Callable[[str, bytes], None] # (peer_id, data)
|
|
46
|
+
PathCallback = Callable[[str, PathStats], None] # (peer_id, stats)
|
|
47
|
+
TopicCallback = Callable[[str, str, bytes], None] # (peer_id, topic, data)
|
|
48
|
+
JsonCallback = Callable[[str, Any], None] # (peer_id, parsed value)
|
|
49
|
+
FileOfferCallback = Callable[[str, int, str, int, bool], None]
|
|
50
|
+
# (peer_id, transfer_id, name, size, is_directory)
|
|
51
|
+
FileProgressCallback = Callable[[FileProgress], None]
|
|
52
|
+
FileCompleteCallback = Callable[[str, int, bool, str], None] # (peer_id, transfer_id, success, path)
|