-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver 2.txt
276 lines (235 loc) · 10.8 KB
/
server 2.txt
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import socket
import sys
import select
import time
import os
class mainServer:
global regEntry
regEntry = {}
global clients
clients = {'Moiz':'09', 'Vineet':'08'}
def __init__ (self):
if os.path.isfile('PA4_logFile.txt'):
os.remove('PA4_logFile.txt')
if os.path.isfile('regEntry.txt'):
openfile = open('regEntry.txt', 'rb')
readfile = openfile.read()
openfile.close()
if str(readfile) == '{}':
os.remove('regEntry.txt')
if os.path.isfile('clientList.txt'):
openfile = open('clientList.txt', 'rb')
readfile = openfile.read()
openfile.close()
if str(readfile) == '{}':
os.remove('clientList.txt')
self.host = ''
self.port = 9090
self.backlog = 5
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.bind((self.host,self.port))
self.s.listen(self.backlog)
def clientConnection(self):
self.client_socket, address = self.s.accept()
self. query = self.client_socket.send('Hi, This is server\n')
self.clientPort = address[1]
self.clientIp = address[0]
self.Logging(self.clientPort, self.clientIp)
while True:
self.data = self.client_socket.recv(4096*100)
print 'Received: ', str(self.data)
self.client_socket.send('Are you are new or a returning user?')
self.checkFlag = self.checkStatus(address)
if self.data.find('Give me the client list') >= 0:
self.clientList(self.data, address)
if self.data.find('Quit') >= 0:
self.check = self.data.split(' ',2)
self.usName = self.check[1]
self.deReg(self.usName,address)
if self.checkFlag == 0:
break
def checkStatus(self,address):
self.data = self.client_socket.recv(4096*100)
try:
if self.data.find('N') >= 0:
self.storeInfo(self.data,address)
return 0
elif self.data.find('R') >= 0:
self.authenticate(self.data,address)
return 0
else:
return 1
except KeyError:
print self.client_socket.send('Enter a valid command')
self.checkStatus(address)
def authenticate(self, data, address):
self.client = {}
self.client_socket.send('Give your Username and Password')
print 'Auth entered'
self.data = self.client_socket.recv(4096*100)
if self.data.find('||||')>=0:
self.store = self.data.split('||||',2)
self.blockName = self.store[0]
self.flag = self.blockCheck(self.blockName, address)
print 'flag: ', self.flag
if self.flag == 0:
return
elif self.flag == 1:
self.client[self.store[0]] = self.store[1]
print 'Auth: ' , self.client
self.auth = {}
self.clientAuth = open('regEntry.txt', 'a+')
self.clientAuth1 = self.clientAuth.read()
self.clientAuth.close()
if self.clientAuth1:
self.auth = eval(self.clientAuth1)
else:
self.auth = {}
print str(self.auth)
while True:
print str(self.auth)
if str(self.auth).find(self.store[0]) >= 0:
for i in range(0, 3):
if self.auth[self.store[0]] != self.store[1]:
passError = 'Bad Password: Attempt - %d\n' %(i+1, )
print i
if i == 2:
self.usName = self.store[0]
self.blockClient(self.usName)
self.deReg(self.usName,address)
self.client_socket.send('You are blocked')
print'Blocked'
break
else:
self.client_socket.send(passError)
passw = self.client_socket.recv(4096)
print passw
self.store[1] = passw.rstrip()
elif self.auth[self.store[0]] == self.store[1]:
self.blockName = self.store[0]
self.blockCheck(self.blockName, address)
self.client_socket.send('You are authenticated')
self.usName = self.store[0]
self.clientList(self.data, address)
break
break
else:
usError = 'Invalid Username. Please enter a valid username\n'
self.client_socket.send(usError)
user = self.client_socket.recv(4096)
self.store = user.split('||||',2)
print 'store: ', str(self.store)
def blockClient(self, usName):
self.block = open('clientsBlocked.txt', 'a')
self.toWrite = usName + '\n'
print 'block: ', self.toWrite
self.blockwrite = self.block.write(self.toWrite)
self.block.close()
return
def blockCheck(self, blockName, address):
self.block = open('clientsBlocked.txt', 'ab+')
self.blockwrite = self.block.read()
self.block.close()
if self.blockwrite.find(self.blockName) >= 0:
print 'Block found'
self.client_socket.send('You have been Blocked already.')
return 0
else:
return 1
def storeInfo(self, data, address):
self.client_socket.send('Give your new Username and Password')
self.data = self.client_socket.recv(4096*100)
print self.data
regDict = {}
if self.data.find('||||')>=0:
self.store = self.data.split('||||',2)
self.blockName = self.store[0]
self.flag = self.blockCheck(self.blockName, address)
if self.flag == 0:
return
elif self.flag == 1:
self.reg = open('regEntry.txt', 'ab+')
self.reg1 = self.reg.read()
if self.reg1:
regDict = eval(self.reg1)
else:
regDict = {}
self.reg.close()
regDict[self.store[0]] = self.store[1]
self.regEnter = open('regEntry.txt', 'w')
self.regEnter.write(str(regDict))
self.regEnter.close()
self.client_socket.send('You are Registered')
self.usName = self.store[0]
self.clientList(self.usName, address)
return
def clientList(self, usName, address):
'''if self.data.find('||||')>=0:
self.store = self.data.split('||||',2)
self.usName = self.store[0]'''
print 'aaaaaaaaaaaaaaaaaaaa'
client_List = {}
self.cList = open('clientList.txt', 'ab+')
self.c_List = self.cList.read()
if self.c_List:
client_List = eval(self.c_List)
else:
client_List = {}
self.cList.close()
client_List[self.usName] = address
self.cList = open('clientList.txt', 'w')
self.c_List = self.cList.write(str(client_List))
self.cList.close()
self.data = self.client_socket.recv(4096*100)
if self.data.find('Give me the client list') >= 0:
self.List = open('clientList.txt', 'rb')
self.List1 = self.List.read()
self.List.close()
print str(self.List1)
self.client_socket.send(str(self.List1))
def deReg(self, usName, address):
deReg_list = {}
deReg_client = {}
self.deRegList = open ('clientList.txt', 'ab+')
self.deRegClist = self.deRegList.read()
if self.deRegList:
deReg_list = eval(self.deRegClist)
else:
deReg_list = {}
self.deRegList.close()
del deReg_list[self.usName]
print 'Keys :' , deReg_list.keys()
self.cList = open('clientList.txt', 'w')
self.cList.write(str(deReg_list))
self.cList.close()
self.reg = open('regEntry.txt', 'ab+')
self.reg1 = self.reg.read()
if self.reg1:
deReg_client = eval(self.reg1)
else:
deReg_client = {}
self.reg.close()
del deReg_client[self.usName]
self.regEnter = open('regEntry.txt', 'w')
self.regEnter.write(str(deReg_client))
self.regEnter.close()
'''self.check = self.data.split(' ',2)
del regEntry[self.check[1]]
if str(regEntry).find('{}') >= 0:
self.newEntry = str(regEntry).strip('{}')
self.new = open('regEntry.txt', 'w')
self.new.write(str(self.newEntry))
self.new.close()
else:
self.new = open('regEntry.txt', 'w')
self.new.write(str(regEntry))
self.new.close()
return 0'''
def Logging(self, clientPort, clientIp):
self.timeStampLog = time.strftime('%c')
self.log = str(self.clientIp) + ' ' + str(self.clientPort)
self.log += ' ' + str(self.timeStampLog) + '\n'
self.logFile = open('PA4_logFile.txt', 'a').write(self.log)
if __name__== '__main__':
temp = mainServer()
temp.clientConnection()