forked from 2044-space-elevator/TouchFish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
50 lines (44 loc) · 1.26 KB
/
chat.py
File metadata and controls
50 lines (44 loc) · 1.26 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
49
50
import socket, threading
ip = input("Connect to IP:")
s = socket.socket()
account_numbers = eval(input("The maximum umbers of account:"))
portin = eval(input("The connecting port (must be spare):"))
s.bind((ip, portin))
s.listen(account_numbers)
s.setblocking(0)
conn = []
address = []
def add_accounts():
while 1:
if len(conn) > account_numbers:
break
conntmp = None
addresstmp = None
try:
conntmp, addresstmp = s.accept()
except:
continue
conntmp.setblocking(0)
conn.append(conntmp)
address.append(addresstmp)
print(f"Connected, ip address: {addresstmp}")
def receive_msg():
while 1:
for i in range(len(conn)):
data = None
try:
data = conn[i].recv(1024).decode('UTF-8')
except:
continue
if (not data):
continue
print(f"Message from client {address[i]}, msg: {data}")
for j in range(len(conn)):
try:
conn[j].send(bytes(data, encoding='utf-8'))
except:
pass
t1 = threading.Thread(target=add_accounts)
t2 = threading.Thread(target=receive_msg)
t1.start()
t2.start()