-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathp2p.py
More file actions
105 lines (79 loc) · 3.32 KB
/
p2p.py
File metadata and controls
105 lines (79 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import typing as ty
from . import base
class Section(base.SectionBase):
@base.returns_no_item
def forward(self, protocol: str, peer_id: str, port: str, **kwargs: base.CommonArgs):
"""Forward connections to libp2p service
Forward connections made to the specified port to another IPFS node.
.. code-block:: python
# forwards connections made to port 8888 to 'QmHash' as protocol '/x/testproto'
>>> client.p2p.forward('/x/testproto', 'QmHash', 8888)
[]
Parameters
----------
protocol
specifies the libp2p protocol name to use for libp2p connections and/or handlers. It must be prefixed with '/x/'.
PeerID
Target endpoint
port
Listening endpoint
Returns
-------
list
An empty list
"""
args = (protocol, peer_id, port)
return self._client.request('/p2p/forward', args, decoder='json', **kwargs)
@base.returns_no_item
def listen(self, protocol: str, port: str, **kwargs: base.CommonArgs):
"""Create libp2p service to forward IPFS connections to port
Creates a libp2p service that forwards IPFS connections to it
to the specified port on the local computer.
.. code-block:: python
# listens for connections of protocol '/x/testproto' and forwards them to port 8888
>>> client.p2p.listen('/x/testproto', 8888)
[]
Parameters
----------
protocol
specifies the libp2p handler name. It must be prefixed with '/x/'.
port
Listener port to which to forward incoming connections
Returns
-------
list
An empty list
"""
args = (protocol, port)
return self._client.request('/p2p/listen', args, decoder='json', **kwargs)
# @base.returns_single_item(base.ResponseBase)
def close(self, all: bool = False, protocol: str = None, listenaddress: str = None, targetaddress: str = None, **kwargs: base.CommonArgs):
"""Stop listening for new connections to forward.
Stops all forwarding and listening libp2p services that match the input arguments.
.. code-block:: python
# Close listening and forwarding connections of protocol '/x/testproto' and port 8888.
>>> client.p2p.close(protocol='/x/testproto', port='8888')
[]
Parameters
----------
protocol
specifies the libp2p handler name. It must be prefixed with '/x/'.
port
Listener port to which to forward incoming connections
Returns
-------
list
An empty list
"""
opts = {}
if all is not None:
opts.update({"all": all})
if protocol is not None:
opts.update({"protocol": str(protocol)})
if listenaddress is not None:
opts.update({"listen-address": str(listenaddress)})
if targetaddress is not None:
opts.update({"target-address": str(targetaddress)})
kwargs.setdefault("opts", {}).update(opts)
args = (all,) # if all is not None else ()
return self._client.request('/p2p/close', decoder='json', **kwargs)