本文整理汇总了Python中kademlia.crawling.NodeSpiderCrawl类的典型用法代码示例。如果您正苦于以下问题:Python NodeSpiderCrawl类的具体用法?Python NodeSpiderCrawl怎么用?Python NodeSpiderCrawl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NodeSpiderCrawl类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initTable
def initTable(results, challenge, id):
nodes = []
for addr, result in results.items():
ip = addr[0]
port = addr[1]
if result[0]:
resultId = result[1]['id']
resultIdHex = resultId.encode('hex').upper()
resultSign = result[1]['signature']
data = self.protocol.certificateExists(resultIdHex)
if not data:
identifier = "{}cert".format(resultIdHex)
self.protocol.callCertFindValue(Node(resultId,
ip,
port),
Node(identifier))
else:
cert_stored = self.protocol.searchForCertificate(resultIdHex)
try:
OpenSSL.crypto.verify(cert_stored,
resultSign,
challenge,
"sha256")
except:
traceback.print_exc()
nodes.append(Node(resultId, ip, port))
spider = NodeSpiderCrawl(self.protocol,
self.node,
nodes,
self.ksize,
self.alpha)
return spider.find()
示例2: initTable
def initTable(results):
nodes = []
for addr, result in results.items():
if result[0]:
nodes.append(Node(result[1], addr[0], addr[1]))
spider = NodeSpiderCrawl(self.protocol, self.node, nodes, self.ksize, self.alpha)
return spider.find()
示例3: direct_message
def direct_message(self, nodeid, message):
"""Send direct message to a node.
Spidercrawls the network to find the node and sends the message
directly. This will fail if the node is behind a NAT and doesn't
have a public ip.
Args:
nodeid: 160bit nodeid of the reciever as bytes
message: iu-msgpack-python serializable message data
Returns:
Defered own transport address (ip, port) if successfull else None
"""
def found_callback(nodes):
nodes = filter(lambda n: n.id == nodeid, nodes)
if len(nodes) == 0:
return defer.succeed(None)
else:
async = self.protocol.callDirectMessage(nodes[0], message)
return async.addCallback(lambda r: r[0] and r[1] or None)
node = KademliaNode(nodeid)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
return defer.succeed(None)
spider = NodeSpiderCrawl(
self.protocol, node, nearest, self.ksize, self.alpha
)
return spider.find().addCallback(found_callback)
示例4: remove
def remove(self, key, value):
"""
For the given key remove the given list values from the set in the network.
"""
dkey = digest(key)
node = Node(dkey)
_log.debug("Server:remove %s" % base64.b64encode(dkey))
def remove_(nodes):
# if this node is close too, then store here as well
max_distance = max([n.distanceTo(node) for n in nodes]) if nodes else sys.maxint
if self.node.distanceTo(node) < max_distance:
try:
pvalue = json.loads(value)
self.set_keys.add(dkey)
if dkey in self.storage:
old_value = json.loads(self.storage[dkey])
new_value = list(set(old_value) - set(pvalue))
self.storage[dkey] = json.dumps(new_value)
_log.debug("%s local remove key: %s old: %s remove: %s new: %s" % (base64.b64encode(node.id), base64.b64encode(dkey), old_value, pvalue, new_value))
except:
_log.debug("Trying to remove somthing not a JSON coded list %s" % value, exc_info=True)
ds = [self.protocol.callRemove(n, dkey, value) for n in nodes]
return defer.DeferredList(ds).addCallback(self._anyRespondSuccess)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to set key %s" % key)
return defer.succeed(False)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
return spider.find().addCallback(remove_)
示例5: send_message
def send_message(self, nodeid, message):
"""
Send a message to a given node on the network.
"""
hexid = binascii.hexlify(nodeid)
self.log.debug("messaging '%s' '%s'" % (hexid, message))
node = Node(nodeid)
def found_callback(nodes):
self.log.debug("nearest nodes %s" % list(map(str, nodes)))
nodes = filter(lambda n: n.id == nodeid, nodes)
if len(nodes) == 0:
self.log.debug("couldnt find destination node")
return defer.succeed(None)
else:
self.log.debug("found node %s" % binascii.hexlify(nodes[0].id))
async = self.protocol.callMessage(nodes[0], message)
return async.addCallback(lambda r: r[0] and r[1] or None)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to find %s" % hexid)
return defer.succeed(None)
spider = NodeSpiderCrawl(self.protocol, node, nearest,
self.ksize, self.alpha)
return spider.find().addCallback(found_callback)
示例6: set
def set(self, key, value):
"""
Set the given key to the given value in the network.
"""
self.log.debug("setting '%s' = '%s' on network" % (key, value))
dkey = digest(key)
node = Node(dkey)
def store(nodes):
self.log.info("setting '%s' on %s" % (key, list(map(str, nodes))))
# if this node is close too, then store here as well
if self.node.distanceTo(node) < max([n.distanceTo(node) for n in nodes]):
self.storage[dkey] = value
ds = [self.protocol.callStore(n, dkey, value) for n in nodes]
d = defer.DeferredList(ds)
d.addCallback(self._anyRespondSuccess)
d.addErrback(self.onError)
return d
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to set key %s" % key)
return defer.succeed(False)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
d = spider.find()
d.addCallback(store)
d.addErrback(self.onError)
return d
示例7: append
def append(self, key, value):
"""
For the given key append the given list values to the set in the network.
"""
dkey = digest(key)
node = Node(dkey)
def append_(nodes):
# if this node is close too, then store here as well
if not nodes or self.node.distanceTo(node) < max([n.distanceTo(node) for n in nodes]):
try:
pvalue = json.loads(value)
self.set_keys.add(dkey)
if dkey not in self.storage:
_log.debug("%s local append key: %s not in storage set value: %s" % (base64.b64encode(node.id), base64.b64encode(dkey), pvalue))
self.storage[dkey] = value
else:
old_value_ = self.storage[dkey]
old_value = json.loads(old_value_)
new_value = list(set(old_value + pvalue))
_log.debug("%s local append key: %s old: %s add: %s new: %s" % (base64.b64encode(node.id), base64.b64encode(dkey), old_value, pvalue, new_value))
self.storage[dkey] = json.dumps(new_value)
except:
_log.debug("Trying to append something not a JSON coded list %s" % value, exc_info=True)
ds = [self.protocol.callAppend(n, dkey, value) for n in nodes]
return defer.DeferredList(ds).addCallback(self._anyRespondSuccess)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to set key %s" % key)
_log.debug("There are no known neighbors to set key %s" % key)
return defer.succeed(False)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
return spider.find().addCallback(append_)
示例8: append
def append(self, key, value):
"""
Append the given key to the given value in the network.
"""
self.log.debug("setting '%s' = '%s' on network" % (key, value))
dkey = digest(key)
def append(nodes, mid):
self.log.info("setting '%s' on %s" % (key, map(str, nodes)))
# TODO: Must add transaction ID so we dont append multiple times.
print "org mid", mid
mid = uuid.uuid1().hex
print "new mid", mid
ds = [self.protocol.callAppend(node, mid, dkey, value) for node in nodes]
return defer.DeferredList(ds).addCallback(self._anyRespondSuccess)
node = Node(dkey)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to set key %s" % key)
return defer.succeed(False)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
return spider.find().addCallback(append, "hej")
示例9: remove
def remove(self, key, value):
"""
For the given key remove the given list values from the set in the network.
"""
dkey = digest(key)
def remove(nodes):
ds = [self.protocol.callRemove(node, dkey, value) for node in nodes]
return defer.DeferredList(ds).addCallback(self._anyRespondSuccess)
node = Node(dkey)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to set key %s" % key)
return defer.succeed(False)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
return spider.find().addCallback(remove)
示例10: set
def set(self, key, value):
"""
Set the given key to the given value in the network.
"""
self.log.debug("setting '%s' = '%s' on network" % (key, value))
dkey = digest(key)
def store(nodes):
self.log.info("setting '%s' on %s" % (key, map(str, nodes)))
ds = [self.protocol.callStore(node, dkey, value) for node in nodes]
return defer.DeferredList(ds).addCallback(self._anyRespondSuccess)
node = Node(dkey)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
self.log.warning("There are no known neighbors to set key %s" % key)
return defer.succeed(False)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
return spider.find().addCallback(store)
示例11: _refresh_table
async def _refresh_table(self):
"""
Refresh buckets that haven't had any lookups in the last hour
(per section 2.3 of the paper).
"""
ds = []
for node_id in self.protocol.getRefreshIDs():
node = Node(node_id)
nearest = self.protocol.router.findNeighbors(node, self.alpha)
spider = NodeSpiderCrawl(self.protocol, node, nearest,
self.ksize, self.alpha)
ds.append(spider.find())
# do our crawling
await asyncio.gather(*ds)
# now republish keys older than one hour
for dkey, value in self.storage.iteritemsOlderThan(3600):
await self.set_digest(dkey, value)
示例12: refreshTable
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 key, value in self.storage.iteritemsOlderThan(3600):
ds.append(self.set(key, value))
return defer.gatherResults(ds)
return defer.gatherResults(ds).addCallback(republishKeys)
示例13: direct_message
def direct_message(self, nodeid, message):
"""Send direct message to a node.
Spidercrawls the network to find the node and sends the message
directly. This will fail if the node is behind a NAT and doesn't
have a public ip.
Args:
nodeid: 160bit nodeid of the reciever as bytes
message: iu-msgpack-python serializable message data
Returns:
Defered own transport address (ip, port) if successfull else None
"""
hexid = binascii.hexlify(nodeid)
self.log.debug("Direct messaging %s: %s" % (hexid, message))
def found_callback(nodes):
nodes = filter(lambda n: n.id == nodeid, nodes)
if len(nodes) == 0:
msg = "{0} couldn't find destination node {1}"
self.log.warning(msg.format(self.get_hex_id(), hexid))
return defer.succeed(None)
else:
self.log.debug("found node %s" % binascii.hexlify(nodes[0].id))
async = self.protocol.callDirectMessage(nodes[0], message)
return async.addCallback(lambda r: r[0] and r[1] or None)
node = Node(nodeid)
nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0:
msg = "{0} has no known neighbors to find {1}"
self.log.warning(msg.format(self.get_hex_id(), hexid))
return defer.succeed(None)
spider = NodeSpiderCrawl(self.protocol, node, nearest,
self.ksize, self.alpha)
return spider.find().addCallback(found_callback)