client.py (2759B)
1import socket
2import threading
3import time
4import utility
5IP = utility.get_ip_address()
67
c_obj = threading.Condition()
8PORT = 5566
9ADDR = (IP, PORT)
10SIZE = 1024
11FORMATmsg = "utf-8"
12USERNAME = None
13PIN = None
14client = None
15S_PORT = None
1617
18
def send_message():
19# c_obj.acquire()
20while True:
21msg = input("")
22globals()['client'].send(msg.encode(FORMATmsg))
2324
25
def recieve_messsage():
26# c_obj.acquire()
27while True:
2829
msg = globals()['client'].recv(SIZE).decode(FORMATmsg)
3031
print(f"[SERVER] {msg}")
32pass
3334
35
def startClient():
36print(f"[CONNECTING] Client connecting to server at {IP}:{PORT}")
37globals()['client'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38globals()['client'].connect(ADDR)
39print(f"[CONNECTED] Client connected to server at {IP}:{PORT}")
4041
42
def main():
43utility.printUI()
4445
while True:
46startClient()
4748
# [TAB] for inputting, sending and verifyin username
49if globals()['USERNAME'] == None or globals()['USERNAME'] == "!NOTACCEPTED":
50while True:
51globals()['USERNAME'] = input("[LOGIN] INPUT USERNAME: ")
5253
globals()['client'].send(
54globals()['USERNAME'].encode(FORMATmsg))
55temp_msg = globals()['client'].recv(SIZE).decode(FORMATmsg)
56if (temp_msg == globals()['USERNAME']):
57print(f"[SERVER] Username accepted")
58break
59else:
60print("[NAME ERROR] Try another username")
61continue
6263
if globals()['PIN'] == None:
64globals()['PIN'] = input("[LOGIN] Input PIN: ")
65globals()['client'].send(globals()['PIN'].encode(FORMATmsg))
6667
temp_msg = globals()['client'].recv(SIZE).decode(FORMATmsg)
68print(f"[SERVER] {temp_msg}")
69if temp_msg == "try again":
70print(
71'[SERVER]: PIN not accepted:(\n[SERVER]: Connection failed:(\nTerminating:(\n')
72globals()['client'].close()
73exit()
7475
print(
76f"[AUTHENTICATED] credentials are verified by server at {IP}:{PORT}")
7778
print(f"[Waiting] Waiting for port number from {IP}:{PORT}")
79globals()['client'].send("PORT".encode(FORMATmsg))
80temp_msg = globals()['client'].recv(SIZE).decode(FORMATmsg)
81globals()['S_PORT'] = int(temp_msg)
82print(f"[SERVER] Your port number is {globals()['S_PORT']}")
83# input("end")
84break
8586
thread_recv = threading.Thread(target=recieve_messsage, args=())
87thread_recv.start()
88time.sleep(0.1)
89thread_send = threading.Thread(target=send_message, args=())
90thread_send.start()
9192
pass
9394
95
if __name__ == "__main__":
96main()