import socket class holaApp(webapp.app): def process(self, parsedRequest,request): """Process the relevant elements of the request. Returns the HTTP code for the reply, and an HTML page. """ return ("200 OK", "<html><body><h1>" + "Hola a todos" + "</h1></body></html>") class adiosApp(webapp.app): def process(self, parsedRequest,request): """Process the relevant elements of the request. Returns the HTTP code for the reply, and an HTML page. """ return ("200 OK", "<html><body><h1>" + "Adios a todos" + "</h1></body></html>") if __name__ == "__main__": hola = holaApp() adios = adiosApp() port = 1234 testWebApp = webapp.webApp("localhost", port, {'/hola': hola, '/adios': adios})
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"
#!/usr/bin/python3 import webapp as WA # Defino unaclase de tipo webApp: mi servidor MyServer = WA.webApp("localhost", 8080)
rest: rest of the resource name after stripping the prefix """ return rest def process(self, parsedReq, nums): """Process the relevant elements of the request. Returns the HTTP code for the reply, and an HTML page. """ try: nums.append(int(parsedReq)) except ValueError: return ("400 Bad Request", "<html><body><h1>No me has dado" + " un entero. Vete.</h1></body></html>") if len(nums) == 1: return ("200 OK", "<html><body><h1>Me has dado un " + str(nums[0]) + ". Dame mas.</h1></body></html>") else: result = nums[0] + nums[1] return ("200 OK", "<html><body><h1>Me habias dado un " + str(nums[0]) + ". Ahora un " + str(nums[1]) + ". Suman " + str(result) + "." + "</h1></body></html>") if __name__ == "__main__": unaSuma = suma() testSuma = webapp.webApp('localhost', 1234, {'/suma/': unaSuma})
return ("200 OK", "<html><body><h1>" + "Hola") class Adios(webapp.app): def parse(self, request, rest): return None def process(self, parsedRequest): # total = self.lista return ("200 OK", "<html><body><h1>" + "Adios") try: if __name__ == "__main__": SumaApp = suma.Suma() HolaApp = hola.Hola() AdiosApp = hola.Adios() AleatApp = aleat.Aleat() testWebApp = webapp.webApp( "localhost", 1234, { '/suma': SumaApp, '/hola': HolaApp, '/adios': AdiosApp, '/aleat': AleatApp }) except TypeError: sys.exit()
#!/usr/bin/python3 """ Miguel Ángel Lozano Montero. Programa que genera URLs aleatorias con un servidor orientado a objetos. """ import webapp import random class aleat(webapp.app): "Application that generates random URLs." def process(self, parsedReq): rand = random.randrange(1000000000) return ("200 OK", "<html><body><h1>Hola. <a href=http://localhost:" + "1234/aleat/" + str(rand) + ">Dame otra</a></h1></body></html>") if __name__ == "__main__": urlaleat = aleat() testAleat = webapp.webApp('localhost', 1234, {'/aleat/': urlaleat})
#!/usr/bin/python3 import webapp import socket class aleatApp(webapp.app): def process(self, parsedRequest, request): #parsedRequest es lo que ha salido de parse *1 """Process the relevant elements of the request. Returns the HTTP code for the reply, and an HTML page. """ import random numero = random.randint(1, 10000) return ("200 OK", "<html><body><a href=http://localhost:1234/aleat/" + str(numero) + ">Dame mas</a></body></html>" ) #Devuelve una lista con el codigo de estado y la pag web *2 if __name__ == "__main__": aleatoria = aleatApp() port = 1234 #la creo global para que en aleatoria no me de fallo testWebApp = webapp.webApp("localhost", port, {'/aleat': aleatoria})
#!/usr/bin/python import suma import hola import aleat import webapp if __name__ == "__main__": saludo = hola.saludoApp() sumar = suma.sumaApp() aleatorio = aleat.aleatApp() testWebApp = webapp.webApp("localhost", 1234, { '/hola': saludo, '/adios': saludo, '/suma': sumar, '/aleat': aleatorio })
splitted_req = request.split(' ') rest = str(splitted_req[1][1:]) parsedRequest = rest.split('/') return parsedRequest[1] def process(self, parsedRequest): if not parsedRequest: return ("404 Bad Request", "<h1>Go Away!</h1>") if self.first is None: try: self.first = int(parsedRequest) reply = ("El primero es " + str(self.first) + ". Dame otro") except ValueError: return None else: result = self.first + int(parsedRequest) reply = "El resultado es " + str(result) self.first = None return ("200 OK", "<html><body>" + reply + "</body></html>\r\n") if __name__ == "__main__": try: sumador = Sumador() webapp.webApp('localhost', 1234, {'/suma': sumador}) except KeyboardInterrupt: print "Closing service" sys.exit()
#!/usr/bin/python3 import webapp import socket class sumaApp(webapp.app): def process(self, parsedRequest, request): """Process the relevant elements of the request. Returns the HTTP code for the reply, and an HTML page. """ try: recurso = request.split()[1][1:] sumandos = recurso.split('/')[1] sumando1, sumando2 = sumandos.split('+') suma = int(sumando1) + int(sumando2) return ("200 OK", "<html><body><h1>" + "Me estas pidiendo: " + sumandos + ". Y la suma es: " + str(suma) + "</h1></body></html>") except ValueError: return ("404 Not found", "<html><body><h1>" + "Argumentos incorrectos" + "</h1></body></html>") if __name__ == "__main__": suma = sumaApp() port = 1234 testWebApp = webapp.webApp("localhost", port, {'/suma': suma})
import webapp import suma import hola import aleat if __name__ == "__main__": anApp = webapp.app() otherApp = webapp.app() hola = hola.hola() suma = suma.suma() aleatorio = aleat.aleatorio() testWebApp = webapp.webApp( "localhost", 1234, { '/app': anApp, '/other': otherApp, '/hola': hola, '/adios': hola, '/suma': suma, '/aleat': aleatorio })
recurso = int(parsedRequest) self.sumando.insert(self.cont, recurso) self.cont += 1 print("cont: " + str(self.cont)) except ValueError: return ("200 OK", "<html><body><h1>Suma</h1>" + "<p>Me has dado un " + recurso + ". Vete" + "</p></body></html>\r\n") if self.cont == 1: print("PPR: " + parsedRequest) return ("200 OK", "<html><body><h1>Calculadora</h1>" + "<p>Me has dado un " + str(self.sumando[self.cont - 1]) + ". Dame otro</p></body></html>\r\n") elif self.cont == 2: suma = self.sumando[0] + self.sumando[1] contad = self.cont self.cont = 0 return ("200 OK", "<html><body><h1>Calculadora</h1>" + "<p>Me habias dado un " + str(self.sumando[contad - 2]) + ". Ahora me has dado un " + str(self.sumando[contad - 1]) + ". La suma es " + str(suma) + "</p>" + "</body></html>\r\n") if __name__ == "__main__": sumaApp = suma() testSuma = webapp.webApp("localhost", 1234, {'/suma/': sumaApp})
"""Application that returns an HTML page with the text "Hola" when resources that start with /hola are invoked.""" def process(self, parsedReq): """Process the relevant elements of the request. Returns the HTTP code for the reply, and an HTML page. """ return ("200 OK", "<html><body><h1>Hola</h1></body></html>") class despedida(webapp.app): """Application that returns an HTML page with the text "Adios" when resources that start with /adios are invoked.""" def process(self, parsedReq): """Process the relevant elements of the request. Returns the HTTP code for the reply, and an HTML page. """ return ("200 OK", "<html><body><h1>Adios</h1></body></html>") if __name__ == "__main__": unHola = saludo() unAdios = despedida() testHola = webapp.webApp("localhost", 1234, {'/hola': unHola, '/adios': unAdios})
import webapp class aleat(webapp.app): def process(self, parsedRequest): import random numero = random.randint(1, 100000) return ("200 OK", "<html><body><a href=" + str(numero) + ">Dame mas</a>" + "</body></html>") if __name__ == "__main__": aleatApp = aleat() testAleat = webapp.webApp("localhost", 1234, {'/aleat': aleatApp})
#!/usr/bin/python # -*- coding: utf-8 -*- import webapp import random import sys class Aleat(webapp.app): def process(self, parsedRequest): rand_num = random.randint(0, 1000000000) return ("200 OK", "<html><body>Hola.<a href=" + "/aleat/" + str(rand_num) + "> Dame otra</a></body></html>\r\n") if __name__ == "__main__": try: aleat = Aleat() testWebApp = webapp.webApp("localhost", 1234, {'/aleat': aleat}) except KeyboardInterrupt: print "Closing service" sys.exit()
#!/usr/bin/python # -*- coding: utf-8 -*- import webapp import aleat import suma class holaApp(webapp.app): def process(self, parsed): return ("200 OK", "<html><body><p>Hola</p></body></html>") class adiosApp(webapp.app): def process(self, parsed): return ("220 OK", "<html><body><p>Adios</p></body></html>") if __name__ == "__main__": hola = holaApp() adios = adiosApp() aleat = aleat.aleat() suma = suma.mySumApp() apps = {"/hola": hola, "/adios": adios, "/aleat": aleat, "/suma": suma} try: testWebApp = webapp.webApp("localhost", 9999, apps) except KeyboardInterrupt: print "Key board interrupt detected."
import sumador import aleat import sys class adios(webapp.app): def process(self, parsedRequest): return ("200 OK", "<html><body>Adios</body></html>\r\n") class hola(webapp.app): def process(self, parsedRequest): return ("200 OK", "<html><body>Hola</body></html>\r\n") if __name__ == "__main__": try: hola = hola() adios = adios() sumador = sumador.Sumador() aleat = aleat.Aleat() testWebApp = webapp.webApp("localhost", 1234, { '/hola': hola, '/adios': adios, '/suma': sumador, '/aleat': aleat }) except KeyboardInterrupt: print "Closing service" sys.exit()
import webapp class hola(webapp.app): def process(self, parsedRequest): return ("200 OK", "<html><body><h1>Hola</h1></body></html>") class adios(webapp.app): def process(self, parsedRequest): return ("200 OK", "<html><body><h1>Adios</h1></body></html>") if __name__ == "__main__": holaApp = hola() adiosApp = adios() testHola = webapp.webApp("localhost", 1234, { '/hola': holaApp, '/adios': adiosApp })