Advertisement
Vagrum

P2P Simple Node

Feb 4th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import socket
  2. import sys
  3.  
  4. class Node:
  5.     ip = ""
  6.     port = "*"
  7.  
  8.     def __init__(self, address):
  9.         self.ip = str(address[0])
  10.         self.port = int(address[1])
  11.  
  12.     def __init__(self, ip, port):
  13.         self.ip = str(ip)
  14.         self.port = int(port)
  15.  
  16. class P2P:
  17.     node = None
  18.     connectedNodes = []
  19.  
  20.     def __checkForConnection(self):
  21.         try:
  22.             data, nodeAddress = self.sock.recvfrom(1024)
  23.  
  24.             if not node in connectedNodes:
  25.                 print "Connection-request from " + nodeAddress[0] + ":" + nodeAddress[1] + ". Accept? (y/n)"
  26.  
  27.                 choice = raw_input()
  28.  
  29.                 if choice == "y":
  30.                     connectedNodes.append(Node(nodeAddress))
  31.  
  32.                     self.sock.sendto("Connection-request accepted.", node.address)
  33.  
  34.                     print "\t" + nodeAddress[0] + ":" + nodeAddress[1] + " has connected to us."
  35.                 else:
  36.                     self.sock.sendto("Connection-request denied.", nodeAddress)
  37.         except:
  38.             pass
  39.  
  40.     def __connect(self, node):
  41.         print "\tTrying to connect to " + node.ip + ":" + str(node.port) + "..."
  42.  
  43.         self.sock.sendto(b"Hello.", (node.ip, node.port))
  44.  
  45.         waiting = True
  46.  
  47.         while waiting:
  48.             data = self.sock.recvfrom(1024)
  49.  
  50.             if not data == None:
  51.                 print "\t'" + data + "'"
  52.  
  53.                 if data == "Connection-request accepted.":
  54.                     connectedNodes.append(node)
  55.  
  56.                 waiting = False
  57.  
  58.         return
  59.  
  60.     def __ask(self):
  61.         print "(" + self.node.ip + "): "
  62.         raw_data = raw_input()
  63.         data = raw_data.split(' ')
  64.  
  65.         return data
  66.  
  67.     def __init__(self):
  68.         ip = socket.gethostbyname(socket.gethostname())
  69.  
  70.         print "Hello " + ip + ". What port would you like to listen to today?"
  71.  
  72.         portEnteredCorrectly = False
  73.  
  74.         while not portEnteredCorrectly:
  75.             port = int(input());
  76.  
  77.             if not port == None:
  78.                 self.node = Node(ip, port)
  79.  
  80.                 portEnteredCorrectly = True
  81.             else:
  82.                 print "Port entered incorrectly. Try again."
  83.  
  84.  
  85.         self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  86.         self.sock.bind((self.node.ip, self.node.port))
  87.         self.sock.setblocking(0)
  88.  
  89.         print "Listening on port " + str(self.node.port) + "..."
  90.  
  91.         while True:
  92.             self.__checkForConnection()
  93.  
  94.             inputtedValues = self.__ask()
  95.  
  96.             amValues = len(inputtedValues)
  97.  
  98.             if amValues == 1:
  99.                 if inputtedValues[0] == "exit":
  100.                     sys.exit(0)
  101.             elif amValues == 3:
  102.                 if inputtedValues[0] == "connect":
  103.                     self.__connect(Node(inputtedValues[1], inputtedValues[2]))
  104.                 else:
  105.                     print "\tInput not recognised."
  106.             else:
  107.                 print "\tInput not recognised."
  108.  
  109.         self.sock.close()
  110. if __name__ == "__main__":
  111.     P2P()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement