(recvSocket, address) = mySocket.accept() print('HTTP request received (going to parse and process):') request = recvSocket.recv(2048).decode('utf-8') print(request) (theApp, rest) = self.select(request) parsedRequest = theApp.parse(request, rest) (returnCode, htmlAnswer) = theApp.process(parsedRequest) print('Answering back...') recvSocket.send(bytes("HTTP/1.1 " + returnCode + " \r\n\r\n" + htmlAnswer + "\r\n", 'utf-8')) recvSocket.close() # Al utilizar los metodos de esta clase en otras y esas a su vez importarlas en la clase padre tengo que poner los imports # aqui para que no haya error ya que utiliza metodos que estan arriba import hola import suma import aleat if __name__ == "__main__": holaApp = hola.hola() adiosApp = hola.adios() sumaApp = suma.suma() aleatApp = aleat.aleat() try: testWebApp = webApp("localhost", 1234, {'/hola': holaApp, '/adios': adiosApp, '/suma': sumaApp, '/aleat': aleatApp}) except KeyboardInterrupt: print ("\nClosing binded socket")
(recvSocket, address) = mySocket.accept() print 'HTTP request received (going to parse and process):' request = recvSocket.recv(2048) print request (theApp, rest) = self.select(request) parsedRequest = theApp.parse(request, rest) try: (returnCode, htmlAnswer) = theApp.process(parsedRequest) except TypeError: (returnCode, htmlAnswer) = app().process(parsedRequest) print 'Answering back...' recvSocket.send("HTTP/1.1 " + returnCode + " \r\n\r\n" + htmlAnswer + "\r\n") recvSocket.close() if __name__ == "__main__": aleat = aleat() hola = holaApp() suma = sumador() anApp = app() testWebApp = webApp( "localhost", 1234, { '/app': anApp, '/suma': suma, '/hola': hola, '/adios': hola, '/aleat': aleat })
def process(self, parsedRequest): return ("200 OK", "<html><body><h1>HOLA</body></html>") class adios(webapp.app): def parse(self, request, rest): return None def process(self, parsedRequest): return ("200 OK", "<html><body><h1>ADIOS</body></html>") if __name__ == "__main__": hola = hola() adios = adios() aleat = aleat.aleat() suma = suma.suma() classapps = { "/hola": hola, "/resta": suma, "/adios": adios, "/aleat": aleat, "/suma": suma } try: testWebApp = webapp.webApp("localhost", 1234, classapps) except KeyboardInterrupt: print "Key board interrupt"
# parse and process methods (in a loop) while True: print('Waiting for connections') (recvSocket, address) = mySocket.accept() print('HTTP request received (going to parse and process):') request = recvSocket.recv(2048).decode('utf-8') print(request) (theApp, rest) = self.select(request) parsedRequest = theApp.parse(request, rest) (returnCode, htmlAnswer) = theApp.process(parsedRequest) print('Answering back...') recvSocket.send( bytes( "HTTP/1.1 " + returnCode + " \r\n\r\n" + htmlAnswer + "\r\n", 'utf-8')) recvSocket.close() if __name__ == "__main__": doAleat = aleat.aleat() doHola = hola.hola() doAdios = hola.adios() doSuma = suma.sumSimple() testWebApp = webApp("localhost", 1234, { '/aleat': doAleat, '/hola': doHola, '/adios': doAdios, '/suma': doSuma })