def __init__(self, host, server_port): """ This method is run when creating a new Client object """ # Set up the socket connection to the server self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.host = host self.server_port = server_port self.run() receiver = MessageReceiver(self, self.connection) receiver.start() while alive: msg = raw_input() msg = msg.split() request = msg[0] if len(msg)==2: content = msg[1] else: content = None payload = json.dumps({'request':request, 'content':content}) self.send_payload(payload) receiver.join() self.disconnect()
def __init__(self, host, server_port): """ This method is run when creating a new Client object """ # Set up the socket connection to the server self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.host = host self.server_port = server_port self.run() receiver = MessageReceiver(self, self.connection) receiver.start() while alive: msg = raw_input() msg = msg.split() request = msg[0] if len(msg) == 2: content = msg[1] else: content = None payload = json.dumps({'request': request, 'content': content}) self.send_payload(payload) receiver.join() self.disconnect()
class Client: """ This is the chat client class """ def __init__(self, host, server_port): """ This method is run when creating a new Client object """ # Set up the socket connection to the server self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.host = host; self.port = server_port; # TODO: Finish init process with necessary code self.run() def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.port)) self.connected = True self.msg_parser = MessageParser() self.msg_receiver = MessageReceiver(self, self.connection) self.msg_receiver.start() while self.connected: inp = raw_input("command <content>:\n").split() command = inp[0] if command not in legal_requests: print("please type a legal command") continue content = None if command == 'login' or command == 'msg': if len(inp) <= 1: print("please type <content>") continue content = " ".join(inp[1:]) data = { 'request': command, 'content': content} self.send_payload(json.dumps(data)) self.msg_receiver.join() def disconnect(self): print("disconnected from server") self.connected = False def receive_message(self, message): message = self.msg_parser.parse(message) try: response = message['response'] content = message['content'] except: print "error" print message return if response == 'error': print content elif response == 'history': for chat_entry in content: print(chat_entry['sender'] + ":" + chat_entry['content']) elif response == 'message': sender = message['sender'] print sender + ":" + content elif response == 'info': print content else: print "wops" + message def send_payload(self, data): self.connection.sendto(data, (self.host, self.port))
print_menu() selection = input('>>> ') if selection == '0': pass elif selection == '1': print_messages() elif selection == '2': invio_messaggio() elif selection == '3': blocca_utente() elif selection == '4': sblocca_utente() else: print('\nOPZIONE NON DISPONIBILE') if __name__ == "__main__": mr = MessageReceiver(config.SERVER_URL, config.USER_ID, config.DB_PATH, config.MEDIA_FOLDER) print('Aggiornamento contatti...') update_users() mr.start() main_menu() mr.stop() mr.join()