user.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python
  2. import socket
  3. import _thread
  4. import time
  5. import json
  6. from padra import padra
  7. import threading
  8. LenFunc = 4
  9. padra = padra(LenFunc)
  10. NumberOfOracles = 3
  11. listOfAll = []
  12. def broadcast():
  13. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  14. msg = bytes("Message: ",'utf-8')
  15. sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  16. sock.sendto(msg, ("255.255.255.255", 6666))
  17. while 1:
  18. m = sock.recvfrom(1024)
  19. listOfAll.append(((m[1][0]),str(int(m[0]))))
  20. print(listOfAll)
  21. connectedOracles = []
  22. def connection():
  23. while len(listOfAll)<NumberOfOracles: pass
  24. for _ in listOfAll:
  25. if len(connectedOracles) < NumberOfOracles:
  26. o = oracle(None,None,None,None,"127.0.0.1",int(_[1]))
  27. o.start()
  28. connectedOracles.append(o)
  29. print(connectedOracles)
  30. time.sleep(2)
  31. print(connectedOracles)
  32. padra.xor(connectedOracles,"11111111111111111111111111223344",
  33. "11111111111111111111111111223341")
  34. class oracle(threading.Thread):
  35. def __init__(self, group, target, name,
  36. verbose,HOST,PORT):
  37. super(oracle,self).__init__(group, target,
  38. name, verbose)
  39. print(HOST,PORT)
  40. self.port = PORT
  41. self.Host = HOST
  42. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  43. s.connect((HOST, PORT))
  44. data = s.recv(1024)
  45. ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  46. print(ss)
  47. ss.connect((HOST,PORT))
  48. self.s = ss
  49. self.history = []
  50. _thread.start_new_thread( self.receive, ())
  51. def run(self):
  52. # logging.debug('running')
  53. return
  54. # 4 char = len of data + data
  55. def send(self,Message):
  56. Message=bytes(Message,'utf-8')
  57. self.s.sendall(Message)
  58. self.history.append(("send",Message))
  59. print("Send",Message)
  60. def receive(self):
  61. while True:
  62. data = self.s.recv(1024)
  63. if not len:
  64. break
  65. self.history.append(("receive",data))
  66. print("receive",data)
  67. try:
  68. _thread.start_new_thread( broadcast, ())
  69. _thread.start_new_thread( connection, ())
  70. except:
  71. print ("Error: unable to start thread")
  72. while 1:
  73. pass