def working(self): '''维护客户端的访问,客户端可以给服务端发送消息,但是只能进行验证和简单的查看服务器消息 无法做任何高级操作''' try: _client_message = self.clientsock.recv( LMServer.BUFSIZE) # 获取指定大小的报文 _text = decryption(_client_message) if _text: _obj = eval(_text.decode(LMServer.ENCODING)) # json对象\ # print(_obj) self.server.handle_user_command(self, _obj) else: self.send_message( payback(MsgType.ERROR, reason="invalid message")) except ConnectionResetError as e: self.debug("LMotorClient.working:" + str(e)) self.shutdown() except ConnectionAbortedError as e: self.shutdown() # 客户端正常被关闭 except json.JSONDecodeError as e: # 如果解析json遇到错误,自动忽略这条消息 # print("错误2") self.send_message(payback(MsgType.ERROR, reason="invalid message")) except Exception as e: self.shutdown() self.server.logger.log( "unknown error happened at client main loop:%s" % str(e), LogType.ERROR, "LMClient.working()")
def working(self): '''维护服务器的主逻辑代码''' try: self.debug("wait for connection...") pack = self.server_sock.accept() # 接受客户端的连接 _clientsock, _addr = pack # 解包 # 以下代码用于验证客户端是否是有效的客户端 _random_string = make_number_sequence() _clientsock.send( encryption(_random_string.encode(LMServer.ENCODING))) _message = _clientsock.recv(LMServer.BUFSIZE) data = decryption(_message) if data: data = data.decode(LMServer.ENCODING) if data == md5(_random_string, LMServer.ENCODING): self.debug("a connection from %s is built" % str(pack[1])) worker = LMClient(pack, self, debug=True) # 创建工作者 worker.start() worker.send_message(payback(MsgType.RESULT, msg=SUCCESS)) else: _clientsock.close() else: _clientsock.close() # 以上代码用于验证客户端是否是有效的客户端 # 遇到无效的客户端时直接关闭 except Exception as e: self.logger.log("unknow error happened at server main loop:%s" % str(e), LogType.ERROR, position="LMServer.working()")
def working(self): '''接受管理员的命令并添加到消息队列''' if self.valid_count >= 0: try: _message = self.managersock.recv(LMServer.BUFSIZE) # 获取指定大小的报文 _text = decryption(_message) if _text: _obj = eval(_text) # print(_obj) # 管理员线程必须由普通线程晋升而来,不然此处应该设置相应的安全措施 self.server.handle_admin_command(self, _obj) else: self.send_message( payback(MsgType.ERROR, reason="invalid message")) except ConnectionResetError as e: self.debug("LMotorManager.working:" + str(e)) self.clearsock(reason="在接受管理员命令的时候遇到了connection_reset_error") except json.JSONDecodeError as e: self.send_message( payback(MsgType.ERROR, reason="invalid message")) except SyntaxError as e: self.debug("LMotorManager.working:" + str(e)) pass except Exception as e: self.debug("LMotorManager.working:" + str(e)) self.clearsock(reason="在接受管理员命令的时候遇到了未知的错误:{}".format(str(e))) self.server.logger.log( "unknown error happened at manager main loop:%s,%s" % (str(e), str(type(e))), LogType.ERROR, "LMManager.working()") else: self.clearsock(reason="在接受管理员命令的时候发现管理员已经超过三次没有回复心跳包")
def connect(self): try: self.localsock.connect(self.server_addr) pwd = decryption(self.localsock.recv(BUFSIZE)).decode(ENCODING) pwd = encryption(md5(pwd,ENCODING).encode(ENCODING)) self.localsock.send(pwd) return True except Exception as e: print(e) return False
def working(self): ''' show message from server ''' while 1: try: msg = self.localsock.recv(BUFSIZE) msg = decryption(msg).decode(ENCODING) _obj = eval(msg) if _obj.get("type") == 999: msg = str({"type":999}) self.localsock.send(encryption(msg.encode(ENCODING))) else: print(_obj) self.flag = True except Exception as e: self.show_message(e) break