-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
92 lines (69 loc) · 2.39 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import socket
import threading
import os
import random
UDP_MAX_SIZE = 65535
COMMANDS=('/members',
'/connect',
'/exit',
'/help')
HELP_TEXT="""
/members- get active members
/connect <clientXXXX> -connect to member
/exit - disconnect from client"""
def listen(s: socket.socket, host:str , port: int):
while True:
msg,addr = s.recvfrom(UDP_MAX_SIZE)
msg_port= addr[-1]
msg= msg.decode('ascii')
allowed_ports=threading.current_thread().allowed_ports
if msg_port not in allowed_ports:
continue
if not msg:
continue
if '__' in msg:
command, content = msg.split('__')
if command =='members':
for n, member in enumerate(content.split(';'), start=1):
print('\r\r' + f'{n}) {member}'+ '\n' + 'you: ', end='')
else:
peer_name=f'client{msg_port}'
print('\r\r' + f'{peer_name}: ' + msg + '\n' + f'you: ', end='')
def start_listen(target,socket,host,port):
th=threading.Thread(target=target,args=(socket,host,port),daemon=True)
th.start()
return th
def connect(host: str = '127.0.0.1', port: int = 8891):
own_port=random.randint(8001,9000)
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, own_port))
listen_thread=start_listen(listen,s,host,port)
allowed_ports =[port]
listen_thread.allowed_ports=allowed_ports
sendto=(host,port)
s.sendto('__join'.encode('ascii'),sendto)
while True:
msg = input(f'you: ')
command=msg.split(' ')[0]
if command in COMMANDS:
if msg=='/members':
s.sendto('__members'.encode('ascii'),sendto)
if msg=='/exit':
peer_port=sendto[-1]
allowed_ports.remove(peer_port)
sendto=(host,port)
print(f'Disconnect from client{peer_port}')
if msg.startswith('/connect'):
peer=msg.split(' ')[-1]
peer_port=int(peer.replace('client', ''))
allowed_ports.append(peer_port)
sendto=(host,peer_port)
print(f'Connect to client{peer_port}')
if msg == '/help':
print(HELP_TEXT)
else:
s.sendto(msg.encode('ascii'),sendto)
if __name__ == '__main__':
os.system('clear')
print('Welcome to chat!')
connect()