-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
71 lines (58 loc) · 2.08 KB
/
client.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
import socket
from threading import Thread
import time
import os
HOST = "127.0.0.1"
PORT = 8080
USERNAME = input("enter your name: ")
class Client:
def __init__(self) -> None:
self.address = self.maintain_connection()
(Thread(target=self.send_message)).start()
(Thread(target=self.receive_messages)).start()
def receive_messages(self) -> str:
while True:
try:
incoming_message = self.address.recv(1024)
incoming_message = incoming_message.decode()
print(incoming_message)
except:
print("connection is lost", end='\r')
time.sleep(0.3)
self.address = self.maintain_connection()
def send_message(self) -> None:
while True:
message = input(str('>> '))
if message == "chatquit":
self.close_connection()
message = (USERNAME + " : " + message).encode()
try:
self.address.send(message)
except: Exception
def close_connection(self) -> None:
print("you pressed quit and choose to close program, Have a nice day! ")
message = USERNAME + " quit"
message = message.encode()
self.address.send(message)
self.address.shutdown(socket.SHUT_RDWR)
os._exit(0)
def maintain_connection(self) -> object:
trying_to_connect = 1
while (trying_to_connect == 1):
try:
address = socket.socket()
address.connect((HOST, PORT))
print('Connected to chat server')
trying_to_connect == 0
return address
except:
print("Trying to connect ", end = '\r')
time.sleep(0.3)
print("Trying to connect. ", end = '\r')
time.sleep(0.3)
print("Trying to connect.. ", end = '\r')
time.sleep(0.3)
print("Trying to connect... ", end = '\r')
time.sleep(1)
if __name__=="__main__":
_ = Client()