-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTCPClient.py
109 lines (88 loc) · 3.83 KB
/
TCPClient.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
100
101
102
103
104
105
106
107
108
109
from socket import socket, AF_INET, SOCK_STREAM, SHUT_RDWR, SOCK_DGRAM
from TCPServer import get_local_IP
import sys
import hashlib
class TCPClient():
def __init__(self, serverIp, serverPort):
# create a tcp socket
self.clientSocket = None
print(f'--- Created socket ---')
self.dataToSend = " "
self.dataReceived = " "
self.initConnection(serverIp, serverPort)
# self.chatTServer(messageToSend)
# self.chatFServer()
# self.chatLoop()
def initConnection(self, serverIp, serverPort):
self.clientSocket = socket(AF_INET, SOCK_STREAM)
# initiate three-way tcp handshake with the server
self.clientSocket.connect((serverIp, serverPort))
print(f'Connected to server: {(serverIp,serverPort)}')
def chatTServer(self, messageToSend: str):
self.clientSocket.sendall(messageToSend.encode())
# print(f'Sent: {messageToSend}\n')
def chatFServer(self):
message = self.clientSocket.recv(1024).decode()
print(f'server says: {message}\n')
return message
def chatLoop(self):
inputMsg = ' '
rcvdMsg = ' '
stillOn = True
# rcvdMsg = self.chatFServer() # greetings from server and request credentials
while stillOn:
# if rcvdMsg.upper().strip() == 'BYE':
# break
inputMsg = str(input('username,password: '))
# TODO : hash the password then send it
if (inputMsg.upper().strip() == 'EXIT'):
# send bye msg to notify the server to close it's side of the connection
self.chatTServer(inputMsg)
stillOn = False
break
username, password = inputMsg.strip().split(',')
self.username = username
hashedPassword = hashlib.sha256()
hashedPassword.update(password.encode())
# send credentials
self.chatTServer(f'{username},{hashedPassword.hexdigest()}')
statFlag, oldBmi = self.chatFServer().strip().split(
':') # check credentials from server
if (statFlag.lower().strip() == 'success') or (statFlag.lower().strip() == 'new'):
# self.chatFServer() # requesting weight and height
self.chatTServer(str(input("Weight,Height: "))) # send them
self.dataReceived = self.chatFServer() # get back the BMI
stillOn = False
else:
#TODO: repeat
# ::DONE::
print('TRY AGAIN...\n')
self.clientSocket.shutdown(SHUT_RDWR)
self.clientSocket.close()
print(f'-- End connection from client Side --')
def check_authority(self, name_pass_pair):
username, password = name_pass_pair.strip().split(',')
self.username = username
hashedPassword = hashlib.sha256()
hashedPassword.update(password.encode())
# send credentials
self.chatTServer(
f'user_password,{username},{hashedPassword.hexdigest()}')
_, statFlag = self.chatFServer().strip().split(
',') # check credentials from server
return statFlag.lower().strip()
def get_bmi(self, weight_height):
self.chatTServer("body_info,"+weight_height +
","+self.username) # send them
return self.chatFServer() # get back the BMI
def close_connection(self):
self.clientSocket.shutdown(SHUT_RDWR)
self.clientSocket.close()
print(f'-- End connection from client Side --')
# sys.exit()
if __name__ == "__main__":
# assuming server is on same machine of client ,this function gets the default local IPv4 Address for any machine.
serverIp = get_local_IP()
serverPort = 22222
client = TCPClient(serverIp, serverPort)
client.chatLoop()