-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
48 lines (36 loc) · 1.14 KB
/
client.py
File metadata and controls
48 lines (36 loc) · 1.14 KB
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
import asyncio
import socket
import threading
import time
HEADER = 64
PORT = 5050
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
# /////////////////////////////////////////////////////////////////////////////
# {functions}
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
# print(client.recv(2048).decode(FORMAT))
def receive_message():
while True:
print(client.recv(2048).decode(FORMAT))
# /////////////////////////////////////////////////////////////////////////////
# wait for another client to connect
while client.recv(2048).decode(FORMAT) == 'wait':
print('Dear client, Please wait')
time.sleep(10)
# make a thread for receive messages continuously
thread = threading.Thread(target=receive_message)
thread.start()
# for sending message
while True:
send(input('enter message: '))