-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerThread.py
70 lines (61 loc) · 2.2 KB
/
ServerThread.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
import threading
import time
import select
import dropbox
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import ast
class ServerThread(threading.Thread):
def __init__(self, server, conn, addr):
super(ServerThread, self).__init__()
self._stop = threading.Event()
self.server = server
self.conn = conn
self.addr = addr
self.last_activity = time.time()
self.server.server_socket.settimeout(5)
def run(self):
# Start thread to handle recieving data
print "Thread Created"
print "Recieving Data from ", self.addr
public_key = server.publickey.exportKey()
print "Sending ", self.addr, " public key"
self.server.server_socket.send(conn, public_key)
fail_count = 0
message = None
while fail_count < 3:
print "Waiting for client reply:"
ready = select.select([self.server.server_socket.ssock], [], [], 10)
if ready[0] :
message = self.server.server_socket.rec(self.server.server_socket)
print "recieved key from client"
break
else:
fail_count += 1
# Get public key from client
if message:
client_public_key = RSA.importKey(message)
print self.client_public_key.exportKey()
while not self.stopped():
try:
data = self.server.server_socket.rec(conn)
print "Recieved: ", data
self.last_activity = time.time()
self.conn.settimeout(120)
# Input data into server
sent = self.server.input_data(data, conn, addr)
# check if send was successful
if sent == 0:
raise RuntimeError("ERROR: Message not sent")
elif sent == "Ended Connection":
self.stop()
except Exception,e:
if e == 'timed out':
print self.addr, " Socket Timeout"
self.stop()
print addr, " is exiting"
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()