-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvikram-central-server-1.py
More file actions
175 lines (141 loc) · 5.49 KB
/
vikram-central-server-1.py
File metadata and controls
175 lines (141 loc) · 5.49 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from tkinter import *
#import numpy as np
import socket, threading
import random
import datetime as dt
import threading
import argparse
import sqlite3
import time
from datetime import datetime
from datetime import date
dbc=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"] # month name bro
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--start_over", action="store_true")
return parser.parse_args()
class ClientThread(threading.Thread):
def __init__(self, clientAddress, clientsocket):
threading.Thread.__init__(self)
self.csocket = clientsocket
print("New connection added to Central Server: ", clientAddress)
def run(self):
print("Connection from local Server: ", clientAddress)
msg = ''
conn = sqlite3.connect('winkle.db') # Opens the SQL Database
print("Opened database successfully")
c=conn.cursor()
c.execute('SELECT * FROM twinkle') #reading from the table
fetchall=c.fetchall()
print(fetchall) # you might know why the below lines don't work , you first need to select
twinklet_idarray=[]
for row in fetchall:
twinklet_idarray.append(row[1])
print(twinklet_idarray)
while True:
try:
data = self.csocket.recv(1024)
msg = data.decode()
if not msg == '':
twinklet_id = Datastoringfun(msg, conn)
day = dayobtain(msg,conn) #obtaining the day from the message
month=monthobtain(msg,conn)
# print("twinklet_id:",twinklet_id,"\ttype:",type(twinklet_id))
if twinklet_id in twinklet_idarray:
self.csocket.send('Open'.encode('utf-8'))
auth_status='Open'
else:
self.csocket.send('Dont_open'.encode('utf-8'))
auth_status='Dont_Open'
print("Authentication Status sent")
print("\n")
attendancemark(auth_status,twinklet_id,day,month) #Marking the attendance buddy
except UnicodeDecodeError:
print("Error in Decoding the String")
except AttributeError:
print("Attribute Incorrect")
def Datastoringfun(data_element, conn):
data_element=data_element.split('*')
sno= data_element[0]
twinklet_id = int(data_element[1])
return twinklet_id
def dayobtain(data_element,conn):
data_element=data_element.split('*') ## OBTAINING DAY(EMBEDDED IN THE MESSAGE) BY SPLICING THE MESSAGE
date= data_element[2]
datelist=date.split("/")
day=datelist[0]
day=int(day)
day=str(day)
return day
def monthobtain(data_element,conn):
data_element=data_element.split('*') ## OBTAINING Month(EMBEDDED IN THE MESSAGE) BY SPLICING THE MESSAGE
date= data_element[2]
datelist=date.split("/")
month=datelist[1]
for i in range(len(dbc)):
if(int(month)==i+1):
mont=dbc[i]
return mont
def markabsentinitial(): #initially when the day starts, i mark absemt for everybody, if they are present, this 'a' will get replaced!!!!
today = date.today()
day=today.strftime("%d")
month = today.strftime("%m")
mont=''
for i in range(len(dbc)):
if(int(month)== i+1):
mont=dbc[i]
conn2= sqlite3.connect(mont+'.db')
d=conn2.cursor()
print("Opened Database",mont)
d.execute('SELECT * FROM twinkle')
day1="day"+day
d.execute('UPDATE twinkle SET '+day1+'="a"')
conn2.commit()
d.close()
conn2.close()
def attendancemark(auth_status,twinklet_id,day,month):
conn1 = sqlite3.connect(month+'.db')
print("Opened database successfully "+month)
d=conn1.cursor()
d.execute('SELECT * FROM twinkle')
fetchalll=d.fetchall()
day1="day"+day
if(auth_status=='Open'):
for row in fetchalll:
if(twinklet_id==row[1]):
d.execute('UPDATE twinkle SET '+day1+'="p" WHERE twinkletid= ? ',(row[1],))
conn1.commit()
d.close()
conn1.close()
if __name__ == "__main__":
global count
global local_count
global auth_status
global twinklet_id
global day
count = 0
args = get_arguments()
if args.start_over is True:
count = 0
else:
try:
count = np.load("checkpoint.npy")
except:
count = 0
host = "192.168.1.2" #Ip adress
port = 9999
Serv_Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Use only for TCP
Serv_Socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Serv_Socket.bind((host, port)) #Serv_Socket.bind((host,port))
print("Vanakkam ..!")
print("Central Server started")
print("Waiting for local server..")
markabsentinitial() #refer to the definition
#local_count = int(count)
while True:
Serv_Socket.listen(1)
clientsock, clientAddress = Serv_Socket.accept()
print("Connection accepted...")
newthread = ClientThread(clientAddress, clientsock)
newthread.start()
clientsock.close()