def get_file(file_destination): if file_destination in _file_cache: return _file_cache[file_destination] if log.is_debugging(): log.debug("File " + file_destination + " has not been found within cache.") return None
def cache_file(file_destination): global _file_cache if os.path.isdir(file_destination): raise Exception("Destination is not a path: " + file_destination) file = open(file_destination, 'rb') cached_file_name = "." + \ file_destination[len(_root_dir):].replace("\\", "/") if log.is_debugging(): log.debug("Caching file: " + cached_file_name) _file_cache[cached_file_name] = file.read()
def run_server(): global SERVER_PORT display_messages = True log.info('Server is running on port ' + str(SERVER_PORT)) log.info('Try accessing server at 127.0.0.1:' + str(SERVER_PORT) + '. It will serve items faster than localhost') log.set_log_level("trace") file_cache.precache("\\static", recursive=True) mini.server_port(SERVER_PORT) log.warn("This is a warning") log.error("This is an error") log.info("This is an info") log.debug("This is a debug message") log.trace("This is a trace message") mini.start()
def parse(req): decoded_req = req.decode() req_param_map = dict() req_param_map['headers'] = dict() body = None method = None if '\r\n\r\n' in decoded_req: body = decoded_req.split('\r\n\r\n')[1] log.debug(decoded_req) for line in decoded_req.splitlines(): if line == '\r': break param = line.split(' ')[0] if param and len(param) > 0 and param[-1] == ':': param = param[:-1] value = ' '.join(line.split(' ')[1:]) if param in HTTP_METHODS: method = param value = value.split(' ')[0] if '?' in value: query_params = value.split('?')[1:] req_param_map['query_params'] = form_query_params(query_params) value = value.split('?')[0] req_param_map[param] = value continue if '\r' in value: value = value.replace('\r', '') log.warn(value) req_param_map['headers'][param] = value log.trace('Request data: ' + str(req_param_map)) return req_param_map, body, method
def server_port(port_number): global port if log.is_debugging(): log.debug("Server port has been configured to: " + str(port_number)) port = port_number