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(AF_INET, SOCK_STREAM) # TODO: Finish init process with necessary code # START self.host = host self.server_port = server_port # Flyttet opp hit fra run self.connection.connect((self.host, self.server_port)) # Lage en tråd # Trenger vi self. forran? thread = MessageReceiver(self, self.connection) # Starter tråden thread.start() self.parser = MessageParser() # Motta input fra bruker while True: user_input = raw_input() if (user_input != None) & (len(user_input) >= 1): # Stopper while løkka # if (user_input.startswith("logout")): # self.send_payload(user_input) # break; # else: self.send_payload(user_input)
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) while True: userInput = raw_input("") liste = [] if " " in userInput: content = "" liste = userInput.split(" ") request = liste[0] for i in range(1, len(liste)): content += liste[i] if (i != len(liste) - 1): content += " " else: request = userInput content = None self.send_payload(request, content) if request == "logout": self.disconnect() reciever = MessageReceiver(self, self.connection) reciever.daemon = True reciever.start()
class Client: def __init__(self, host, serverPort): self.host = host self.serverPort = serverPort self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.connection.connect((self.host,self.serverPort)) self.jsonObject = None def disconnect(self): self.connection.close() try: self.messageReceiver.stop() except: pass def rawInput(self): request = raw_input("Request type: ") if(request in ["login", "msg"]): content = raw_input("Content: ") self.jsonObject = json.dumps({'request': request, 'content': content}, indent=4) else: self.jsonObject = json.dumps({'request': request}, indent=4) return request def receiveMessage(self): self.messageReceiver = MessageReceiver(self.connection) self.messageReceiver.start() def send(self): self.connection.send(self.jsonObject)
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) receiver = MessageReceiver(self, self.connection) receiver.start() while True: command = input("> ") if len(command.split(" ")) > 1: # It's a command with content command = command.split(" ") if command[0] == "login": raw_message = {'request': 'login', 'content': command[1]} elif command[0] == "msg": messageString = "" for i in command[1:]: messageString += i + ' ' messageString = messageString[:-1] raw_message = {'request': 'msg', 'content': messageString} else: print("Unknown command! Please try again.") continue else: # It's a command without content if command == 'logout' or command == 'help' or command == 'history' or command == 'names': raw_message = {'request': command, 'content': None} else: print("Unknown command! Please try again.") continue JSON_message = json.dumps(raw_message) self.send_payload(JSON_message.encode("utf-8"))
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) while True: userInput = raw_input("") liste = [] if " " in userInput: content = "" liste = userInput.split(" ") request = liste[0] for i in range(1, len(liste)): content += liste[i] if(i != len(liste)-1): content += " " else: request = userInput content = None self.send_payload(request, content) if request == "logout": self.disconnect() reciever = MessageReceiver(self, self.connection) reciever.daemon = True reciever.start()
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.message_receiver = MessageReceiver(self, self.connection) self.message_receiver.start() print(bcolors.HEADER + bcolors.BOLD + ' Urban Robot Advanced Chat System') print(' --------------------------------' + bcolors.ENDC) print('You are now connected') self.help() while True: time.sleep(0.03) request = input('>>> ').lower().lstrip().rstrip() request_lower = request.lower() if re.search('^login(( *[^\s]+)|((\s)*(?!.)))', request_lower): self.login(request[6:].lstrip()) elif request_lower == 'logout': self.logout() elif re.search('^msg(( *[^\s]+)|((\s)*(?!.)))', request_lower): self.msg(request[4:].lstrip()) elif request_lower == 'names': self.names() elif request_lower == 'help': self.help() else: # TODO : Do something here print(bcolors.FAIL + '\tInvalid command!' + bcolors.ENDC) self.help()
def run(self): try: # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) messageReceiver = MessageReceiver(self, self.connection) messageReceiver.start() except socket.error as e: sys.exit("Connection to server refused.") while (not self.connection._closed): if self.state == State.LOGIN: print("Username:"******"Disconnected from server")
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.thread = MessageReceiver(self, self.connection) self.thread.start() while True: text = input().split(' ', 1) if (len(text) > 0 and len(text) < 3): if (len(text) == 1): text.append('') if (text[0] == 'login'): payload = json.dumps({ 'request': 'login', 'content': text[1] }) self.send_payload(payload) elif (text[0] == 'logout'): self.disconnect() elif (text[0] == 'names'): payload = json.dumps({'request': 'names'}) self.send_payload(payload) elif (text[0] == 'help'): payload = json.dumps({'request': 'help'}) self.send_payload(payload) elif (text[0] == 'msg'): payload = json.dumps({ 'request': 'msg', 'content': text[1] }) self.send_payload(payload) else: print('Unknown command, type "help" for help') else: print('Unknown command, type "help" for help')
class Client: def __init__(self, host, serverPort): self.host = host self.serverPort = serverPort self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.connection.connect((self.host, self.serverPort)) self.jsonObject = None def disconnect(self): self.connection.close() try: self.messageReceiver.stop() except: pass def rawInput(self): request = raw_input("Request type: ") if (request in ["login", "msg"]): content = raw_input("Content: ") self.jsonObject = json.dumps( { 'request': request, 'content': content }, indent=4) else: self.jsonObject = json.dumps({'request': request}, indent=4) return request def receiveMessage(self): self.messageReceiver = MessageReceiver(self.connection) self.messageReceiver.start() def send(self): self.connection.send(self.jsonObject)
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) # TODO: Finish init process with necessary code self.host = host self.server_port = server_port self.run() # Start a MessageReceiver thread receiver = MessageReceiver(self, self.connection) receiver.start() # Create a MessageParser object for use later self.parser = MessageParser() # Loop to handle continuously reading from input while True: user_input = raw_input() if user_input is not None and user_input is not "": self.send_payload(user_input)
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.MessageReceiver = MessageReceiver(self, self.connection) self.MessageReceiver.start() while not self.disconnected: # Lager tom pakke som skal sendes til server: self.data = {'request': None, 'content': None} # Henter request fra bruker: self.request = input(" > ") if (self.request == ""): continue elif (self.request == "login"): self.data['request'] = self.request self.data['content'] = input("username: "******"names", "logout", "help"]): self.data['request'] = self.request else: #Bare vanlig msg: self.data['request'] = "msg" self.data['content'] = self.request # Gjør om pakken til JSON: self.data_json = json.dumps(self.data) # Sender pakken til serveren: self.connection.send(self.data_json.encode()) # Sleep så man rekker å disconnecte time.sleep(0.5)
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 __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.host = host self.server_port = server_port self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.connect() self.messagereceiver = MessageReceiver("client", self.connection) self.messagereceiver.start() self.run() def run(self): while True: try: msg = self.prompt_user() self.send_request( bytes(jsonify(msg), 'utf-8') ) except Exception: print("Exception in client. Restarting client.") traceback.print_exc() return self def connect(self): while True: errno = self.connection.connect_ex((self.host, self.server_port)) if errno == 0: print("Connected to server.") return else: print("Connection failed. Retrying...") sleep(1) def prompt_user(self): msg = {} inp = input() print("\033[1A\033[K", end='') args = inp.split() if len(args) is not 0: msg['request'] = args[0] msg['content'] = "" if len(args)>1: msg['content'] = inp[len(args[0])+1:] return msg else: msg["content"] = "" msg["request"] = "" return msg def send_request(self, request): self.connection.send(request)
def run(self): # Initiate the connection to the server s_thread = MessageReceiver(self, self.connection) s_thread.daemon = True s_thread.start() while self.active: self.get_input()
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) # TODO: Finish init process with necessary code self.host = host self.server_port = server_port self.messageParser = MessageParser() self.messageReceiver = MessageReceiver(self, self.connection) self.run() def run(self): """ Main process of the client. Waits for input from user """ self.connection.connect((self.host, self.server_port)) self.messageReceiver.start() print 'Client running...\nType \'quit\' to end or \'help\' for info' while True: command = raw_input() if command == 'quit': self.disconnect() print 'Client closing' break self.send_payload(self.messageParser.parse_dataToSend(command)) def disconnect(self): """ Close sock connection """ self.connection.close() def receive_message(self, message): """ Prints received message after it's parsed """ print self.messageParser.parse(message) def send_payload(self, data): """ Sends data to server """ self.connection.send(data)
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) # print "[*] Kobling etablert" listener = MessageReceiver(self, self.connection) listener.daemon = True listener.start() # print "[*] Lytter startet." self.handle_input()
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) print "[*] Kobling etablert" lytter = MessageReceiver(self,self.connection) lytter.daemon = True lytter.start() print "[*] Lytter startet." self.handle_input()
class Client: """ This is the chat client class """ # Set to True if you wish to exit the chat client exit = False 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.server_address = (host, server_port) # Initiate the message parser class self.client_message_parser = ClientMessageParser() self.message_sender_thread = MessageSender self.message_receiver_thread = MessageReceiver self.run() def run(self): # Initiate the connection to the server self.connection.connect(self.server_address) # Instantiate the message receiver class as a new thread and start the thread self.message_receiver_thread = MessageReceiver(self, self.connection) self.message_receiver_thread.start() # Instantiate the message sender class as a new thread and start the thread self.message_sender_thread = MessageSender(self, self.connection) self.message_sender_thread.start() # Run until the user sets exit to False try: while not self.exit: payload = raw_input("") print "\n" if payload == 'exit': self.exit = True break elif self.input_is_valid(payload): encoded_message = self.client_message_parser.parse_user_input(payload) self.message_sender_thread.queue_payload_for_sending(encoded_message) finally: # Have to wait for the logout message to actually be sent self.disconnect() print "Disconnected from server." def disconnect(self): self.connection.close() def input_is_valid(self, payload): return not len(payload.strip()) == 0
class Client: """ This is the chat client class """ def __init__(self, host, server_port): """ This method is run when creating a new Client object """ self.host = host self.server_port = server_port # Set up the socket connection to the server self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TODO: Finish init process with necessary code self.run() def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.receiver = MessageReceiver(self, self.connection) self.receiver.start() def login(self, username): payload = json.dumps({"request": "login", "content": username}) self.send_payload(payload) def logout(self): payload = json.dumps({"request": "logout"}) self.send_payload(payload) def send_msg(self, msg): payload = json.dumps({"request": "msg", "content": msg}) self.send_payload(payload) def send_names_request(self): payload = json.dumps({"request": "names"}) self.send_payload(payload) def send_help_request(self): payload = json.dumps({"request": "help"}) self.send_payload(payload) def disconnect(self): self.logout() # Close Connection? def receive_message(self, message): # TODO: Handle incoming message print message def send_payload(self, data): self.connection.send(data)
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.messageReceiver = MessageReceiver(self, self.connection) self.run()
class Client: """ This is the chat client class """ def __init__(self, host, server_port): """ This method is run when creating a new Client object """ self.serverport = server_port self.h = host # Set up the socket connection to the server self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.MParser = MessageParser() self.MReceiver = MessageReceiver(self, self.connection) self.run() def run(self): # Initiate the connection to the server self.connection.connect((self.h, self.serverport)) self.MReceiver.start() while True: userInput = raw_input() try: if userInput: request, content = userInput.split() if not content: content = None payload = { 'request': request, 'content': content } self.send_payload(payload) except KeyboardInterrupt: self.disconnect() def handleInput(self, userInput): message = userInput.split(' ') def disconnect(self): self.connection.close() print "Server disconnected." def receive_message(self, message): print self.MParser.parse(message) def send_payload(self, data): payload = json.dumps(data) self.connection.send(payload)
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.thread = MessageReceiver(self, self.connection) self.thread.start() while True: time.sleep(0.5) request_content = input("Type in your request:").split(' ', 1) try: request_content[0] = request_content[0].lower() request_content[1] = request_content[1].lower() print(request_content) if request_content[1] == "none": if request_content[0] == "names": self.send_payload({ "request": "names", "content": "None" }) elif request_content[0] == "help": self.send_payload({ "request": "help", "content": "None" }) else: if request_content[0] == "logout": self.disconnect() elif request_content[0] == "names": self.send_payload({ "request": "names", "content": "None" }) elif request_content[0] == "help": self.send_payload({ "request": "help", "content": "None" }) elif request_content[0] == "login": self.send_payload({ "request": "login", "content": request_content[1] }) elif request_content[0] == "msg": self.send_payload({ "request": "msg", "content": request_content[1] }) else: self.not_supported(request_content) except IndexError: print("Input not valid!")
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.thread = MessageReceiver(self, self.connection) self.thread.start() while True: txt = input().split(' ', 1) #Split on first space if (len(txt) > 0 and len(txt) < 3): # Dont accept more than 2 arguments if (len(txt) == 1): txt.append('') #What if short input? if (txt[0] == 'login'): payload = json.dumps({ 'request': 'login', 'content': txt[1] }) self.send_payload(payload) elif (txt[0] == 'msg'): payload = json.dumps({'request': 'msg', 'content': txt[1]}) self.send_payload(payload) elif (txt[0] == 'names'): payload = json.dumps({ 'request': 'names', 'content': txt[1] }) self.send_payload(payload) elif (txt[0] == 'logout'): payload = json.dumps({ 'request': 'logout', 'content': txt[1] }) self.send_payload(payload) elif (txt[0] == 'help'): payload = json.dumps({ 'request': 'help', 'content': txt[1] }) self.send_payload(payload) elif (txt[0] == 'history'): payload = json.dumps({ 'request': 'history', 'content': txt[1] }) self.send_payload(payload) else: print( 'Oh oh! No such command, type "help" to see all possible commands' ) else: print( 'Oh oh! No such command, type "help" to see all possible commands' )
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.server_port = server_port self.userLoggedIn = False self.messageReceiver = MessageReceiver( self, self.connection) # is a thread by itself. self.userInput = UserInput(self) # is a thread by itself. self.parser = MessageParser(self) self.run() def run(self): # Initiate the connection to the server try: self.connection.connect((self.host, self.server_port)) except: print "No connection to the server were established. Exiting." sys.exit() self.messageReceiver.start() # start the thread self.userInput.start() # start the thread print "Client Ready. Please type command. type 'help' to view server commands." def disconnect(self): self.connection.close() print "Server disconnected" self.userInput.kill() sys.exit() pass def receive_message(self, message): parsed_message = self.parser.parse(message) print parsed_message def send_payload(self, data): json = self.parser.encode(data) self.connection.send(json) pass
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.server_port = server_port self.userLoggedIn = False self.messageReceiver = MessageReceiver(self, self.connection) # is a thread by itself. self.userInput = UserInput(self) # is a thread by itself. self.parser = MessageParser(self) self.run() def run(self): # Initiate the connection to the server try: self.connection.connect((self.host, self.server_port)) except: print "No connection to the server were established. Exiting." sys.exit() self.messageReceiver.start() # start the thread self.userInput.start() # start the thread print "Client Ready. Please type command. type 'help' to view server commands." def disconnect(self): self.connection.close() print "Server disconnected" self.userInput.kill() sys.exit() pass def receive_message(self, message): parsed_message = self.parser.parse(message) print parsed_message def send_payload(self, data): json = self.parser.encode(data) self.connection.send(json) pass
def run(self): self.connection.connect((self.host, self.server_port)) message_receiver = MessageReceiver(self, self.connection) message_receiver.start() while True: message = raw_input('Enter command \n') self.send_payload(message) if message == 'logout': time.sleep(1) break self.disconnect()
def createReceiverService(self): self.messageReceiverObject = MessageReceiver() messageReceiverThread = QThread() self.messageReceiverObject.moveToThread(messageReceiverThread) messageReceiverThread.started.connect(self.messageReceiverObject.runReceiver) self.messageReceiverObject.updatedHosts.connect(self.updateAvailableHosts) self.messageReceiverObject.updatedHosts.connect(self.updateHostsToNotify) self.messageReceiverObject.votingUpdate.connect(self.updateVotings) self.messageReceiverObject.finished.connect(messageReceiverThread.quit) self.messageReceiverObject.finished.connect(self.messageReceiverObject.deleteLater) messageReceiverThread.finished.connect(messageReceiverThread.deleteLater) return messageReceiverThread
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.server_port = server_port self.MessageReceiver = MessageReceiver(self, self.connection) self.run() def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.MessageReceiver.start() while True: data = raw_input("") if data == 'exit': self.disconnect() sys.exit() self.send_payload(str(data)) def disconnect(self): self.send_payload(json.dumps({'request': 'logout'})) self.connection.close() def send_payload(self, data): splitted = data.split(" ") if data.find('help') != -1 or data.find('names') != -1 or data.find( 'history') != -1 or data.find('logout') != -1: self.connection.sendall(json.dumps({'request': splitted[0]})) elif data.find('login') != -1: self.connection.sendall( json.dumps({ 'request': splitted[0], 'content': splitted[1] })) else: self.connection.sendall( json.dumps({ 'request': 'msg', 'content': data }))
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) worker = MessageReceiver(self, self.connection) worker.daemon = True worker.start() running = True while running: raw = raw_input() if raw == "exit": running = False self.disconnect() else: self.send_payload(raw)
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) adress = ('localhost', 9998) self.connection.connect(adress) receiver = MessageReceiver(self, self.connection) receiver.start() self.running = True self.run()
class Client: def __init__(self, host, server_port): self.host = host self.server_port = 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) # TODO: Finish init process with necessary code self.run() def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.thread = MessageReceiver(self, self.connection) self.thread.start() while (1): new_payload = self.take_input() if new_payload['request'] == 'disconnect': self.disconnect() exit() self.send_payload(json.dumps(new_payload)) time.sleep(0.1) def disconnect(self): self.connection.close() def receive_message(self, message): msg_parser = MessageParser() print msg_parser.parse(message) def send_payload(self, data): self.connection.send(data) def take_input(self): payload = {} payload['request'] = raw_input() if payload['request'] == 'login' or payload['request'] == 'msg': payload['content'] = raw_input('Enter content:') print '\n' return payload
def run(self): print "Type 'help' if stuck" self.connection.connect((self.host, self.server_port)) thread1 = MessageReceiver(self, self.connection) thread1.start() while True: self.data = raw_input(">>> ") print "\033[A \033[A" # shit starts here # print self.data i = 0 if self.data == "help": print "HELP\n---------------\n\nlogin <username> ------ log in with the given username\n" \ "logout -------- log out \nmsg <message> ------ send message \nnames ------- list users in chat" \ "\nhelp ------- view help text " continue elif self.data[:5] == "login": # make this a login message i = 6 self.jdata['request'] = "login" elif self.data[:6] == "logout": # make this a logout message i = 7 self.jdata['request'] = "logout" elif self.data[:3] == "msg": # this is a message i = 4 self.jdata['request'] = "msg" elif self.data[:5] == "names": # client asks for names i = 6 self.jdata['request'] = "names" elif self.data[:7] == "history": # client asks for names i = 8 self.jdata['request'] = "history" else: print "ERROR - no command was recognised" continue # shit ends here (probably not) self.jdata['content'] = self.data[i:] #strip it json_data = json.dumps(self.jdata) # print "JSON to be sent: " + json_data # print json.dumps(json_data, indent=4, sort_keys=True) self.connection.sendall(json_data) # + "\n") # self.received = self.connection.recv(1024) # v2 # print self.received self.disconnect()
def run(self): # Initiate the connection to the server self.connection.connect((self.server_host, self.server_port)) # We kick off a background thread which'll listen for incoming messages from # the server. Whenever a message from the server is received, the MessageReceiver # will call the receive_message method in this class. thread = MessageReceiver(self, self.connection) thread.start() # We listen for user input and send it to the dispatcher for formatting. while True: input_string = str(input("")) self.dispatcher(input_string)
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.thread = MessageReceiver(self, self.connection) self.thread.start() while (1): new_payload = self.take_input() if new_payload['request'] == 'disconnect': self.disconnect() exit() self.send_payload(json.dumps(new_payload)) time.sleep(0.1)
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.receiver = MessageReceiver(self, self.connection) self.MessagePars = MessageParser() while True: input_ = raw_input().split(' ') if len(input_) != 0: if (input_[0] == "login"): self.send_payload({ 'request': 'login', 'content': input_[1] }) elif (input_[0] == "logout"): self.send_payload({ 'request': 'logout', }) elif (input_[0] == "msg"): self.send_payload({'request': 'msg', 'content': input_[1]}) elif (input_[0] == "names"): self.send_payload({'request': 'names'}) elif (input_[0] == "help"): self.send_payload({'request': 'help'}) else: print "Error, type 'help'"
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.msgParser = MessageParser() msgReceiver = MessageReceiver(self, self.connection) msgReceiver.start() print("INSTRUCTIONS\nUser must login first - type 'login <username>'\ntype 'help' for list over available commands\n\n") while self.connection: userinput = raw_input() if userinput == 'logout': self.disconnect() elif userinput == 'exit': exit() else: self.send_payload(userinput)
def run(self): # Connecting to server self.connection.connect((self.host, self.server_port)) #Creats the running thread for recieving messages self.thread = MessageReceiver(self, self.connection) self.thread.start() #--> run in MessageReceiver running_status = self.thread.is_alive() while running_status: #Collects input from chat-user user_input = raw_input().split(' ', 1 ) request = user_input[0] content = 'None' try: content = user_input[1] except Exception, e: pass if request == 'login': try: content = user_input[1] except Exception, e: print '[ERROR] Username unvalid. Try another username with the allowed characters: A-Z, a-z, 0-9' pass payload = json.dumps({'request': 'login', 'content': content}) self.send_payload(payload)
def run(self): # Initiate the connection to the server self.connection.connect(self.server_address) # Instantiate the message receiver class as a new thread and start the thread self.message_receiver_thread = MessageReceiver(self, self.connection) self.message_receiver_thread.start() # Instantiate the message sender class as a new thread and start the thread self.message_sender_thread = MessageSender(self, self.connection) self.message_sender_thread.start() # Run until the user sets exit to False try: while not self.exit: payload = raw_input("") print "\n" if payload == 'exit': self.exit = True break elif self.input_is_valid(payload): encoded_message = self.client_message_parser.parse_user_input(payload) self.message_sender_thread.queue_payload_for_sending(encoded_message) finally: # Have to wait for the logout message to actually be sent self.disconnect() print "Disconnected from server."
def run(self): # Initiate the connection to the server print "client: connecting" self.connection.connect((self.host, self.server_port)) self.thread = MessageReceiver(self, self.connection) self.thread.start() print "client: connected" print "1. login <username>, 2. logout, 3. msg <message>, 4. history, 5. users, 6. help\n" while True: payload = self.create_response() self.send_payload(payload) if input is "logout": self.disconnect() break
def run(self): # TODO # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) receiver = MessageReceiver(self, self.connection) receiver.start() running = True while running: print("What to do: ") client_input = input() if client_input == "exit": running = False self.disconnect() else: self.send_payload(client_input)
class Client: def __init__(self, host, server_port): # 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() self.msg = '' def run(self): # Connecting to server self.connection.connect((self.host, self.server_port)) #Creats the running thread for recieving messages self.thread = MessageReceiver(self, self.connection) self.thread.start() #--> run in MessageReceiver running_status = self.thread.is_alive() while running_status: #Collects input from chat-user user_input = raw_input().split(' ', 1 ) request = user_input[0] content = 'None' try: content = user_input[1] except Exception, e: pass if request == 'login': try: content = user_input[1] except Exception, e: print '[ERROR] Username unvalid. Try another username with the allowed characters: A-Z, a-z, 0-9' pass payload = json.dumps({'request': 'login', 'content': content}) self.send_payload(payload) elif request == 'logout': payload = json.dumps({'request': 'logout', content: None}) self.send_payload(payload)
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(AF_INET, SOCK_STREAM) self.host = host self.server_port = server_port self.message_reciever = MessageReceiver(self, self.connection) self.message_parser = MessageParser(self) self.received_answer = True self.is_logged_in = False self.run()
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.server_port = server_port self.messageReceiver = MessageReceiver(self, self.connection) self.run() # TODO: Finish init process with necessary code def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) self.messageReceiver.start() print 'Welcome to this chatting room! Start chatting by using command login <username>' while True: input = raw_input('>') self.send_payload(input) def disconnect(self): # TODO: Handle disconnection self.connection.disconnect() def receive_message(self, message): # TODO: Handle incoming message1 print(message) def send_payload(self, data): # TODO: Handle sending of a payload stringarray = data.split(' ') content = "" for i in range(1, len(stringarray)): content += stringarray[i] content += ' ' data = {'request': stringarray[0], 'content': content} self.connection.sendall(json.dumps(data))
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.msg = MessageReceiver(self, self.connection) self.host = host self.server_port = server_port self.hasLoggedOn = False self.connection.connect((self.host, self.server_port)) #msg is a request sent from the client to the server print("Messager Reciever started") self.msg.start() print("Run method started!") self.run()
def __init__(self, host, server_port): """ This method is run when creating a new Client object """ self.loggedon = False # Set up the socket connection to the server try: self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error as e: print "Failed to create socket. Error message:", e self.host = host self.server_port = server_port self.lastMessageRecieved = None self.parser = MessageParser() self.run() self.rcv = MessageReceiver(self, self.connection) self.rcv.start()
class Client: def __init__(self, host, server_port): self.host = host self.server_port = server_port self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.run() def run(self): self.connection.connect((self.host, self.server_port)) self.messagereceiver = MessageReceiver(self, self.connection) self.messagereceiver.start() def disconnect(self): self.messagereceiver.is_running = False self.connection.close() pass def send_payload(self, data): self.connection.send(data.encode("UTF-8")) pass
def __init__(self, host, server_port): """ This method is run when creating a new Client object """ self.serverport = server_port self.h = host # Set up the socket connection to the server self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.MParser = MessageParser() self.MReceiver = MessageReceiver(self, self.connection) self.run()
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.msgRec = MessageReceiver(self,self.connection) self.hasLoggedOn = False self.run()
def run(self): self.connection.connect((self.host, self.server_port)) self.thread = MessageReceiver(self, self.connection) self.thread.daemon=True self.thread.start() while True: input_= raw_input('>> ') self.send_payload(input_) if input_== 'quit': break self.disconnect()
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) receiver = MessageReceiver(self, self.connection) receiver.start() while True: command = input("> ") if len(command.split(" ")) > 1: # It's a command with content command = command.split(" ") if command[0] == "login": raw_message = { 'request': 'login', 'content': command[1] } elif command[0] == "msg": messageString = "" for i in command[1:]: messageString += i + ' ' messageString = messageString[:-1] raw_message = { 'request': 'msg', 'content': messageString } else: print("Unknown command! Please try again.") continue else: # It's a command without content if command == 'logout' or command == 'help' or command == 'history' or command == 'names': raw_message = { 'request': command, 'content': None } else: print("Unknown command! Please try again.") continue JSON_message = json.dumps(raw_message) self.send_payload(JSON_message.encode("utf-8"))
def run(self): # Initiate the connection to the server self.connection.connect((self.host, self.server_port)) # Sets up a unique thread self.thread = MessageReceiver(self, self.connection) # Start listening for messages self.thread.start() # Listen for and respond to input while True: inputvalue = raw_input().split(' ', 1) action = inputvalue[0] # command might only be 1 word try: content = inputvalue[1] except Exception as e: pass if action == 'login': if self.thread.isActive is False: print('Re-Run the program to reconnect.') return payload = json.dumps({'request': 'login', 'content': content}) self.send_payload(payload) elif action == 'logout': payload = json.dumps({'request': 'logout'}) self.send_payload(payload) self.disconnect() print('disconnected') elif action == 'names': payload = json.dumps({'request': 'names'}) self.send_payload(payload) elif action == 'help': payload = json.dumps({'request': 'help'}) self.send_payload(payload) elif action == 'msg': payload = json.dumps({'request': 'msg', 'content': content}) self.send_payload(payload) elif action == 'history': payload = json.dumps({'request': 'history'}) self.send_payload(payload) else: print('Unknown command, type "help" for help')
def run(self): # Kobler til server self.connection.connect((self.host, self.server_port)) #Lager en traad å kjore paa self.thread = MessageReceiver(self, self.connection) #Kjorer traaden self.thread.start() running_status = self.thread.is_alive() while running_status: #Hent input fra bruker user_input = raw_input().split(' ', 1 ) print "Melding fra bruker mottatt " #Sorter keyword og innhold, takler kun 'help', 'names' request = user_input[0] try: content = user_input[1] except Exception, e: pass print "Formatert melding " if request == 'login': print "Login mottatt" payload = json.dumps({'request': 'login', 'content': content}) print "Pakket som json" self.send_payload(payload) print "Sendt med self.send_payload(payload)" elif request == 'logout': payload = json.dumps({'request': 'logout'}) self.send_payload(payload) elif request == 'msg': payload = json.dumps({'request': 'msg', 'content': content}) self.send_payload(payload) elif request == 'names': payload = json.dumps({'request': 'names'}) self.send_payload(payload) elif request == 'help': payload = json.dumps({'request': 'help'}) self.send_payload(payload) else: print "Hva er det du prover paa? " return
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.messageParser = MessageParser() self.host = "" self.server_port = 9998 # TODO: Finish init process with necessary code self.run() self.messageReceiver = MessageReceiver(self,self.connection) self.messageReceiver.start()
def __init__(self, host, server_port): """ This method is run when creating a new Client object """ self.host = (host, server_port) # Set up the socket connection to the server self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set up the incoming messages queue: self.queue = Queue() self.prompt = ["username: ", []] #ech, spis meg self.MessageParser = MessageParser() self.MessageReceiver = MessageReceiver(self.connection, self) self.run()
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.userLoggedIn = False self.messageReceiver = MessageReceiver(self, self.connection) # is a thread by itself. self.userInput = UserInput(self) # is a thread by itself. self.parser = MessageParser(self) self.run()