-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTCPServer.py
More file actions
151 lines (110 loc) · 5.19 KB
/
TCPServer.py
File metadata and controls
151 lines (110 loc) · 5.19 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
from socket import socket, AF_INET, SOCK_STREAM, SHUT_RDWR, timeout, SOCK_DGRAM ,error,gethostname,gethostbyname
import sys
from BMI import BMI
from db import ServerDB
class TCPServer():
def __init__(self, serverPort):
self.serverSocket = None
self.serverState = None
self.connectionSocket = None
self.clientAddr = None
self.bmiCal = BMI()
self.dataBase = ServerDB()
self.initServer(serverPort)
self.initConnection()
self.chatLoop()
self.serverSocket.shutdown(SHUT_RDWR)
self.serverSocket.close()
sys.exit()
def initServer(self, serverPort):
localIP = get_local_IP()
# Create the server Welcoming socket
self.serverSocket = socket(AF_INET, SOCK_STREAM)
# Bind the Welcoming socket to {it's interface} and {serverPort}
try:
self.serverSocket.bind((localIP, serverPort))
except error:
print("Failed to bind")
sys.exit()
print(f'---- Created serverSocket ----')
# server Welcoming socket is waiting for clients to connect to it ...
self.serverSocket.listen(1)
print(f'serverSocket Listening...')
self.serverState = True
def initConnection(self):
# while self.serverState:
self.connectionSocket, self.clientAddr = self.serverSocket.accept()
# if idle: times out after 30 secs
self.connectionSocket.settimeout(30)
print(f'-- Created new connectionSocket --')
print(f'conncted with: {self.clientAddr}')
def chatFClient(self):
recievedMsg = self.connectionSocket.recv(1024).decode()
print(f'{self.clientAddr} says: {recievedMsg}\n')
return recievedMsg
def chatTClient(self, messageTosend: str):
self.connectionSocket.sendall(messageTosend.encode())
# print(f'Sent: {messageTosend}')
def checkCredentials(self,username,password):
result = self.dataBase.select_user_by_name((username))
if result == 'notFound': # create new entry
self.dataBase.create_user((username,password,-99.99))
return 'new',-99.99
else:
storedPassword = result[1]
oldBmi = result[-1]
if storedPassword == password:
return 'success',oldBmi
else:
return 'fail',-99.99
def chatLoop(self):
inputMsg = ' '
rcvdMsg = ' '
self.chatTClient('Welcome to the great BMI calculator,\nPlease Enter username,password to login/signup\nTo Quit Enter exit')
while rcvdMsg.upper().strip() != 'BYE':
try:
# TODO: check for 'BYE'
# ::DONE::
response = self.chatFClient()
if response.upper().strip() == 'EXIT':
break
else:
username,password = response.strip().split(',')
print(f'username: {username} ,password: {password}')
#TODO: check in DB if user exists by name , if not create a new user entry , if it does -> check if password match,,, and send a notification to verify the login , and old BMI
# :: DONE ::
#FIXME: could add an option to check both if the password exits and is correct but username is not, and instead of creating a new entry , ask the user first if he wwant to signup
statFlag,oldBmi = self.checkCredentials(username,password)
if statFlag == 'new':
self.chatTClient(f'{statFlag}:new accounted is created\n Please Enter you Weight,Height')
elif statFlag == 'success':
self.chatTClient(f'{statFlag}:logged-in successfully, your old BMI is {oldBmi}\n Please Enter you Weight,Height')
else:
self.chatTClient(f'{statFlag}:Try again') # fail
continue # loop again
try:
weight,height = self.chatFClient().strip().split(',')
bmi,catagory = self.bmiCal.notify(int(weight),int(height))
self.chatTClient(f'{bmi}, {catagory}')
self.dataBase.update_user((bmi,username))
except ValueError:
break
break
except timeout:
print('TIMEOUT')
self.chatTClient('BYE')
break
self.connectionSocket.shutdown(SHUT_RDWR)
self.connectionSocket.close()
print(f'-- End connection from Server side--')
# ----------------------------- helper functions ----------------------------- #
def get_local_IP() -> str:
hostName = gethostname()
local_ip = gethostbyname(hostName)
return local_ip
# ---------------------------------------------------------------------------- #
if __name__ == "__main__":
# ------------------------- single connection server ------------------------- #
server = TCPServer(22222)
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #