Esempio n. 1
0
        # 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
    })
Esempio n. 2
0
            (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")
        # Accept connections, read incoming data, and call
        # 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)
            print request
            (theApp, rest) = self.select(request)
            parsedRequest = theApp.parse(request, rest)
            (returnCode, htmlAnswer) = theApp.process(parsedRequest)
            print 'Answering back...'
            recvSocket.send("HTTP/1.1 " + returnCode + " \r\n\r\n"
                            + htmlAnswer + "\r\n")
            recvSocket.close()

if __name__ == "__main__":
    anApp = app()
    otherApp = app()
    sumaApp = suma.suma()
    holaApp = hola.hola()
    adiosApp = hola.adios()
    aleatApp = aleat.aleatorio()
    testWebApp = webApp(socket.gethostname(), 1234, {'/app': anApp,
                                                      '/other': otherApp,
                                                      '/hola': holaApp,
                                                      '/adios': adiosApp,
                                                      '/suma': sumaApp,
                                                      '/aleat': aleatApp})