def socket_client(): """This function is used for testing purposes""" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(ADDRESS)) s.connect(ADDRESS) print("Connected.") # while True: # message = input("Enter message (blank input to end): ") # if(not message): # socket_utils.sendJson(s, { "end": True }) # break socket_utils.sendJson( s, { "user_email": "*****@*****.**", "name": "Alexa", "status": "logged in" }) object = socket_utils.recvJson(s) print("Received:") print(object) print("Disconnecting from server.") print("Done.")
def login(user): """ Method that validates and enables the user to login to the library server. Parameters: user: Assigns to the user that is logged in at that instance. """ sys.path.append("..") DB_NAME = "user.db" with open("config.json", "r") as file: data = json.load(file) HOST = data["masterpi_ip"] # The server's hostname or IP address. PORT = 13942 # The port used by the server. ADDRESS = (HOST, PORT) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(ADDRESS)) s.connect(ADDRESS) print("Connected.") print("Logging in as {}".format(user["username"])) socket_utils.sendJson(s, user) print("Waiting for Master Pi...") while(True): object = socket_utils.recvJson(s) if("logout" in object): print("Master Pi logged out.") print() break
def run(self): """ start the socket server """ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(self.ADDRESS) s.listen() print("Listening on {}...".format(self.ADDRESS)) while True: print("Waiting for Reception Pi...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() user = socket_utils.recvJson(conn) print('user: '******'realName']) print("username: "******"logout": True})
def main(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(ADDRESS) s.listen() print("Listening on {}...".format(ADDRESS)) while True: print("Waiting for Reception Pi...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() user = socket_utils.recvJson(conn) engineer = socket_utils.recvJson(conn) menu(user) engineerMenu(engineer) socket_utils.sendJson(conn, {"logout": True})
def login(user): """ - Prompts the user to Login and connects with Master Pi - Gives the user 2 options with their car - Either to Unlock or Return""" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(ADDRESS)) s.connect(ADDRESS) print("Connected.") print("Logging in as {}".format(user["username"])) print("Waiting for Master Pi...") while(True): print() choice = input(""" Please choose to either: 1: Unlock Car 2: Return Car Please enter your choice: """) if choice == "1": user["finish"] = 0 break elif choice == "2": user["finish"] = 1 break else: print("\n You must only select either 1 or 2") print(" Please try again\n") print() socket_utils.sendJson(s, user) while(True): object = socket_utils.recvJson(s) if("authenticated" in object): print("Car Unlocked") print() break elif("returned" in object): print("Car Returned") print() break elif("nope" in object): print("Not Authorized") print() break elif("nobooking" in object): print("No Booking Found") print() break
def main(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(ADDRESS) s.listen() print("Listening on {}...".format(ADDRESS)) while True: print("---------------------------") print("Waiting for Reception Pi...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() data = socket_utils.recvJson(conn) if("type" in data): #Login if(data['type'] == 'login'): result = Login(data['username'], data['pass'], data['car_id']) socket_utils.sendJson(conn, result) #Facial Login elif(data['type'] == 'face-login'): result = FaceLogin(data['username'], data['car_id']) socket_utils.sendJson(conn, result) #Engineer Login elif(data['type'] == 'eng-login'): result = EngLogin(data['username'], data['car_id']) socket_utils.sendJson(conn, result) #Logout elif(data['type'] == 'logout'): result = Logout(data['username'], data['car_id'], data['bId'], data['token']) socket_utils.sendJson(conn, result) #Engineer Logout elif(data['type'] == 'eng-comp'): result = engComp(data['username'], data['car_id'], data['token']) socket_utils.sendJson(conn, result) #Car login - when car comes online elif(data['type'] == 'carReg'): result = carOnline(data['car_id'], data['lat'], data['lng'], True) socket_utils.sendJson(conn, result) #Car location update elif(data['type'] == 'carLoc'): result = updateOnly(data['car_id'], data['lat'], data['lng']) socket_utils.sendJson(conn, result) #Car Logout - when car goes offline elif(data['type'] == 'carOff'): result = carOnline(data['car_id'], data['lat'], data['lng'], False) socket_utils.sendJson(conn, result) else: print("Type Error: {}".format(data['type'])) socket_utils.sendJson(conn, {"error": True, "type": "{}".format(data['type']), "msg": "Invalid Type value - {}".format(data['type'])}) else: print("No type attribute") socket_utils.sendJson(conn, {"error": True, "type": "Missing", "msg": "Type Attribute missing"})
def mainRun(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(self.ADDRESS)) s.connect(self.ADDRESS) print("Connected.") print("Logging in as {}".format(self.jsonData)) socket_utils.sendJson(s, self.jsonData) print("Waiting for Master Pi...") while (True): object = socket_utils.recvJson(s) if ("logout" in object): print("Master Pi logged out.") print() break
def login(user): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(ADDRESS)) s.connect(ADDRESS) print("Connected.") print("Logging in as {}".format(user["username"])) socket_utils.sendJson(s, user) print("Waiting for Master Pi...") while(True): object = socket_utils.recvJson(s) if("logout" in object): print("Master Pi logged out.") print() break
def _execute(self, payload): '''Execute command via TCP connection to Master Pi Args: payload (dict): data being transfer to Master Pi Returns: (dict) result including rc and an account dict ''' with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect( (self._conf["master"]["host"], self._conf["master"]["port"])) socket_utils.sendJson(s, payload) resp = socket_utils.recvJson(s) return resp
def run(self): '''Start running the daemon''' with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(self._addr) s.listen() print("Listening on {}...".format(self._addr)) while True: print("Waiting for Agent...") conn, addr = s.accept() with conn: print("Accepted connection from {}...".format(addr)) data = socket_utils.recvJson(conn) resp = self._updateDetails(data) socket_utils.sendJson(conn, resp)
def login(user): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(ADDRESS)) s.connect(ADDRESS) print("Connected.") print("Logging in as {}".format(user)) #Sending information to the master pi socket_utils.sendJson(s, user) print("Waiting for Master Pi...") #Loop to wait for the master pi to send logout = True while(True): object = socket_utils.recvJson(s) if("logout" in object): print("Master Pi logged out.") print() break
def __send_json(self, json): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(self.__ADDRESS)) s.connect(self.__ADDRESS) print("Connected.") socket_utils.sendJson(s, json) print("Waiting for Master Pi...") while(True): object = socket_utils.recvJson(s) if object: #print("object is", object) if(object['Response']=="recieve_pickle"): pickle_data = socket_utils.recvPickle(s, self.__pickle_path) if (pickle_data): print("pickle updated successfully") break
def queryConnect(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(ADDRESS) s.listen() print("Listening on {}...".format(ADDRESS)) while True: print("Waiting for authentication...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() user = socket_utils.recvJson(conn) username = user[0] email = user[2] Menu().userMenu(username, email) socket_utils.sendJson(conn, {"logout": True})
def socket_client(user_email): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(ADDRESS)) s.connect(ADDRESS) print("Connected.") user = get_user(user_email) print("Logging in as {}".format(user_email)) socket_utils.sendJson(s, user) while True: obj = socket_utils.recvJson(s) if obj["status"] == "logged out": print("Logging out ...") curs.execute( "UPDATE library_users SET status=%s WHERE email=%s", ("logged out", user_email)) conn.commit() print("{} has logged out".format(user_email)) break
def main(self): """This is the driver function for server :param: None :return: None """ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(self.__ADDRESS) s.listen() print("Listening on {}...".format(self.__ADDRESS)) while True: print("Waiting for Reception Pi...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() data = socket_utils.recvJson(conn) req_type = data["req_type"] if (req_type == "verify_booking_details"): self.__receive_booking_details(data["username"], data["booking_id"], data["car_id"], conn) elif (req_type == "verify_credentials"): self.__receive_credentials(data["enc_password"], data["username"], conn) elif (req_type == "unlock_details"): self.__receive_unlock_details(data["unlock_time"], data["car_id"], conn) elif (req_type == "lock_details"): self.__receive_lock_details(data["lock_time"], data["car_id"], data["duration"], conn) elif (req_type == "load_pickle"): self.__receive_pickle_update_req(conn) elif (req_type == "get_engineer_details"): self.__get_engineer_details(data["car_id"], conn) elif (req_type == "update_report_status"): self.__update_report_status(data["reportid"], data["status"], conn)
def main(): #setting up the socket and listening on the Address with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(ADDRESS) s.listen() print("Listening on {}...".format(ADDRESS)) while True: print("Waiting for Reception Pi...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() #recieving information from the reception pi user = socket_utils.recvJson(conn) #HERE IS WHERE OUR MENU WILL BE PLACED menu(user) #closing the connection by sending logout = True socket_utils.sendJson(conn, {"logout": True})
def main(): """ A main method that calls connects the user to the library server. """ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(ADDRESS) s.listen() print("Listening on {}...".format(ADDRESS)) while True: print("Waiting for Reception Pi...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() user = socket_utils.recvJson(conn) LMSUserDatabaseUtils().addUser(user) LMSUserDatabaseUtils().getUser(user) menu(user) socket_utils.sendJson(conn, {"logout": True})
def main(): with DatabaseUtils() as db: db.createCustomerTable() db.createCarTable() db.createBookHistoryTable() print(db.getCustomer()) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(ADDRESS) s.listen() print("Listening on {}...".format(ADDRESS)) while True: print("Waiting for Reception Pi...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() user = socket_utils.recvJson(conn) menu(user) socket_utils.sendJson(conn, {"logout": True})
import pprint import socket import socket_utils # HOST = input("Enter IP address of server: ") HOST = "10.247.202.192" # The server's hostname or IP address. PORT = 64000 # The port used by the server. ADDRESS = (HOST, PORT) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Connecting to {}...".format(ADDRESS)) s.connect(ADDRESS) print("Connected.") while True: message = input("Enter message (blank input to end): ") if not message: socket_utils.sendJson(s, {"end": True}) break socket_utils.sendJson(s, {"message": message}) object = socket_utils.recvJson(s) print("Received:") pprint.pprint(object) print(object["message"]) print("Disconnecting from server.") print("Done.")
break else: print('Invalid Input!') time.sleep(2) except Exception as e: print(e) time.sleep(2) if __name__ == '__main__': # Opening a socket server and wait for Reception's Pi to respond with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(ADDRESS) # Infinite loop to prevent program frond exiting while True: print("Waiting for Reception Pi...") s.listen() conn, addr = s.accept() print("Connected") object = socket_utils.recvJson(conn) if ("end" in object): break # Notify users that their information has been received print("Received") menu = Menu.get_instance() # Invoke main menu menu.display_menu(object['name'], object['user_email']) # Return the user JSON with a changed status to let Reception Pi knows the user wants to log out object['status'] = "logged out" # print("Sending data back.") socket_utils.sendJson(conn, object)
def main(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(ADDRESS) s.listen() print("Listening on {}...".format(ADDRESS)) while True: print("---------------------------") print("Waiting for Reception Pi...") conn, addr = s.accept() with conn: print("Connected to {}".format(addr)) print() data = socket_utils.recvJson(conn) if ("type" in data): #Login if (data['type'] == 'login'): print("Login - {} [Car ID({})]".format( data['username'], data['car_id'])) result = Login(data['username'], data['pass'], data['car_id']) socket_utils.sendJson(conn, result) #Facial Login elif (data['type'] == 'face-login'): print("Facial Recognition Login - {} [Car ID({})]". format(data['username'], data['car_id'])) result = FaceLogin(data['username'], data['car_id']) socket_utils.sendJson(conn, result) #Logout elif (data['type'] == 'logout'): print("Logout - {} [Car ID({})]".format( data['username'], data['car_id'])) result = Logout(data['username'], data['car_id'], data['bId']) if "success" in result: print("Succesfully Logged out") socket_utils.sendJson( conn, { "success": True, "type": "{}".format(data['type']), "msg": "Logged out User {}".format( data['username']) }) if "error" in result: print("Failed to Logged out") socket_utils.sendJson( conn, { "error": True, "type": "{}".format(data['type']), "msg": "No booking for User {}".format( data['username']) }) #Car login - when car comes online elif (data['type'] == 'carReg'): print( "Car ID({}) Online Request\nLocation: Lat - {} Lng - {}" .format(data['car_id'], data['lat'], data['lng'])) result = updateLocation(data['car_id'], data['lat'], data['lng']) if "success" in result: print("Success - Car ID({}) online".format( data['car_id'])) socket_utils.sendJson( conn, { "success": True, "type": "{}".format(data['type']), "msg": "Car ID({}) is online".format( data['car_id']) }) if "error" in result: print("Error - Car ID({}) location update failed". format(data['car_id'])) socket_utils.sendJson( conn, { "error": True, "type": "{}".format(data['type']), "msg": "Car ID({}) Location update failed".format( data['car_id']) }) #Car location update elif (data['type'] == 'carLoc'): print( "Car ID({}) Location Update Request\nLocation: Lat - {} Lng - {}" .format(data['car_id'], data['lat'], data['lng'])) result = updateLocation(data['car_id'], data['lat'], data['lng']) if "success" in result: print( "Success - Car ID({}) Location updated".format( data['car_id'])) socket_utils.sendJson( conn, { "success": True, "type": "{}".format(data['type']), "msg": "Car ID({}) location updated".format( data['car_id']) }) if "error" in result: print("Error - Car ID({}) location update failed". format(data['car_id'])) socket_utils.sendJson( conn, { "error": True, "type": "{}".format(data['type']), "msg": "Car ID({}) Location update failed".format( data['car_id']) }) #Car Logout - when car goes offline elif (data['type'] == 'carOff'): print( "Car ID({}) Offline Request\nLocation: Lat - {} Lng - {}" .format(data['car_id'], data['lat'], data['lng'])) result = updateLocation(data['car_id'], data['lat'], data['lng']) if "success" in result: print("Success - Car ID({}) Offline".format( data['car_id'])) socket_utils.sendJson( conn, { "success": True, "type": "{}".format(data['type']), "msg": "Car ID({}) is offline".format( data['car_id']) }) if "error" in result: print("Error - Car ID({}) location update failed". format(data['car_id'])) socket_utils.sendJson( conn, { "error": True, "type": "{}".format(data['type']), "msg": "Car ID({}) Location update failed".format( data['car_id']) }) else: print("Type Error: {}".format(data['type'])) socket_utils.sendJson( conn, { "error": True, "type": "{}".format(data['type']), "msg": "Invalid Type value - {}".format(data['type']) }) else: print("No type attribute") socket_utils.sendJson( conn, { "error": True, "type": "Missing", "msg": "Type Attribute missing" })