-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
118 lines (96 loc) · 3.19 KB
/
App.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import socket
import threading
from string import *
from DHCalculator import *
from getIp import *
LOCAL_IP = ""
FIRST_CONNECTION_FLAG = False
calculator = DHCalculator()
G = 0
P = 0
A = 0
class Handler(threading.Thread):
def __init__(self,local_host,local_port):
threading.Thread.__init__(self,name="messenger_receiver")
self.host = local_host
self.port = local_port
self.dataRecived = ""
def listen(self):
global FIRST_CONNECTION_FLAG
global G
global P
global A
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((self.host,self.port))
sock.listen(10)
while True:
connection, client_address = sock.accept()
try:
full_message = ""
while True:
data = connection.recv(16)
full_message +=data.decode("utf-8")
if not data:
if full_message.count(",") == 1:
self.dataRecived = full_message.split(",")
print("data recebida",self.dataRecived)
G = self.dataRecived[0]
P = self.dataRecived[1]
print("P e G transportados:",G,P)
break
finally:
connection.shutdown(2)
connection.close()
def run(self):
self.listen()
class Sender(threading.Thread):
def __init__(self,remote_host,remote_port):
threading.Thread.__init__(self,name="messenger_sender")
self.host = remote_host
self.port = remote_port
def run(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
global FIRST_CONNECTION_FLAG
global G
global P
global A
message = ""
while True:
try:
s.connect((self.host,self.port))
except Exception:
FIRST_CONNECTION_FLAG = True
G = calculator.generateGP()
P = calculator.generateGP()
else:
print("P e G hehe",P,G)
if((P and G) != 0):
print("P e G globais:",P,G)
break
message = str(G)+","+str(P)
s.send(message.encode("utf-8"))
s.shutdown(2)
s.close()
def execute():
a = int(input("Type your local key:\n>> "))
calculator.setA(a)
localport = int(input("LocalPORT:"))
remotehost = input("remoteIP:")
remoteport = int(input("remotePORT:"))
print("Waiting for another peer")
receiver = Handler(LOCAL_IP,localport)
sender = Sender(remotehost,remoteport)
treads = [sender.start(),receiver.start()]
def main():
LOCAL_IP = get_lan_ip()
dialog = input("Your IP is: "+LOCAL_IP+"?(y/n)\n>>")
if(dialog == "y"):
execute()
elif(dialog == "n"):
LOCAL_IP = input("Local IP:\n>>")
execute()
else:
print("Invalid input!")
main()
if __name__ == "__main__":
main()