-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathTcp_server.py
159 lines (130 loc) · 5.24 KB
/
Tcp_server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#-*-coding:utf-8-*-
from socket import *
import threading
address='127.0.0.1'
port=6337
buffsize=1024
s = socket(AF_INET, SOCK_STREAM)
s.bind((address,port))
s.listen(10) #最大连接数
client_list=[]
user_list=[[2097557613,123456],[2097557614,123456],[2097557615,123456],[2097557616,123456],[2097557617,123456],[2097557618,123456]]
user_l=len(user_list)
user_client=[]
group_list=[['tcp群'],['兼职群'],['同学群'],['学习资料群']]
def login(logindata,clientsock):
for x in range(0,user_l):
print("登录请求"+str(logindata[1]))
if len(user_client)>=1:
ul=len(user_client)
if str(user_list[x][0])==str(logindata[1]) and str(user_list[x][1])!=str(logindata[2]):
login_bkinfo = 'flase-pw'
clientsock.send(login_bkinfo.encode())
break
elif str(user_list[x][0])==str(logindata[1]) and str(user_list[x][1])==str(logindata[2]):
for user_cl in range(0, ul):
if str(user_client[user_cl][0]) == str(logindata[1]):
login_bkinfo = 'flase-login'
clientsock.send(login_bkinfo.encode())
break
elif user_cl == ul - 1:
usercl=[]
usercl.append(logindata[1])
usercl.append(clientsock)
login_bkinfo = 'true'
user_client.append(usercl)
print(user_client)
clientsock.send(login_bkinfo.encode())
break
elif x==user_l-1:
login_bkinfo = 'flase-user'
clientsock.send(login_bkinfo.encode())
else:
if str(user_list[x][0])==str(logindata[1]) and str(user_list[x][1])!=str(logindata[2]):
login_bkinfo = 'flase-pw'
clientsock.send(login_bkinfo.encode())
break
elif str(user_list[x][0])==str(logindata[1]) and str(user_list[x][1])==str(logindata[2]):
usercl=[]
usercl.append(logindata[1])
usercl.append(clientsock)
login_bkinfo = 'true'
user_client.append(usercl)
print(user_client)
clientsock.send(login_bkinfo.encode())
break
elif x==user_l-1:
login_bkinfo = 'flase-user'
clientsock.send(login_bkinfo.encode())
def tcplink(clientsock,clientaddress):
group_l = len(group_list)
while True:
recvdata=clientsock.recv(buffsize).decode('utf-8')
logindata=recvdata.split(' ')
print(logindata)
if str(logindata[0])=='login':
login(logindata,clientsock)
elif str(logindata[0])=='wechat_req':
#reqci=1
for y in range(0,group_l):
if str(group_list[y][0])==str(logindata[1]):
requser=str(logindata[2])+' '+'加入'
group_list[y].append(clientsock)
groupl=len(group_list[y])
if groupl>2:
for h in range(1,groupl):
group_list[y][h].send(requser.encode())
else:
clientsock.send(requser.encode())
break
elif str(logindata[0])=='wechat':
for wl in range(0,group_l):
if str(group_list[wl][0])==str(logindata[1]):
senddata=str(logindata[2])+":"+str(logindata[3])
l = len(group_list[wl])
try:
if l >=2:
for x in range(1, l):
group_list[wl][x].send(senddata.encode())
else:
clientsock.send(senddata.encode())
break
print("群聊信息" + str(senddata)+str(clientaddress))
except ValueError:
break
elif str(logindata[0])=='personal':
#print(logindata)
user_cl = len(user_client)
#print(user_client)
send_info = str(logindata[1])+":"+str(logindata[3])
z=1
for pl in range(0,user_cl):
if user_client[pl][0]==logindata[2]:
user_client[pl][1].send(send_info.encode())
#clientsock.send(send_info.encode())
break
elif z==user_cl:
back=str(logindata[2])+'不在线'
clientsock.send(back.encode())
z+=1
elif str(logindata[0])=='':
print('无法识别:')
print(logindata[0])
break
clientsock.close()
del client_list[-1]
while True:
clientsock,clientaddress=s.accept()
client_list.append(clientsock)
print('connect from:',clientaddress)
'''joinfo=str(clientaddress)
try:
ld=len(client_list)
for x in range(0, ld):
client_list[x].send(joinfo.encode())
except ValueError:
continue
'''
t=threading.Thread(target=tcplink,args=(clientsock,clientaddress)) #新创建的线程
t.start()
s.close()