-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiffs.py
More file actions
executable file
·75 lines (67 loc) · 2.53 KB
/
diffs.py
File metadata and controls
executable file
·75 lines (67 loc) · 2.53 KB
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
import socket
import sys
import traceback
from threading import Thread
def start_server():
host = "127.0.0.1"
port = 8888 # arbitrary non-privileged port
y = 9 # receiver's private key
g,n = 7,11
print '******* Prime Numbers decided : ',g,' , ',n,'*******'
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # SO_REUSEADDR flag tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire
print("Socket created")
try:
soc.bind((host, port))
except:
print("Bind failed. Error : " + str(sys.exc_info()))
sys.exit()
B = str((g**y)%n)
soc.listen(5) # queue up to 5 requests
print("Socket now listening")
# infinite loop- do not reset for every requests
connection, address = soc.accept()
ip, port = str(address[0]), str(address[1])
print("Connected with " + ip + ":" + port)
try:
Thread(target=client_thread, args=(connection, ip, port,B)).start()
except:
print("Thread did not start.")
traceback.print_exc()
soc.close()
def client_thread(connection, ip, port, B, max_buffer_size = 5120):
client_input = receive_input(connection, max_buffer_size)
if "--QUIT--" in client_input:
print("Client is requesting to quit")
connection.close()
print("Connection " + ip + ":" + port + " closed")
is_active = False
else:
print 'Computed B : ',B
temp = raw_input('Sending B...')
connection.sendall(B.encode("utf8"))
print 'Send successful!'
def receive_input(connection, max_buffer_size):
y = 9 # receiver's private key
g,n = 7,11
client_input = connection.recv(max_buffer_size)
client_input_size = sys.getsizeof(client_input)
if client_input_size > max_buffer_size:
print("The input size is greater than expected {}".format(client_input_size))
decoded_input = client_input.decode("utf8").rstrip() # decode and strip end of line
A = int(decoded_input)
print 'Received : ',decoded_input
print 'Shared key k1 : ',(A**y)%n
return str(A)
start_server()
''' ************************ OUTPUT ****************************
******* Prime Numbers decided : 7 , 11 *******
Socket created
Socket now listening
Connected with 127.0.0.1:42230
Received : 2
Shared key k1 : 6
Computed B : 8
Sending B...
Send successful!
'''