-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
76 lines (57 loc) · 2.03 KB
/
Server.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
import socket
import threading
import dframe as df
from threading import Thread
from dframe import *
lock = threading.Lock()
def client_thread(connection):
data = connection.recv(1024) #receiving voter details #2
#verify voter details
log = (data.decode()).split(' ')
log[0] = int(log[0])
if(df.verify(log[0],log[1])): #3 Authenticate
if(df.isEligible(log[0])):
print('Voter Logged in... ID:'+str(log[0]))
connection.send("Authenticate".encode())
else:
print('Vote Already Cast by ID:'+str(log[0]))
connection.send("VoteCasted".encode())
else:
print('Invalid Voter')
connection.send("InvalidVoter".encode())
data = connection.recv(1024) #4 Get Vote
print("Vote Received from ID: "+str(log[0])+" Processing...")
lock.acquire()
#update Database
if(df.vote_update(data.decode(),log[0])):
print("Vote Casted Sucessfully by voter ID = "+str(log[0]))
connection.send("Successful".encode())
else:
print("Vote Update Failed by voter ID = "+str(log[0]))
connection.send("Vote Update Failed".encode())
#5
lock.release()
connection.close()
def voting_Server():
serversocket = socket.socket()
host = socket.gethostname()
port = 4001
ThreadCount = 0
try :
serversocket.bind((host, port))
except socket.error as e :
print(str(e))
print("Waiting for the connection")
serversocket.listen(10)
print( "Listening on " + str(host) + ":" + str(port))
while True :
client, address = serversocket.accept()
print('Connected to :', address)
client.send("Connection Established".encode()) ### 1
t = Thread(target = client_thread,args = (client,))
t.start()
ThreadCount+=1
# break
serversocket.close()
if __name__ == '__main__':
voting_Server()