本文整理汇总了Python中dht.protocol.KademliaProtocol.ping方法的典型用法代码示例。如果您正苦于以下问题:Python KademliaProtocol.ping方法的具体用法?Python KademliaProtocol.ping怎么用?Python KademliaProtocol.ping使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dht.protocol.KademliaProtocol
的用法示例。
在下文中一共展示了KademliaProtocol.ping方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Server
# 需要导入模块: from dht.protocol import KademliaProtocol [as 别名]
# 或者: from dht.protocol.KademliaProtocol import ping [as 别名]
class Server(object):
"""
High level view of a node instance. This is the object that should be created
to start listening as an active node on the network.
"""
def __init__(self, node, ksize=20, alpha=3, storage=None):
"""
Create a server instance. This will start listening on the given port.
Args:
node: The node instance for this peer. It must contain (at minimum) an ID,
public key, ip address, and port.
ksize (int): The k parameter from the paper
alpha (int): The alpha parameter from the paper
storage: An instance that implements :interface:`~dht.storage.IStorage`
"""
self.ksize = ksize
self.alpha = alpha
self.log = Logger(system=self)
self.storage = storage or ForgetfulStorage()
self.node = node
self.protocol = KademliaProtocol(self.node, self.storage, ksize)
self.refreshLoop = LoopingCall(self.refreshTable).start(3600)
def listen(self, port):
"""
Start listening on the given port.
This is the same as calling::
reactor.listenUDP(port, server.protocol)
"""
return reactor.listenUDP(port, self.protocol)
def refreshTable(self):
"""
Refresh buckets that haven't had any lookups in the last hour
(per section 2.3 of the paper).
"""
ds = []
for id in self.protocol.getRefreshIDs():
node = Node(id)
nearest = self.protocol.router.findNeighbors(node, self.alpha)
spider = NodeSpiderCrawl(self.protocol, node, nearest)
ds.append(spider.find())
def republishKeys(_):
ds = []
# Republish keys older than one hour
for keyword in self.storage.iterkeys():
for k, v in self.storage.iteritems(keyword):
if self.storage.get_ttl(keyword, k) < 601200:
ds.append(self.set(keyword, k, v))
return defer.gatherResults(ds).addCallback(republishKeys)
def querySeed(self, seed, pubkey):
"""
Query an HTTP seed and return a `list` if (ip, port) `tuple` pairs.
Args:
seed: A `string` consisting of "ip:port" or "hostname:port"
pubkey: The hex encoded public key to verify the signature on the response
"""
nodes = []
c = httplib.HTTPConnection(seed)
c.request("GET", "/")
response = c.getresponse()
self.log.info("Https response from %s: %s, %s" % (seed, response.status, response.reason))
data = response.read()
reread_data = data.decode("zlib")
seeds = peers.PeerSeeds()
try:
seeds.ParseFromString(reread_data)
for peer in seeds.peer_data:
p = peers.PeerData()
p.ParseFromString(peer)
tup = (str(p.ip_address), p.port)
nodes.append(tup)
verify_key = nacl.signing.VerifyKey(pubkey, encoder=nacl.encoding.HexEncoder)
verify_key.verify(seed.signature + "".join(seeds.peer_data))
except:
self.log.error("Error parsing seed response.")
return nodes
def bootstrappableNeighbors(self):
"""
Get a :class:`list` of (ip, port) :class:`tuple` pairs suitable for use as an argument
to the bootstrap method.
The server should have been bootstrapped
already - this is just a utility for getting some neighbors and then
storing them if this server is going down for a while. When it comes
back up, the list of nodes can be used to bootstrap.
"""
neighbors = self.protocol.router.findNeighbors(self.node)
return [tuple(n)[-2:] for n in neighbors]
def bootstrap(self, addrs):
#.........这里部分代码省略.........