-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.py
134 lines (126 loc) · 5.12 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
#coding:utf-8
'''
file:server.py
date:2017/9/11 14:43
author:lockey
email:[email protected]
platform:win7.x86_64 pycharm python3
desc:p2p communication serverside
'''
import socketserver,json,time
import subprocess
connLst = []
groupLst = []
## 代号 地址和端口 连接对象
#optype = {'ag':'group adding','cp':'chat with individual','cg':'chat with group'}
class Connector(object): ##存放连接
def __init__(self,account,password,addrPort,conObj):
self.account = account
self.password = password
self.addrPort = addrPort
self.conObj = conObj
class Group(object):
def __init__(self,groupname,groupOwner):
self.groupId = 'group'+str(len(groupLst)+1)
self.groupName = 'group'+groupname
self.groupOwner = groupOwner
self.createTime = time.time()
self.members=[groupOwner]
class MyServer(socketserver.BaseRequestHandler):
def handle(self):
print("got connection from",self.client_address)
userIn = False
global connLst
global groupLst
while not userIn:
conn = self.request
data = conn.recv(1024)
if not data:
continue
dataobj = json.loads(data.decode('utf-8'))
#如果连接客户端发送过来的信息格式是一个列表且注册标识为False时进行用户注册
ret = '0'
if type(dataobj) == list and not userIn:
account = dataobj[0]
password = dataobj[1]
optype = dataobj[2]
existuser = False
if len(connLst) > 0:
for obj in connLst:
if obj.account == account:
existuser = True
if obj.password == password:
userIn = True
print('{} has logged in system({})'.format(account,self.client_address))
break
if optype == 'login' and (not userIn or not existuser):
ret = '1'
print('{} failed to logged in system({})'.format(account, self.client_address))
else:
if existuser:
ret = '1'
print('{} failed to register({}),account existed!'.format(account, self.client_address))
else:
try:
conObj = Connector(account,password,self.client_address,self.request)
connLst.append(conObj)
print('{} has registered to system({})'.format(account,self.client_address))
userIn = True
except:
print('%s failed to register for exception!'%account)
ret = '99'
conn.sendall(ret.encode('utf-8'))
if ret == '0':
break
while True:
conn = self.request
data = conn.recv(1024)
if not data:
continue
print(data)
dataobj = data.decode('utf-8')
dataobj = json.loads(dataobj)
if dataobj['type'] == 'ag' and userIn:
groupName = dataobj['groupName']
groupObj = Group(groupName,self.request)
groupLst.append(groupObj)
conn.sendall('ag0'.encode('utf-8'))
print('%s added'%groupName)
continue
if dataobj['type'] == 'eg' and userIn:
groupName = dataobj['groupName']
ret = 'eg1'
for group in groupLst:
if groupName == group.groupName:
group.members.append(self.request)
print('{} added into {}'.format(self.client_address,groupName))
ret = 'eg0'
break
conn.sendall(ret.encode('utf-8'))
continue
#客户端将数据发给服务器端然后由服务器转发给目标客户端
print('connLst',connLst)
print('grouplst',groupLst)
if len(connLst) > 1:
sendok = False
if dataobj['type'] == 'cg':
print('group',data)
for obj in groupLst:
if obj.groupName == dataobj['to']:
for user in obj.members:
if user != self.request:
user.sendall(data)
else:
for obj in connLst:
if dataobj['to'] == obj.account:
obj.conObj.sendall(data)
sendok = True
if sendok == False:
print('no target valid!')
else:
conn.sendall('-1'.encode('utf-8'))
continue
if __name__ == '__main__':
server = socketserver.ThreadingTCPServer(('192.168.1.7',8022),MyServer)
print('waiting for connection...')
server.serve_forever()