对等网络基础设施库。
Peertable的Python项目详细描述
可浏览
欢迎来到Peertable!这个项目是一个对等的基础设施 python 3的网络库。你也可以单独使用 模式,尽管除了连接之外,它几乎没有其他用途 现有的对等体和网络(因此它们可以找到彼此) 一旦他们这么做了,你就不需要了!).
创建桥对等节点
默认的(主要是演示性的)可访问应用程序将, 默认情况下,只记录给它的消息,但是,和任何对等方一样, 是的,服务于一个实际的目的:充当同龄人之间的桥梁,或者 甚至可能是网络!
要使用默认应用程序,请使用:
python3 -i -m peertable
然后,插入您的侦听端口,公共IP地址(优先选择 在NAT或转发端口外部)和外部端口(如果在隧道后面 按顺序排列。外部端口是可选的。
然后,插入要连接到的初始对等点的空格分隔列表(或 没有,如果没有)。
等一下!现在您已连接,并处于交互提示中。它可能
似乎它是空闲的,但它是连接到目标对等点!它是
发送identificatoin请求(以及通过
分机)。现在可以使用send命令
在本例中,另一侧将看到: 有效负载是您在调用 举个很好的例子,看看这个: 在您询问之前,是的,这是您执行>>> [c.id for c in s.clients]
['fc4h0MwGznaAqObYDWMXUiAAZ']
>>> for c in s.clients:
... s.send_id(c.id, peertable.Message(True, "TESTMESG", "Hello! If you are reading this, then a human is behind the recipient peer of this message, i.e. YOU! Your ID: " + c.id))
>+ + + + +
Received message!
Sender ID: p8vM6FqyBGzm0Sdnr6XCDoZdy
Message type: TESTMESG
Payload length: 135
Payload in Base64: SGVsbG8hIElmIHlvdSBhcmUgcmVhZGluZyB0aGlzLCB0aGVuIGEgaHVtYW4gaXMgYmVoaW5kIHRoZSByZWNpcGllbnQgcGVlciBvZiB0aGlzIG1lc3NhZ2UsIGkuZS4gWU9VISBZb3VyIElEOiBmYzRoME13R3puYUFxT2JZRFdNWFVpQUFa
- - - - -<
peertable.Message
的第三个参数中给出的消息,而返回值则是调用s.send_id
的第二个参数。使用库
import peertable
import random
import base64
class TestApp(peertable.PeerApplication):
def receive(self, server, client, message):
print(">+ + + + +\nReceived message!\nSender ID: {}\nMessage type: {}\nPayload length: {}\nPayload in Base64: {}\n- - - - -<".format(client.id, message.message_type, len(message.payload), base64.b64encode(message.payload).decode('utf-8')))
if __name__ == "__main__":
print("Insert your new peer's listen port: (defaults to 2912)")
port = int(input() or 2912)
print()
print("Insert your machine's public IP address (so others can connect to you, etc):")
my_addr = input()
print()
print("Insert this server's public port, in case you use a tunnel or port forward (or none otherwise):")
my_port = int(input() or port)
s = peertable.PeerServer(my_addr, port=port, remote_port=my_port)
s.start_loop()
s.register_app(TestApp())
print()
print("My port: " + str(s.port))
print("My ID: " + str(s.id))
print()
print("Insert target IP:port addresses, separated by space:")
addrs = input()
for addr in addrs.split(' '):
try:
addr = addr.split(':')
if len(addr) < 2:
raise ValueError("-----")
addr[1] = int(addr[1])
s.connect(tuple(addr))
except ValueError:
pass
python3 -i -m peertable
时运行的脚本。推荐PyPI第三方库