def run(self): """ This method overrides the run method of the Thread class. It will be called automatically when the start() method is called. This loop will run until the client decides to close it. """ while True: # receive data on the connection data = self.client.recv(self.size) # data = bytes.decode(data) if data: # get the first word in the request command = data logging.debug("command: %s " % command) if 'HELO' in command: response_content = '200 HELO {}'.format(self.address) elif 'AUTH' in command: email = command.split('EMAIL:')[1].split(' PASSWORD:'******'PASSWORD:'******'_id']))) if save_token(token): logging.info("token saved") response_content = '200 TOKEN {}'.format(token) else: logging.error('Unauthorized') response_content = '401 ' + 'Unauthorized' elif 'VALIDATE_TOKEN' in command: token = command.split('VALIDATE_TOKEN ')[1] valid = validate_token(token) if valid: logging.debug('token valid...') response_content = 'VALID' else: logging.debug('token invalid...') response_content = 'NOT VALID' else: response_content = 'TCHAU' # sends data to the client status = self.client.send(response_content) # the return of send() are the byte sent # so, we can check if the data was sent successfully if not status: print "data wasn't sent successfully" else: print "Closing client | client {0}, address {1}".format( self.client, self.address) self.client.close() break
def _doing_connections(self): """ Method to make the connection """ logging.info("Waiting Request...") # socket to client and clients address .accept - accept connection client_connection, client_address = self.socket.accept() while True: # accept(): Return a new socket representing the connection, and the address of the client logging.debug("connection from: {}' ".format(client_address)) data = client_connection.recv(self.size) # receive the client data request_string = bytes.decode(data) # decode the request to string # get the first word in the request os.environ["REQUEST_METHOD"] = request_string.split(' ')[0] logging.debug("method: %s " % os.environ["REQUEST_METHOD"]) logging.debug("content: %s " % request_string) response_content = '' if os.environ["REQUEST_METHOD"] == 'HELO': response_content = 'HELO {}'.format(client_address) elif os.environ["REQUEST_METHOD"] == 'AUTH': email = request_string.split('EMAIL:')[1].split( ' PASSWORD:'******'PASSWORD:'******'_id'])) save_token(token) response_content = 'TOKEN {}'.format(token) elif os.environ["REQUEST_METHOD"] == 'VALIDATE_TOKEN': token = request_string.split('TOKEN ')[1] valid = validate_token(token) if valid: response_content = 'VALID' else: response_content = 'NOT VALID' elif os.environ["REQUEST_METHOD"] == 'BYE': break response_headers = self._build_header(Status.CODE200) server_response = response_headers.encode() + response_content client_connection.sendall(server_response) logging.info("closing connection with client...") client_connection.close()
def add_user(message): if not users_collection.find_one({'user_id': message.from_user.id}): new_user = dict() new_user['chat_id'] = message.chat.id new_user['user_id'] = message.from_user.id new_user['first_name'] = message.chat.first_name new_user['last_name'] = message.chat.last_name new_user['username'] = message.from_user.username new_user['places'] = [] users_collection.insert_one(new_user)
def check(): sessionID = request.headers['Authorization'] if checksession(sessionID): check_result = sessions_collection.find_one({'_id': sessionID}) userid = check_result['user'] user = users_collection.find_one({'_id': userid}) calendars = [] for calendar in calendars_collection.find({'owner': userid}): calendars.append(calendar) return json.dumps({"user": user, "calendars": calendars}) else: # Session is not availible return 'Session is not availible', 401
def user_data(user_id): return users_collection.find_one({'user_id': user_id})