#!/usr/bin/python import webappmulti import socket usuario = socket.gethostname() class App(webappmulti.app): def parse(self, request, rest): paquete = request.split()[1][1:] (a) = paquete.split('/')[0] return a def process(self, parsedRequest): return ("200 OK", "<html><body><h1>" + parsedRequest + "</h1></body></html>") if __name__ == "__main__": HolaApp = App() MiApp = webappmulti.webApp(usuario, 1234, {'/Hola': HolaApp, '/Adios': HolaApp})
return '-' class mul (operation): def operate (self, oper1, oper2): return oper1 * oper2 def sign (self): return '*' class div (operation): #heredad de la clase operacion que implementa todo lo que tienen comun las 4 operaciones def operate (self, oper1, oper2): return oper1 / oper2 def sign (self): return '/' if __name__ == "__main__": addObj = add() subObj = sub() mulObj = mul() divObj = div() multiCalc = webappmulti.webApp ("localhost", 1234, {'/add': addObj, #ponemos las miniapps en esas URL '/sub': subObj, '/mul': mulObj, '/div': divObj,})
return oper1 * oper2 def sign(self): return '*' class div(operation): def operate(self, oper1, oper2): return oper1 / oper2 def sign(self): return '/' if __name__ == "__main__": addObj = add() subObj = sub() mulObj = mul() divObj = div() #si ejecuto esto, se crea una clase de cada tipo, y la clase multi. que es llamada desde webappmulti multiCalc = webappmulti.webApp("localhost", 1234, { '/add': addObj, '/sub': subObj, '/mul': mulObj, '/div': divObj, })
#!/usr/bin/python import webappmulti import socket import random import hola import suma import aleat usuario = socket.gethostname() if __name__ == "__main__": SumaApp = suma.App() AleatApp = aleat.App() HolaApp = hola.App() AdiosApp = hola.App() MiApp = webappmulti.webApp(usuario, 1234, { '/suma': SumaApp, '/aleat': AleatApp, '/hola': HolaApp, '/adios': HolaApp })
return '-' class mul (operation): def operate (self, oper1, oper2): return oper1 * oper2 def sign (self): return '*' class div (operation): def operate (self, oper1, oper2): return oper1 / oper2 def sign (self): return '/' if __name__ == "__main__": addObj = add() subObj = sub() mulObj = mul() divObj = div() multiCalc = webappmulti.webApp ("localhost", 1234, {'/add': addObj, '/sub': subObj, '/mul': mulObj, '/div': divObj,})
if verb == "POST": params = urlparse.parse_qs(rest[1:]) print params try: oper = params['type'][0] resources['/' + str(self.calcno)] = classes[oper]() self.calcno += 1 success = True except: success = False (error, message) = ("400 Bad Request", simplecalc.decorateHTML("400 Bad Request: Error in creation of calculator")) else: success = False (error, message) = ("405 Method Not Allowed", "405 Method Not Allowed: HTTP method " + verb + " not supported") if success: return ("200 OK", simplecalc.decorateHTML(str(resources))) else: return (error, simplecalc.decorateHTML(message)) def __init__ (self): self.calcno = 0 if __name__ == "__main__": createObj = create() resources['/'] = createObj multiCalc = webappmulti.webApp ("localhost", 1234, resources)
print params try: oper = params['type'][0] resources['/' + str(self.calcno)] = classes[oper]() self.calcno += 1 success = True except: success = False (error, message) = ( "400 Bad Request", simplecalc.decorateHTML( "400 Bad Request: Error in creation of calculator")) else: success = False (error, message) = ("405 Method Not Allowed", "405 Method Not Allowed: HTTP method " + verb + " not supported") if success: return ("200 OK", simplecalc.decorateHTML(str(resources))) else: return (error, simplecalc.decorateHTML(message)) def __init__(self): self.calcno = 0 if __name__ == "__main__": createObj = create() resources['/'] = createObj multiCalc = webappmulti.webApp("localhost", 1234, resources)
#!/usr/bin/python import webappmulti import socket import random usuario = socket.gethostname() class App(webappmulti.app): def process(self, parsedRequest): aleatorio = str(int(random.random()*1000000)) return ("200 OK", "<html><body><h1>" + aleatorio + "</h1></body></html>") if __name__ == "__main__": AleatApp = App() MiApp = webappmulti.webApp(usuario, 1234, {'/aleat': AleatApp})
#!/usr/bin/python3 import webappmulti import socket import hola import suma import aleat if __name__ == "__main__": holaApp = hola.hola() adiosApp = hola.adios() sumaApp = suma.suma() aleatApp = aleat.aleat() testWebApp = webappmulti.webApp(socket.gethostname(), 1231, { '/hola': holaApp, '/adios': adiosApp, '/suma': sumaApp, '/aleat': aleatApp })
def process(self, parsedRequest): try: if self.status == 0: self.sum1 = int(parsedRequest) msg = "Recibido primer operando: " + str(self.sum1) self.status = 1 else: self.sum2 = int(parsedRequest) op = self.sum1 + self.sum2 msg = "Resultado de la suma: " + str(self.sum1) + ' + ' + str( self.sum2) + ' = ' + str(op) self.status = 0 except ValueError: msg = "La URL debe entregar un numero al final. Ejemplo: localhost:1234/suma/3" return ("200 OK", "<html><body><h1>" + msg + "</h1></body></html>") if __name__ == "__main__": hola = holaApp() adios = adiosApp() suma = sumaApp() aleat = aleatApp() testWebApp = webappmulti.webApp("localhost", 1234, { '/hola': hola, '/adios': adios, '/suma': suma, '/aleat': aleat })
except: success = False (error, message) = ("400 Error", "Error in parameters for operation") elif verb == 'GET': success = True else: success = False (error, message) = ("400 Error", "HTTP verb " + verb + " not supported") if success: return ("200 OK", decorateHTML(str(self.oper1) + self.sign(self.ope) + str(self.oper2) + "=" + str(self.result))) else: return (error, decorateHTML(message)) def __init__ (self): self.oper1 = 0 self.oper2 = 0 self.ope = "suma" self.result = 0 if __name__ == "__main__": calcObj = calculator() multiCalc = webappmulti.webApp ("localhost", 1234, {'/calculator': calcObj,})