def get_response_pattern(): """ Get response data. """ responsePattern = {} try: responsePattern = json_file_to_pyData(pathRespPatternFile) if not responsePattern: responsePattern = {} TheLogger.debug("Current response pattern: \n" + \ json_dumps(responsePattern)) except Exception as e: TheLogger.error(str(e)) return responsePattern
def set_response_pattern(pathRespPatternFileIn, flagAddData): """ Set response data. """ try: responsePatternIn = json_file_to_pyData(pathRespPatternFileIn) TheLogger.debug("Input response pattern: \n" + \ json_dumps(responsePatternIn)) responsePattern = {} if flagAddData: responsePattern = get_response_pattern() responsePattern.update(**responsePatternIn) TheLogger.debug("Incomming response pattern: \n" + \ json_dumps(responsePattern)) pyData_to_json_file(responsePattern, pathRespPatternFile) except Exception as e: TheLogger.error(str(e))
def do_GET(self): """ Process GET requests. """ urlEntire = "http://" + self.client_address[0] + self.path urlPieces = urlparse(urlEntire) urlPath = urlPieces.path.strip("/") urlQueries = {} if urlPieces.query: urlQueries = dict( [x.split("=",1) for x in urlPieces.query.split("&")]) TheLogger.info("Recieved request: %s" % urlEntire) TheLogger.debug("Recieved request's urlpath : %s" % urlPath) TheLogger.debug("Recieved request's queries : %s" % urlQueries) if not urlPath: self.response_OK() return responsePattern = get_response_pattern() responsePattern = defaultdict(lambda: None, responsePattern) pattern = responsePattern[urlPath] if not pattern: self.response_404() return self.send_response(200) headers = { "Content-Length": len(pattern["respData"]), "Content-type": "text/plain" } if "headers" in pattern: headers.update(pattern["headers"]) for k, v in headers.items(): self.send_header(k, v) self.end_headers() self.wfile.write(pattern["respData"].encode())
def log_message(self, f, *args): """ Forbidden logging. """ TheLogger.debug("INNER: " + f % args)