-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
58 lines (46 loc) · 1.32 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
import socket, os, sys
from threading import Thread
try:
HOST, PORT, NAME = sys.argv[1], int(sys.argv[2]), sys.argv[3]
except:
print("Invalid arguments given:")
HOST = input("Enter host: ")
PORT = int(input('Enter port: '))
NAME = input("Enter name: ")
print("#"+"-"*30+"#") #separator
BUFSIZE = 1024
ADDR = (HOST, PORT)
RESET = "\033[0;0m"
BOLD = "\033[;1m"
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(ADDR)
client_socket.send(bytes(NAME, "utf-8"))
left = False
def rc(): #rc > recieve
while True:
try:
msg = client_socket.recv(BUFSIZE).decode("utf-8")
if not left:
sys.stdout.write("\r\033[K")
sys.stdout.write(msg), sys.stdout.flush()
sys.stdout.write("\r\033[K")
sys.stdout.write(BOLD+"{}> ".format(NAME)+RESET); sys.stdout.flush()
except:
break
sys.stdout.write(BOLD+"Welcome {}. You can start typing messages!".format(NAME)+RESET+"\n")
while True:
x = Thread(target=rc)
x.start()
sys.stdout.write("\r\033[K")
sys.stdout.write(BOLD+"{}> ".format(NAME)+RESET); sys.stdout.flush()
msg = sys.stdin.readline()
if msg == "/quit\n":
left = True
client_socket.send(bytes("LEFT", "utf-8"))
client_socket.close()
sys.exit()
break
elif msg == "/list\n":
client_socket.send(bytes("LIST", "utf-8"))
continue
client_socket.send(bytes(msg, "utf-8"))