def setProcParameter(self, parameter): #parameter is something like this 'status=OFF' basicPath = pathlib.Path.cwd() filePath = pathlib.Path(basicPath, 'config.txt') f = io.readFile(filePath) defParameter = parameter.split('=')[0] v = f.split(defParameter) before = v[0] after = v[1].split('\n', 1)[1] middle = parameter + '\n' s = before + middle + after io.writeFile(filePath, s)
def sendHomepage(self): page = io.readFile(os.curdir + '/index.html') pageLength = len(page) # s is the response message s = sm.setCode('HTTP/1.1', 200) s = sm.setMessageAnswer(s, 'OK') s = sm.setContentLength(s, pageLength) s = sm.setContentType(s, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + page io.writeMessage(self.csocket, s)
def manageFrontendRequest(self, msgFromClient): path = self.getPath(msgFromClient) contentType = 'text/' + path.split('.')[-1] code = io.readFile(os.curdir + path) pageLength = len(code) # s is the response message s = sm.setCode('HTTP/1.1', 200) s = sm.setMessageAnswer(s, 'OK') s = sm.setContentLength(s, pageLength) s = sm.setContentType(s, contentType) s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + code io.writeMessage(self.csocket, s) io.closeConnection(self.csocket)
def sendNotExistingPath(self): html_page = io.readFile(os.path.curdir + '/error.html') newContent = '''<body>\n<div id="msg_instruction"> 404 Not Found </div> <div id="details"> The path specified in the HTTP request does not exist. </div>''' v = html_page.split('<body>') updatedHTML = v[0] + newContent + v[1] page_length = len(updatedHTML) s = sm.setCode('HTTP/1.1', 404) s = sm.setMessageAnswer(s, 'Not Found') s = sm.setContentLength(s, page_length) s = sm.setContentType(s, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + updatedHTML io.writeMessage(self.csocket, s)
def sendMethodNotAllowed(self): html_page = io.readFile(os.path.curdir + '/error.html') newContent = '''<body>\n<div id="msg_instruction"> 405 Method Not Allowed </div> <div id="details"> The method specified in the HTTP requested is not allowed. the only method supported is GET </div>''' v = html_page.split('<body>') updatedHTML = v[0] + newContent + v[1] page_length = len(updatedHTML) s = sm.setCode('HTTP/1.1', 405) s = sm.setMessageAnswer(s, 'Method Not Allowed') s = sm.setContentLength(s, page_length) s = sm.setContentType(s, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + updatedHTML io.writeMessage(self.csocket, s)
def sendCommunicationError(self, exc): html_page = io.readFile(os.path.curdir + '/error.html') newContent = '''<body>\n<div id="msg_instruction"> 409 Conflict </div> <div id="details"> The gateway server cannot be reached properly. Try again later.<br></div>''' newContent = newContent + f'<div>Error detected: {exc}</div>' v = html_page.split('<body>') updatedHTML = v[0] + newContent + v[1] page_length = len(updatedHTML) s = sm.setCode('HTTP/1.1', 409) s = sm.setMessageAnswer(s, 'Conflict') s = sm.setContentLength(s, page_length) s = sm.setContentType(s, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + updatedHTML io.writeMessage(self.csocket, s) io.closeConnection(self.csocket)
def sendBadRequest(self): html_page = io.readFile(os.path.curdir + '/error.html') newContent = '''<body>\n<div id="msg_instruction"> 400 Bad Request </div> <div id="details"> The server cannot process your request. If it's an auth request, check that all the parameters:<br> name, cardNumber, cvv, expDate and amount are present in the url. </div>''' v = html_page.split('<body>') updatedHTML = v[0] + newContent + v[1] page_length = len(updatedHTML) s = sm.setCode('HTTP/1.1', 400) s = sm.setMessageAnswer(s, 'Bad Request') s = sm.setContentLength(s, page_length) s = sm.setContentType(s, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + updatedHTML io.writeMessage(self.csocket, s) io.closeConnection(self.csocket)
def getProcParameters(self): basicPath = pathlib.Path.cwd() filePath = pathlib.Path(basicPath, 'config.txt') f = io.readFile(filePath) return f