本文整理汇总了Python中Packet.send方法的典型用法代码示例。如果您正苦于以下问题:Python Packet.send方法的具体用法?Python Packet.send怎么用?Python Packet.send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Packet
的用法示例。
在下文中一共展示了Packet.send方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: listenAndRespond
# 需要导入模块: import Packet [as 别名]
# 或者: from Packet import send [as 别名]
def listenAndRespond(self):
s = socket(AF_INET, SOCK_DGRAM)
s.bind((HOST,PORT))
addr = (HOST,PORT)
while True:
data, addr = s.recvfrom(1024)
print data
packet = json.loads(data)
if packet["type"] == "JOINING_NETWORK_SIMPLIFIED":
print "Add node to routing table"
self.routing_table.append((packet["node_id"], packet["from_ip"]))
print self.routing_table
if self.node_id != packet["target_id"]:
closest_id, closest_ip = self.findClosestNode(packet["target_id"])
print closest_id
Packet("JOINING_NETWORK_RELAY_SIMPLIFIED", node_id=packet["node_id"], target_id=packet["target_id"], gateway_id=self.node_id).send(self.sendSock, closest_ip)
print 'sending packet on to ' + closest_ip
elif packet["type"] == "JOINING_NETWORK_RELAY_SIMPLIFIED":
p = Packet("ROUTING_INFO", gateway_id = packet["gateway_id"], node_id = packet["node_id"], from_ip = self.ip, route_table = self.routing_table)
_, closest_ip = findClosestNode(packet["gateway_id"])
p.send(self.sendSock, closest_ip)
elif packet["type"] == "INDEX":
print "Adding index"
if packet["target_id"] == self.node_id:
print "actually adding to this node's links"
self.url.append(packet["link"])
print self.url
else:
_, closest_ip = findClosestNode(packet['target_id'])
Packet("INDEX", node_id=packet["sender_id"], target_id=packet["target_id"], keyword=packet["keyword"], link=packet['link']).send(self.sendSock, closest_ip)
elif packet["type"] == "ROUTING_INFO":
if self.node_id == packet["gateway_id"]:
_, closest_ip = findClosestNode(packet["node_id"])
p = Packet("ROUTING_INFO", gateway_id = packet["gateway_id"], node_id= packet["node_id"], from_ip = packet["from_ip"], route_table=packet["route_table"])
p.send(self.sendSock, closest_ip)
elif self.node_id == packet["target_id"]:
self.routing_table.append(packet["route_table"])
else:
_, closest_ip = findClosestNode(packet["gateway_id"])
p = Packet("ROUTING_INFO", gateway_id = packet["gateway_id"], node_id=packet["node_id"], from_ip=packet["from_ip"], route_table=packet["route_table"])
p.send(self.sendSock, closest_ip)
elif packet["type"] == "SEARCH":
print 'received SEARCH packet'
if packet['node_id'] == self.node_id:
_, closest_ip = findClosestNode(packet["sender_id"])
p =Packet("SERACH_RESPONSE", word=packet["word"], node_id=packet["sender_id"], sender_id=self.node_id, response=self.url)
p.send(self.sendSock, closest_ip)
else:
_, closest_ip = findClosestNode(packet["node_id"])
p = Packet("SEARCH", word = packet["word"], sender_id = packet["sender_id"], node_id=packet["node_id"])
p.send(self.sendSock, closest_ip)
elif packet["type"] == "SEARCH_RESPONSE":
if packet["target_id"] == self.node_id:
print 'Search Results: '
for result in packet['response']:
print result
else:
_, closest_ip = findClosestNode(packet["node_id"])
p = Packet("SEARCH_RESPONSE", word=packet["word"], node_id= packet["node_id"], sender_id = packet["sender_id"], response=packet['response'])
p.send(self.sendSock, closest_ip)
elif packet["type"] == "LEAVING_NETWORK":
print "received a leaving packet"
print "current routing table"
print self.routing_table
for id,ip in self.routing_table:
if id == packet["node_id"]:
self.routing_table.remove((id, ip))
print 'new routing table'
print self.routing_table
示例2: search
# 需要导入模块: import Packet [as 别名]
# 或者: from Packet import send [as 别名]
def search(self, word):
print 'searching'
target_id, target_ip = findClosestNode(hashCode(word))
p = Packet("SEARCH", word=word, node_id=target_id, sender_id=self.node_id)
p.send(self.sendSock, target_ip)
示例3: index
# 需要导入模块: import Packet [as 别名]
# 或者: from Packet import send [as 别名]
def index(self):
print 'indexing'
p = Packet('INDEX', target_id=self.node_id, sender_id=self.node_id, keyword="Abba", link=["domain.com"])
p.send(self.sendSock, "127.0.0.1")
示例4: leave
# 需要导入模块: import Packet [as 别名]
# 或者: from Packet import send [as 别名]
def leave(self):
print 'leaving network'
p = Packet('LEAVING_NETWORK', node_id=8)
for _,ip in self.routing_table:
print ip
p.send(self.sendSock, ip)
示例5: join
# 需要导入模块: import Packet [as 别名]
# 或者: from Packet import send [as 别名]
def join(self):
print 'joining'
p = Packet("JOINING_NETWORK_SIMPLIFIED", node_id=self.node_id+5, target_id = 2, from_ip="127.0.0.1")
p.send(self.sendSock, "127.0.0.1")