-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
360 lines (315 loc) · 14.4 KB
/
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#-*-coding:utf-8-*-
'''
login_data:
(1)[group_chat, group_title, id]
(2)[group, group_title, id, text]
(3)[single_person, end_name, recv_name, text]
(4)[login, id, password]
'''
from socket import *
import threading
import pymysql
import os
import sys
import struct
import inspect
import ctypes
def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
db = pymysql.connect("localhost", "root", "root", "liuchang")
cursor = db.cursor()
sql = 'select * from simple'
try:
cursor.execute(sql)
result = cursor.fetchall()
except:
print('error: cant fetch data ')
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 = []
for i in result:
user_list.append([i[1], i[2]])
print(user_list)
user_l = len(user_list)
user_client = []
thread_list = []
group_list = [['信息安全专业群'], ['信息安全学习群'], ['兼职群']]
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 = 'error_password'
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 = 'error_login'
clientsock.send(login_bkinfo.encode())
break
elif user_cl == ul - 1:
usercl=[]
usercl.append(logindata[1])
usercl.append(clientsock)
login_bkinfo = 'OK'
user_client.append(usercl)
#print(user_client)
clientsock.send(login_bkinfo.encode())
break
elif x==user_l-1:
login_bkinfo = 'error_id'
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 = 'error_password'
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 = 'OK'
user_client.append(usercl)
#print(user_client)
clientsock.send(login_bkinfo.encode())
break
elif x==user_l-1:
login_bkinfo = 'error_id'
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]) == 'group_chat':
#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]) == 'group':
#接受文件
'''
先接受图片到本地缓存server_down_loads,再识别接受用户,
判断其是否在线,若在线进行转发,否则不转发,提示不在线
使用一个数据包file来预定一个文件的发送时间,在接收到file的
提示数据包的时候,就准备接受一个file,与用户转发也是同理
'''
if logindata[3] == "file":
fileinfo_size = struct.calcsize('128sl')
buf = clientsock.recv(fileinfo_size)
if buf:
filename, filesize = struct.unpack('128sl', buf)
fn = filename.decode().strip('\x00')
new_filename = os.path.join('./server_downloads', fn)
recvd_size = 0
fp = open(new_filename, 'wb')
while not recvd_size == filesize:
if filesize - recvd_size > 1024:
data = clientsock.recv(1024)
recvd_size += len(data)
else:
data = clientsock.recv(1024)
recvd_size = filesize
fp.write(data)
fp.close()
#转发文件给群内成员
for wl in range(0, group_l):
if str(group_list[wl][0]) == str(logindata[1]):
senddata = ["file"]
senddata.append(str(logindata[2])+":")
senddata = '*'.join(senddata)
l = len(group_list[wl])
try:
if l >=2:
for x in range(1, l):
group_list[wl][x].send(senddata.encode())
file_path = new_filename
fhead = struct.pack(b'128sl', bytes(os.path.basename(file_path), encoding='utf-8'), os.stat(file_path).st_size)
print('client filepath: {0}'.format(file_path))
group_list[wl][x].send(fhead)
fp = open(file_path, 'rb')
while 1:
data = fp.read(1024)
if not data:
print('{0} file send over...'.format(file_path))
break
group_list[wl][x].send(data)
else:
#clientsock.send(senddata.encode())
break
print("群聊信息" + str(senddata)+str(clientaddress))
except ValueError:
break
#正常普通交流信息
else:
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]) == 'single_person':
if logindata[3] == "file":
'''
先接受图片到本地缓存server_down_loads,再识别接受用户,
判断其是否在线,若在线进行转发,否则不转发,提示不在线
使用一个数据包file来预定一个文件的发送时间,在接收到file的
提示数据包的时候,就准备接受一个file,与用户转发也是同理
'''
fileinfo_size = struct.calcsize('128sl')
buf = clientsock.recv(fileinfo_size)
if buf:
filename, filesize = struct.unpack('128sl', buf)
fn = filename.decode().strip('\x00')
new_filename = os.path.join('./server_downloads', fn)
recvd_size = 0
fp = open(new_filename, 'wb')
while not recvd_size == filesize:
if filesize - recvd_size > 1024:
data = clientsock.recv(1024)
recvd_size += len(data)
else:
data = clientsock.recv(1024)
recvd_size = filesize
fp.write(data)
fp.close()
#转发给用户
user_cl = len(user_client)
#print(user_client)
senddata = ["file"]
senddata.append(str(logindata[1])+":")
send_info = '*'.join(senddata)
z = 1
for pl in range(0,user_cl):
if user_client[pl][0] == logindata[2]:
#print(logindata[2])
user_client[pl][1].send(send_info.encode())
file_path = new_filename
fhead = struct.pack(b'128sl', bytes(os.path.basename(file_path), encoding='utf-8'), os.stat(file_path).st_size)
print('client filepath: {0}'.format(file_path))
user_client[pl][1].send(fhead)
fp = open(file_path, 'rb')
while 1:
data = fp.read(1024)
if not data:
print('{0} file send over...'.format(file_path))
break
user_client[pl][1].send(data)
#clientsock.send(send_info.encode())
break
elif z == user_cl:
back = str(logindata[2])+'不在线'
clientsock.send(back.encode())
z+=1
'''
for wl in range(0, group_l):
if str(group_list[wl][0]) == str(logindata[1]):
senddata = ["file"]
senddata.append(str(logindata[2])+":")
senddata = '*'.join(senddata)
l = len(group_list[wl])
try:
if l >=2:
for x in range(1, l):
group_list[wl][x].send(senddata.encode())
file_path = new_filename
fhead = struct.pack(b'128sl', bytes(os.path.basename(file_path), encoding='utf-8'), os.stat(file_path).st_size)
print('client filepath: {0}'.format(file_path))
group_list[wl][x].send(fhead)
fp = open(file_path, 'rb')
while 1:
data = fp.read(1024)
if not data:
print('{0} file send over...'.format(file_path))
break
group_list[wl][x].send(data)
else:
#clientsock.send(senddata.encode())
break
print("群聊信息" + str(senddata)+str(clientaddress))
except ValueError:
break
'''
else:
#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]) == 'quit':
user_cl = len(user_client)
for pl in range(0,user_cl):
if user_client[pl][0]==logindata[1]:
del user_client[pl]
#clientsock.send(send_info.encode())
break
break
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()