def getExceptionsFile(module, assignment): global s try: if setSocket(): args = {'module': module, 'assignment': assignment} addLecturerAuthDetails(args) response = request( Request(s, FileServerCommands.GET_EXCEPTIONS_FILE, args)) if response is not None: if not response.disconnected: if response.success == "True": return response.data['exceptions'], False else: error = True doError(f"{response.message}") else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return {}, error
def alertMacAddress(lecturer, mac): global alertMacAddressStr, s try: if setSocket(): args = {} addLecturerAuthDetails(args) args['mac'] = mac response = request( Request(s, FileServerCommands.ALERT_MAC_ADDRESS, args)) if response is not None: if not response.disconnected: if response.success == "True": data = response.data if 'alertMacAddress' in data: alertMacAddressStr = data['alertMacAddress'] alert = False if 'alert' in data: alert = data['alert'] alert = alert.upper() if alert == "TRUE": alert = True else: alert = False return alert, False else: error = True doError( f"A server error occurred checking if MAC address should be alerted: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return False, error
def checkAssignmentExists(module, academic_year, assignment): global s try: if setSocket(): args = { FileServerCommands.CheckExistsRequestCodes.CODE: FileServerCommands.CheckExistsRequestCodes.ASSIGNMENT_EXISTS, 'module': module, 'academic_year': academic_year, 'assignment': assignment } response = request( Request(s, FileServerCommands.CHECK_EXISTS, args)) if response is not None: if not response.disconnected: if response.success == "True": exists = response.data['exists'] if exists == "True": exists = True else: exists = False return exists, False else: error = True doError( f"A server error occurred checking if assignment exists: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return False, error
def serveRequest(request: Request, addr): while True: try: request.receive() if not request.disconnected and not request.http_requested: command = request.command if not command: logging.error( "No command provided to server, sending error response" ) respond(request, False, "A command needs to be provided to the server") else: if command in FileServerCommands.VALID_COMMANDS: processCommand(request, command) else: logging.error( f"Command {command} is not valid, sending error response" ) respond( request, False, f"The command provided: {command} is not valid. Expected: {FileServerCommands.VALID_COMMANDS}" ) elif request.http_requested: logging.info( "Server was accessed with a HTTP request. Information HTML page sent to browser" ) break else: logging.debug(f"{addr} disconnected") break except Exception as e: if not isinstance(e, BrokenPipeError): e = format_exc() logging.error(e) respond(request, False, f"An exception occurred: {e}") break
def checkCredentials(lect, passw): global s, lecturer, password try: if setSocket(): args = {'lecturer': lect, 'password': passw} response = request( Request(s, FileServerCommands.AUTHENTICATE_LECTURER, args)) if response is not None: if not response.disconnected: if response.success == "True": if response.message == "AUTHENTICATED": lecturer = lect password = passw return True, False elif response.message == "NOT_AUTHENTICATED": return False, False # Return false for login_error parameter as this failure is a credential error, not socket error else: login_error = True doError( f"A server error occurred checking credentials: {response.message}" ) else: login_error = True doError( f"A server error occurred checking credentials: {response.message}" ) else: s = None login_error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None login_error = True logging.error(f"Request Error") else: login_error = True except MessagingError as m: s = None doError(f"{m}") login_error = True return False, login_error
def get_all_test_items(module_code, assignment) -> tuple[list, bool]: global s try: if setSocket(): args = {} addLecturerAuthDetails(args) args[ FileServerCommands.ModuleInfoRequestCodes. CODE] = FileServerCommands.ModuleInfoRequestCodes.MODULE_TEST_ITEMS args['module_code'] = module_code args['assignment'] = assignment response = request(Request(s, FileServerCommands.MODULE_INFO, args)) if response is not None: if not response.disconnected: if response.success == "True": return response.data['test_items'], False else: error = True doError( f"A server error occurred retrieving test items: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return [], error
def getLecturerModules(lecturer): global s try: if setSocket(): args = {} addLecturerAuthDetails(args) response = request( Request(s, FileServerCommands.GET_LECTURER_MODULES, args)) if response is not None: if not response.disconnected: if response.success == "True": modules = [] for value in response.data['modules']: modules.append(value) return modules, False else: error = True doError( f"A server error occurred retrieving lecturer modules: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return {}, error
def updateExceptionsFile(module, assignment, student_id, exceptions): global s try: if setSocket(): args = { 'module': module, 'assignment': assignment, 'student_id': student_id, 'exceptions': exceptions } addLecturerAuthDetails(args) response = request( Request(s, FileServerCommands.UPDATE_EXCEPTIONS_FILE, args)) if response is not None: if not response.disconnected: if response.success == "True": return False else: error = True doError(f"{response.message}") else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return error
def updateDefinitionsFile(definitions_file, definitions): global s try: if setSocket(): args = { 'definitions_file': definitions_file, 'definitions': definitions } response = request( Request(s, FileServerCommands.UPDATE_DEFINITIONS_FILE, args)) if response is not None: if not response.disconnected: if response.success == "True": return False else: error = True doError( f"A server error occurred updating definitions file: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return error
def trustMacAddress(lecturer, trust): global s try: if setSocket(): args = {} addLecturerAuthDetails(args) args['mac'] = alertMacAddressStr args['trust'] = trust response = request( Request(s, FileServerCommands.TRUST_MAC_ADDRESS, args)) if response is not None: if not response.disconnected: if response.success == "True": return False else: error = True doError( f"A server error occurred trusting MAC address: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return error
def get_vars(module, week_number, student_id): global s try: if setSocket(): args = { 'module': module, 'week_number': week_number, 'student_id': student_id } response = request(Request(s, FileServerCommands.GET_VARS, args)) if response is not None: if not response.disconnected: if response.success == "True": return response.data['vars'], False else: error = True doError( f"A server error occurred retrieving vars: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return {}, error
def cloneAssignment(module, assignment, content): global s try: if setSocket(): args = { 'module': module, 'assignment': assignment, 'content': content } response = request( Request(s, FileServerCommands.CLONE_ASSIGNMENT, args)) if response is not None: if not response.disconnected: if response.success == "True": return response.data, False else: error = True doError(f"Error: {response.message}") else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return "", error
def createAssignmentDirectory(module, assignment_name): global s try: if setSocket(): args = {'module': module, 'assignment': assignment_name} response = request( Request(s, FileServerCommands.CREATE_ASSIGNMENT_DIRECTORY, args)) if response is not None: if not response.disconnected: if response.success == "True": return response.data['params_path'], False else: error = True doError( f"A server error occurred checking if assignment exists: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return "", error
def uploadFile(file, destination_path): global s try: if setSocket(): contents, error = readFileContents(file) if not error: args = {'contents': contents, 'destination': destination_path} response = request( Request(s, FileServerCommands.UPLOAD_FILE, args)) if response is not None: if not response.disconnected: if response.success == "True": return False else: error = True doError(f"{response.message}") else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return error
def generateAssignmentCSV(module, assignment='all') -> bool: global s try: error = False if setSocket(): args = { 'module': module, 'assignment': assignment # for now, we only support all } response = request( Request(s, FileServerCommands.GENERATE_ASSIGNMENT_CSV, args)) if response is not None: if not response.disconnected: if response.success == "True": return False else: error = True doError(f"{response.message}") else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") except MessagingError as m: s = None doError(f"{m}") error = True return error
def getParams(module, assignment): global s try: if setSocket(): args = {'module': module, 'assignment': assignment} response = request(Request(s, FileServerCommands.GET_PARAMS, args)) if response is not None: if not response.disconnected: if response.success == "True": return response.data['content'], response.data[ 'filename'], False else: error = True doError( f"A server error occurred retrieving params: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return "", "", error
def getDefinitions(module, academic_year): global s try: if setSocket(): args = {'module': module, 'academic_year': academic_year} response = request( Request(s, FileServerCommands.GET_DEFINITIONS_FILE, args)) if response is not None: if not response.disconnected: if response.success == "True": return response.data['definitions'], False else: error = True doError( f"A server error occurred retrieving definitions: {response.message}" ) else: s = None error = True if response.error: logging.error( f"Response Error: {response.error_message}") else: s = None error = True logging.error(f"Request Error") else: error = True except MessagingError as m: s = None doError(f"{m}") error = True return {}, error
logging.error(e) respond(request, False, f"An exception occurred: {e}") break if __name__ == "__main__": logging.info( f"Starting handin_file_server on host {const.FILE_SERVER_HOST} and port {const.FILE_SERVER_PORT}" ) print( f"Starting handin_file_server on host {const.FILE_SERVER_HOST} and port {const.FILE_SERVER_PORT}" ) load_file_server_commands() signal.signal(signal.SIGINT, signal_handler) s = listenerSocket(const.FILE_ADDR) while True: # continuously loop through accepting any new connections that arrive conn, addr = acceptSocket(s) log_string = f"New connection to handin_file_server: {addr}" print(log_string) logging.info(log_string) request = Request( conn ) # create the request object to parse the request made to the server t = threading.Thread(target=serveRequest, args=(request, addr)) t.daemon = True t.start() s.close()