def setUp(self): self.server = socket(AF_INET, SOCK_STREAM) address = ('',1234) self.server.bind(address) self.server.listen() self.client = ChatClient("Mauricio","localhost",1234) self.client.init_socket()
def identify_client(self, client_socket, client_address): not_identified = True while not_identified: sleep(0.3) identify = "Ingresa 'IDENTIFY username' (donde username es el nombre que usarás)" client_socket.send(bytes(identify, "utf-8")) sleep(0.3) disconnect = "O bien, ingresa 'DISCONNECT' para salir" client_socket.send(bytes(disconnect, "utf-8")) message = client_socket.recv(self.bufsize).decode("utf-8") if message.startswith("IDENTIFY"): name = message[len("IDENTIFY "):] try: self.public_room.verify_name(name) except ExistingUsernameException: username_error = '"%s" ya está en uso. Prueba con otro\n' % name client_socket.send(bytes(username_error, "utf-8")) continue host, port = client_address client = ChatClient(name, host, port) client.set_server_socket(client_socket) print('{}:{} se ha identificado como: {}'.format( host, port, name)) greeting = '¡Hola %s! Ahora puedes comenzar a disfrutar de PyChat :)' % name client_socket.send(bytes(greeting, "utf-8")) join_message = "%s se ha unido al chat. ¡Sé amable y di hola!" % name self.public_room.broadcast(join_message) self.public_room.add_client(client) return True, client elif message == "DISCONNECT": goodbye = 'Hasta luego :)' client_socket.send(bytes(goodbye, "utf-8")) sleep(0.3) client_socket.close() print( "Un cliente no identificado con la direccion %s:%s se ha ido" % client_address) return False, None error = "¡Creo que has cometido un error!" client_socket.send(bytes(error, "utf-8"))
def __init__(self, *args, **kwargs): super(RootBox, self).__init__(*args, **kwargs) self.chat_client = None self.receive_queue = Queue() # create the confirm box for quitting self.quit_confirm = ConfirmPopup(title='Quit?', confirm_text='Quit') self.quit_confirm.bind(on_confirm=self.quit_pushed) self.quit_confirm.bind(on_cancel=self.cancel_pushed) # there is some sort of instantiation order problem where you can't # directly refer to custom classes as children in the kv language, so # everywhere were we want custom classes there is a BoxLayout which we # will now put the custom classes inside self.menu = Menu() self.menu.hide_item('Chats') self.menu.bind(text=self.menu_action) self.ids.menu_container.add_widget(self.menu) self.chat_box = ChatBox() self.ids.chat_box_container.add_widget(self.chat_box) # don't add friend_box just yet, it has the connection status label in # it self.friend_box = FriendBox(self) store = JsonStore('storage.json') try: userid = store.get('account')['userid'] password = store.get('account')['password'] host = store.get('server')['host'] port = store.get('server')['port'] except KeyError: self.ids.connection_label.text = 'No Username Set' else: # create the chat client and start processing on separate thread self.chat_client = ChatClient(self, userid, password) self.chat_client.connect((host, port)) self.chat_client.process(block=False)
client.send("DISCONNECT") client.close_socket() top.quit() host = input('Enter host: ') port = input('Enter port: ') if not host: host = "localhost" if not port: port = 1234 else: port = int(port) client = ChatClient("Mauricio",host,port) client.init_socket() top = tkinter.Tk() top.title("PyChat: Chat built in Python") messages_frame = tkinter.Frame(top) my_msg = tkinter.StringVar() my_msg.set("") scrollbar = tkinter.Scrollbar(messages_frame) msg_list = tkinter.Listbox(messages_frame, height=30, width=100, yscrollcommand=scrollbar.set) scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y) msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH) msg_list.pack() messages_frame.pack() entry_field = tkinter.Entry(top, textvariable=my_msg)
from chatclient import ChatClient IP_ADDRESS = "127.0.0.1" PORT = 5500 if __name__ == "__main__": server = ChatClient(IP_ADDRESS, PORT) server.run()
def setUp(self): self.client = ChatClient("Mauricio") self.client2 = ChatClient("Aglae") self.public_room = ChatRoom() self.private_room = ChatRoom(self.client, "foo", False)