-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
47 lines (40 loc) · 1.11 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
import socket
import threading
PORT = 5050
SERVER = "127.0.1.1"
ADDR = (SERVER, PORT)
HEADER = 64
FORMAT = 'utf-8'
DISCONNECT_MSG = "!exit"
#send msg to server
def send(msg):
message = msg.encode(FORMAT)
msgLength = len(message)
sendLength = str(msgLength).encode(FORMAT)
sendLength += b' ' * (HEADER-len(sendLength))
client.send(sendLength)
client.send(message)
#rx msgs fm server
def recieve():
while True:
msgLength = client.recv(HEADER).decode(FORMAT)
if msgLength:
msgLength = int(msgLength)
msg = client.recv(msgLength).decode(FORMAT)
if msg == DISCONNECT_MSG:
connected = False
print(f"\r[{SERVER}] {msg}\n[SEND] ", end="")
#thread setup for rx msgs
def connect():
thread = threading.Thread(target=recieve)
thread.start()
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
print(f"[Connected] Client connected to server at {ADDR}")
print("Use '!exit' to disconnect")
connect()
msg = ""
while (msg != DISCONNECT_MSG):
print("[SEND] ", end="")
msg = input()
send(msg)