-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasgn10Server.py
64 lines (48 loc) · 1.83 KB
/
asgn10Server.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
import socket
HOST = '127.0.0.1'
PORT = 65432
INVALID_WORDS = ['DESTROY', 'REMOVE', 'DELETE']
def contains_invalid_words(message):
for word in INVALID_WORDS:
if word.lower() in message or word.upper() in message:
return word.lower()
return None
def authenticated(username):
if username.lower() == 'master':
return True
else:
return False
def start_server(host=HOST,port=PORT):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen()
conn, address = s.accept()
print(f'Connection from: ({address},{port})')
while True:
data = conn.recv(1024)
username = data.decode()
print(f'from connected user: {username}')
if authenticated(username):
while True:
conn.send('\nEnter a message -->'.encode())
message = conn.recv(1024)
print(f'from connected user: {message.decode()}')
invalid_word = contains_invalid_words(message.decode())
if invalid_word:
invalid_response = f'{invalid_word} IS AN INVALID WORD!'
conn.send(f'{invalid_response}'.encode())
print(f'sending: {invalid_response}')
continue
else:
valid_response =f'VALID: {message.decode()}'
print(f'sending: {valid_response}')
conn.send(valid_response.encode())
continue
else:
conn.send('INVALID USER NAME!\nENDING PROGRAM'.encode())
break
conn.close()
def main():
start_server()
if __name__ =='__main__':
main()