本文整理汇总了Python中contact.Contact.last_seen方法的典型用法代码示例。如果您正苦于以下问题:Python Contact.last_seen方法的具体用法?Python Contact.last_seen怎么用?Python Contact.last_seen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类contact.Contact
的用法示例。
在下文中一共展示了Contact.last_seen方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: on_req
# 需要导入模块: from contact import Contact [as 别名]
# 或者: from contact.Contact import last_seen [as 别名]
def on_req(self, remote_host, remote_port, *args, **kwargs):
node_id = kwargs['id']
local_host = kwargs['local_host']
local_port = kwargs['local_port']
bootstrap = kwargs.get('bootstrap', False)
# update contact's `last_seen`, or add contact
c = self.node.rt.contacts.get(node_id)
if c:
c.id = node_id
c.last_seen = time.time()
else:
c = self.node.rt.contacts.get((remote_host, remote_port))
if c:
c.id = node_id
c.last_seen = time.time()
else:
# add_contact
c = self.node.rt.add_contacts.get(node_id)
if c:
self.node.rt.add_contacts.remove(c)
self.node.rt.contacts.add(c)
c.id = node_id
c.last_seen = time.time()
print(PrintColors.GREEN + 'new contact [DISCOVERY REQ]:', self.node, c, PrintColors.END)
else:
c = self.node.rt.add_contacts.get((remote_host, remote_port))
if c:
self.node.rt.add_contacts.remove(c)
self.node.rt.contacts.add(c)
c.id = node_id
c.last_seen = time.time()
print(PrintColors.GREEN + 'new contact [DISCOVERY REQ]:', self.node, c, PrintColors.END)
else:
# remove_contact
c = self.node.rt.remove_contacts.get(node_id)
if c:
self.node.rt.remove_contacts.remove(c)
self.node.rt.contacts.add(c)
c.id = node_id
c.last_seen = time.time()
print(PrintColors.GREEN + 'new contact [DISCOVERY REQ]:', self.node, c, PrintColors.END)
else:
c = self.node.rt.remove_contacts.get((remote_host, remote_port))
if c:
self.node.rt.remove_contacts.remove(c)
self.node.rt.contacts.add(c)
c.id = node_id
c.last_seen = time.time()
print(PrintColors.GREEN + 'new contact [DISCOVERY REQ]:', self.node, c, PrintColors.END)
else:
c = Contact(
id = node_id,
local_host = local_host,
local_port = local_port,
remote_host = remote_host,
remote_port = remote_port,
bootstrap = bootstrap,
)
# because `c` is requesting to discover nodes
# put it into known active contacts
c.last_seen = time.time()
self.node.rt.contacts.add(c)
print(PrintColors.GREEN + 'new contact [DISCOVERY REQ]:', self.node, c, PrintColors.END)
# forward to res_discover_nodes
self.res(remote_host, remote_port, *args, **kwargs)
示例2: on_res
# 需要导入模块: from contact import Contact [as 别名]
# 或者: from contact.Contact import last_seen [as 别名]
def on_res(self, remote_host, remote_port, res):
node_id = res['id']
local_host = res['local_host']
local_port = res['local_port']
contacts = res['contacts']
bootstrap = res.get('bootstrap', False)
# update contact's `last_seen`, or add contact
c = self.node.rt.contacts.get(node_id)
if c:
c.id = node_id
c.last_seen = time.time()
else:
c = self.node.rt.contacts.get((remote_host, remote_port))
if c:
c.id = node_id
c.last_seen = time.time()
else:
# add_contact
c = self.node.rt.add_contacts.get(node_id)
if c:
self.node.rt.add_contacts.remove(c)
self.node.rt.contacts.add(c)
c.id = node_id
c.last_seen = time.time()
print(PrintColors.GREEN + 'new contact [DISCOVERY ON RES]:', self.node, c, PrintColors.END)
else:
c = self.node.rt.add_contacts.get((remote_host, remote_port))
if c:
self.node.rt.add_contacts.remove(c)
self.node.rt.contacts.add(c)
c.id = node_id
c.last_seen = time.time()
print(PrintColors.GREEN + 'new contact [DISCOVERY ON RES]:', self.node, c, PrintColors.END)
else:
# remove_contact
c = self.node.rt.remove_contacts.get(node_id)
if c:
self.node.rt.remove_contacts.remove(c)
self.node.rt.contacts.add(c)
c.id = node_id
c.last_seen = time.time()
print(PrintColors.GREEN + 'new contact [DISCOVERY ON RES]:', self.node, c, PrintColors.END)
else:
c = self.node.rt.remove_contacts.get((remote_host, remote_port))
if c:
self.node.rt.remove_contacts.remove(c)
self.node.rt.contacts.add(c)
c.id = node_id
c.last_seen = time.time()
print(PrintColors.GREEN + 'new contact [DISCOVERY ON RES]:', self.node, c, PrintColors.END)
else:
c = Contact(
id = node_id,
local_host = local_host,
local_port = local_port,
remote_host = remote_host,
remote_port = remote_port,
bootstrap = bootstrap,
)
# because `c` is requesting to discover nodes
# put it into known active contacts
c.last_seen = time.time()
self.node.rt.contacts.add(c)
print(PrintColors.GREEN + 'new contact [DISCOVERY ON RES]:', self.node, c, PrintColors.END)
# update discovered nodes/contacts
for cd in contacts:
node_id = cd['id']
local_host = cd['local_host']
local_port = cd['local_port']
remote_host = cd['remote_host']
remote_port = cd['remote_port']
bootstrap = cd.get('bootstrap', False)
# update contact's `last_seen`, or add contact
c = self.node.rt.contacts.get(node_id)
if c:
c.id = node_id
else:
c = self.node.rt.contacts.get((remote_host, remote_port))
if c:
c.id = node_id
else:
# add_contact
c = self.node.rt.add_contacts.get(node_id)
if c:
c.id = node_id
else:
c = self.node.rt.add_contacts.get((remote_host, remote_port))
#.........这里部分代码省略.........