def start(serverHost, serverPort): """ Start HTTP server. """ if not serverHost or not serverPort: TheLogger.error("Set server host and port.") exit(10) if not is_port_idle(serverPort): TheLogger.error("Port {} is in use.".format(serverPort)) exit(11) TheLogger.info("Start HTTP server with %s:%s..." % (serverHost, serverPort)) httpServer = HttpServer(serverHost, serverPort) httpServer.start()
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())