-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
226 lines (201 loc) · 9.45 KB
/
Server.py
File metadata and controls
226 lines (201 loc) · 9.45 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
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
import copy
import socket
import urllib.request
from threading import Thread
from Settings import getTime
import Settings
import Mysql
import demjson
clientPool = dict() # {userID: clientSocket}
groupPool = dict()
chatPool = dict()
class Server:
def __init__(self):
self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 负责监听的socket
self.serverSocket.bind(Settings.settings.addr)
self.serverSocket.listen(5)
def serve_forever(self):
print(getTime() + 'listening...')
while True:
clientSocket, addr = self.serverSocket.accept()
threads = Thread(target=Handler(clientSocket, addr).handle)
threads.setDaemon(True)
threads.start()
class Handler:
def __init__(self, clientSocket, addr):
self.clientSocket = clientSocket
self.clientAddr = addr
self.mysql = Mysql.Sql()
self.connectionEstablished = False
def _sendData(self, data, marker, soc=None):
size = len(bytes(str(data), encoding='utf-8'))
if size <= 9:
length = '00' + str(size)
elif 9 < size <= 99:
length = '0' + str(size)
else:
length = str(size)
if soc:
soc.sendall(bytes(marker + length + str(data), encoding='utf-8'))
else:
self.clientSocket.sendall(bytes(marker + length + str(data), encoding='utf-8'))
def _getData(self):
marker = self.clientSocket.recv(1).decode()
if marker == '':
return 'Null'
else:
if not '$#'.__contains__(marker):
raise Settings.DataFetchException from None
else:
length = int(self.clientSocket.recv(3).decode())
return self.clientSocket.recv(length).decode()
def _connectionTest(self):
if not self.connectionEstablished:
raise Settings.ConnectionNotEstablishedException from None
def _repostMsg(self):
messagePool = []
while True:
try:
msg = demjson.decode(self._getData(), encoding='utf8')
index = ' '.join(sorted([msg['toID'], msg['fromID']]))
if msg['toID'] != '1000':
if msg['type'] == 'exit':
m = copy.deepcopy(msg)
m['toID'], m['fromID'] = m['fromID'], m['toID']
self._sendData(m, '#')
chatPool.get(index)[msg['fromID']] = None
break
elif chatPool.get(index)[msg['toID']]: # open
messagePool.append(msg)
for i in messagePool:
friendSoc = chatPool.get(index)[msg['toID']]
self._sendData(i, '#', friendSoc)
messagePool = []
else:
if msg['type'] != 'exit':
messagePool.append(msg)
elif msg['toID'] == '1000':
if msg['type'] == 'exit':
self._sendData({'type': 'exit'}, '#')
break
req = {"perception": {"inputText": {"text": msg['msg']},
"selfInfo": {"location": {"city": "武汉", "province": "湖北省", "street": "东湖"}}},
"userInfo": {"apiKey": "dbecb8157b834e21b26a87cb67792788", "userId": "OnlyUseAlphabet"}}
req = demjson.encode(req, encoding='utf8')
http_post = urllib.request.Request(Settings.settings.apiUrl, data=req, headers={
'content-type': 'application/json'})
response = urllib.request.urlopen(http_post)
response_str = response.read().decode('utf8')
response_dic = demjson.decode(response_str, encoding='utf8')
results_text = response_dic['results'][0]['values']['text']
maps = {"fromID": '1000', "toID": msg['fromID'], "time": getTime()[:-4], "msg": results_text,
'fromName': '陈建文的小迷妹', 'type': msg['type']}
self._sendData(maps, '#')
except Exception as e:
pass
def _groupTalk(self):
while bool(groupPool):
try:
msg = demjson.decode(self._getData(), encoding='utf8')
fromID = msg['fromID']
if msg['type'] != 'exit':
for k, v in groupPool.items():
if k != fromID:
self._sendData(msg, '#', v)
else:
data = {'type': 'exit'}
self._sendData(data, '#')
groupPool.pop(msg['fromID'])
break
except:
raise
def handle(self):
while True:
command = self._getData()
if command == 'Open':
print(getTime() + 'Get command:', command)
self.connectionEstablished = True
self._sendData(getTime() + 'Connection established ' + str(self.clientAddr), '#')
print(getTime() + 'Connection established ' + str(self.clientAddr))
elif command == 'Login':
self._connectionTest()
print(getTime() + 'Get command:', command)
ID, pwd = self._getData().split(' ')
if self.mysql.loginCheck(ID, pwd):
if self.mysql.getOnlineUsers().__contains__(ID):
print(getTime() + str(self.clientAddr), 'Login failed.', ID, 'is already online!')
self._sendData('False', '#')
else:
print(getTime() + str(self.clientAddr), 'login successful!')
clientPool.update({ID: self.clientSocket})
self.mysql.updateOnlineStatus(ID, 'Y')
self._sendData('True', '#')
maps = self.mysql.getSelfInfo(ID)
maps.update(self.mysql.getFriendsList(ID))
self._sendData(maps, '#')
else:
print(getTime() + str(self.clientAddr), 'login failed!')
self._sendData('False', '#')
elif command == 'Logout':
self._connectionTest()
print(getTime() + 'Get command:', command)
ID = self._getData()
self.mysql.updateOnlineStatus(ID, 'N')
clientPool.pop(ID)
elif command == 'Register':
self._connectionTest()
print(getTime() + 'Get command:', command)
userID, nickname, sex, email, pwd, headAddr = self._getData().split(' ')
try:
self.mysql.insertUsers(userID, pwd, nickname, sex, email, headAddr, 'N')
self.mysql.addFriend(userID, '1000')
self.mysql.addFriend(userID, '0000')
self._sendData('Y', '#')
except Settings.IdExistException:
self._sendData('N', '#')
elif command == 'Add':
self._connectionTest()
print(getTime() + 'Get command:', command)
fromID, toID = self._getData().split(' ')
self._sendData(self.mysql.addFriend(fromID, toID), '#')
elif command == 'Refresh':
self._connectionTest()
print(getTime() + 'Get command:', command)
ID = self._getData()
maps = self.mysql.getSelfInfo(ID)
maps.update(self.mysql.getFriendsList(ID))
self._sendData(maps, '#')
elif command == 'Connect':
self._connectionTest()
print(getTime() + 'Get command:', command)
myID, toID = self._getData().split(' ')
index = ' '.join(sorted([myID, toID]))
x = chatPool.get(index)
if not x:
chatPool.update({index: {myID: self.clientSocket, toID: None}})
else:
chatPool[index][myID] = self.clientSocket
self._repostMsg()
elif command == 'Start':
self._connectionTest()
print(getTime() + 'Get command:', command)
ifOnline = 'Online' if clientPool.get(self._getData()) else 'Offline'
self._sendData(ifOnline, '#')
elif command == 'GroupIn':
self._connectionTest()
print(getTime() + 'Get command:', command)
talker = self._getData()
groupPool[talker] = self.clientSocket
self._groupTalk()
elif command == 'Close':
self._connectionTest()
print(getTime() + 'Get command:', command)
self.connectionEstablished = False
self._sendData(getTime() + 'Connection closed ' + str(self.clientAddr), '#')
print(getTime() + 'Connection closed ' + str(self.clientAddr))
self.clientSocket.close()
break
def run():
Server().serve_forever()
if __name__ == '__main__':
run()