-
Notifications
You must be signed in to change notification settings - Fork 38
/
voter.py
99 lines (77 loc) · 3.04 KB
/
voter.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
import tkinter as tk
import socket
from tkinter import *
from VotingPage import votingPg
def establish_connection():
try:
host = socket.gethostname()
port = 4001
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_socket.connect((host, port))
print(client_socket)
message = client_socket.recv(1024) #connection establishment message #1
if(message.decode()=="Connection Established"):
return client_socket
else:
return 'Failed'
except:
print("Connection Failed, check if server is running...")
return 'Failed'
def failed_return(root,frame1,client_socket,message):
for widget in frame1.winfo_children():
widget.destroy()
message = message + "... \nTry again..."
Label(frame1, text=message, font=('Helvetica', 12, 'bold')).grid(row = 1, column = 1)
try:
client_socket.close()
except:
return
def log_server(root,frame1,client_socket,voter_ID,password):
if(not (voter_ID and password)):
voter_ID = "0"
password = "x"
message = voter_ID + " " + password
client_socket.send(message.encode()) #2
message = client_socket.recv(1024) #Authenticatication message
message = message.decode()
if(message=="Authenticate"):
votingPg(root, frame1, client_socket)
elif(message=="VoteCasted"):
message = "Vote has Already been Cast"
failed_return(root,frame1,client_socket,message)
elif(message=="InvalidVoter"):
message = "Invalid Voter"
failed_return(root,frame1,client_socket,message)
else:
message = "Server Error"
failed_return(root,frame1,client_socket,message)
def voterLogin(root,frame1):
client_socket = establish_connection()
if(client_socket == 'Failed'):
message = "Connection failed"
failed_return(root,frame1,client_socket,message)
return
root.title("Voter Login")
for widget in frame1.winfo_children():
widget.destroy()
Label(frame1, text="Voter Login", font=('Helvetica', 18, 'bold')).grid(row = 0, column = 2, rowspan=1)
Label(frame1, text="").grid(row = 1,column = 0)
Label(frame1, text="Voter ID: ", anchor="e", justify=LEFT).grid(row = 2,column = 0)
Label(frame1, text="Password: ", anchor="e", justify=LEFT).grid(row = 3,column = 0)
voter_ID = tk.StringVar()
# name = tk.StringVar()
password = tk.StringVar()
e1 = Entry(frame1, textvariable = voter_ID)
e1.grid(row = 2,column = 2)
e3 = Entry(frame1, textvariable = password, show = '*')
e3.grid(row = 3,column = 2)
sub = Button(frame1, text="Login", width=10, command = lambda: log_server(root, frame1, client_socket, voter_ID.get(), password.get()))
Label(frame1, text="").grid(row = 4,column = 0)
sub.grid(row = 5, column = 3, columnspan = 2)
frame1.pack()
root.mainloop()
# if __name__ == "__main__":
# root = Tk()
# root.geometry('500x500')
# frame1 = Frame(root)
# voterLogin(root,frame1)