def start(self, minecraftServer=None): self.minecraftServer = minecraftServer if not self.isOnline(): self.log('Trying to start the client, connecting to ' + utils.addressToString(self.server_addr)) self.sock = socket.socket() # 发送客户端信息 try: self.sock.connect(self.server_addr) self.send_login(self.info.name, self.info.password) except socket.error: self.log('Fail to connect to the server') return # 获取登录结果 try: data = self.recieveData() result = json.loads(data)['result'] except socket.error: self.log('Fail to receive login result') return except ValueError: self.log('Fail to read login result') return self.log(utils.stringAdd('Result: ', result)) if result == 'login success': super(ChatClient, self).start() else: self.log('Client has already been started')
def __init__(self, configFile, LogFile, mode): js = json.load(open(configFile, 'r')) super(ChatClient, self).__init__(lib.ChatClientInfo(js['name'], js['password']), js['aes_key'], LogFile) self.mode = mode self.consoleOutput = mode != Mode.MCD self.server_addr = (js['server_hostname'], js['server_port']) self.log('Client Info: name = ' + self.info.name + ', password = '******'Mode = ' + mode) self.log('AESKey = ' + self.AESKey) self.log('Server address = ' + utils.addressToString(self.server_addr))
def __init__(self, configFile): config = json.load(open(configFile, 'r')) super(ChatServer, self).__init__('Server', GeneralLogFile, config['aes_key']) self.server_addr = utils.toUTF8(config['hostname']), config['port'] self.log('AESKey = ' + self.AESKey) self.log('Server address = ' + utils.addressToString(self.server_addr)) self.clients = [] for c in config['clients']: info = lib.ChatClientInfo(c['name'], c['password']) self.log('Adding Client: name = {0}, password = {1}'.format( info.name, info.password)) self.clients.append(ChatClient(info, self, self.AESKey))
def run(self): self.sock = socket.socket() try: self.sock.bind(self.server_addr) except socket.error: self.log('Fail to bind ' + str(self.server_addr)) return False self.sock.listen(5) self.online = True self.log('Server Started') while self.isOnline(): conn, addr = self.sock.accept() self.log( 'Client {0} connected, receiving initializing data'.format( utils.addressToString(addr))) try: js = json.loads(self.recieveData(conn)) # 接受客户端信息的数据包 self.log('Initializing data =' + str(js)) if js['action'] == 'login': info = lib.ChatClientInfo(js['name'], js['password']) flag = False for client in self.clients: if client.info == info: self.send_result('login success', conn) flag = True self.log('Starting client "' + client.info.name + '"') client.tryStart(conn, addr) break if flag == False: self.send_result('login fail', conn) else: self.log('Action not matches, ignore') except (ValueError, TypeError, KeyError) as err: self.log('Fail to read received initializing json data: ' + str(err)) except socket.error: self.log('Fail to respond the client') utils.sleep() return True
def run(self): self.sock = socket.socket() try: self.sock.bind(self.server_addr) except socket.error: self.log('Fail to bind ' + str(self.server_addr)) return False self.sock.listen(5) self.online = True self.log('Server Started') self.start_console_thread() try: while self.isOnline(): conn, addr = self.sock.accept() self.handle_client_connection(conn, addr) self.log( 'Client {0} connected, receiving initializing data'.format( utils.addressToString(addr))) utils.sleep() except: if self.isOnline(): raise return True
def run(self): self.log('Client address = ' + utils.addressToString(self.addr)) super(ChatClient, self).run()