0

So protocols allow asynchronous functionality but only on the TCP level.

from twisted.internet import reactor, protocol, endpoints

class UpperProtocol( protocol.Protocol ):
    def connectionMade( self ):
        self.transport.write( "Convet to Uppercase !\n" )

    def connectionLost( self, reason ):
        pass

    def dataReceived( self, data ):
        self.transport.write( data.upper() )
        self.transport.loseConnection()


factory = protocol.ServerFactory()
factory.protocol = UpperProtocol

#reactor.listenTCP( 8000, factory )
endpoints.serverFromString( reactor, "tcp:8000" ).listen( factory )
reactor.run()

I can do a POST request with this:

from twisted.internet import reactor, endpoints
from twisted.web.resource import Resource
from twisted.web.server import Site
import intent

import cgi

class UpperCaller(Resource):
    def render_POST(self, request):
        text            = request.args[ "text" ][ 0 ]
        response        = text.upper()
        return cgi.escape( response )

root = Resource()
root.putChild( "upper", UpperCaller() )
factory  = Site( root )
endpoint = endpoints.TCP4ServerEndpoint( reactor, 8880 )
endpoint.listen( factory )
reactor.run()

How can I call the protocol to be used whenever a post request arrives ? If done via the POST server it will not be asynchronous like the Protocol call from Telnet would be.

How can I made it work via a POST call ? I would like to let it be an API call where I pass text and it returns the output by calling the protocol.

Meghdeep Ray
  • 5,262
  • 4
  • 34
  • 58

0 Answers0