Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Lintel Technologies: How Implement Multiservice in Twisted.

$
0
0

Multiservice module is service collection provided by twisted, which is useful for creating a new service and combines with two or more existing services.

The major tools that manages Twisted application is a command line utility called twistd. twistd is a cross-platform, and is the recommended tool for running twisted applications.

The core component of the Twisted Application infrastructure is the

twisted.application.service.Application

object. which represents your application. Application acts as a container of any “Services” that your application provides. This will be done through Services.

Services manages application that can be started and stopped. In Application object can contain many services, or can even hierarchies of Services using “Multiservice” or your own custom IServiceCollection implementations.

Multiservice Implementaion:

To use multiserivce, which implements IService. For this, import internet and service module.

from twisted.application import internet, service

 

Example :

from twisted.application import internet,service
from serviceone import getServiceOne
from servicesecond import getServiceSecond
from twisted.web.server import Site

agentService = service.MultiService()
agentService.addService(getServiceOne())
agentService.addService(getServiceSecond())
application = service.Application("Receive Request")
agentService.setServiceParent(application)

To run, Save above code in a file as serviceexample.tac . Here, “tac ” file is regular python file. Twisted application infrastructure, protocol implementations live in a module, services, using those protocols are registered in a Twisted Application Configuration(TAC) file and the reactor and configuration are managed by an external utility.

Here, I use multiservice functionality from service. agentservice create object of multiservice. Then add services using add service method. In service, you can add web servers, FTP servers and SSH clients. After this, set application name and pass application to serviceparent method.

now, add service on port 8082 as :

from twisted.web.server import Site, NOT_DONE_YET
from twisted.web.resource import Resource
import logging
from twisted.application import internet

log = logging.getLogger("State Home")

class StateHome(Resource):
    isLeaf = True


    def reqComplete(self, req):
        req.write("Successfully Called.")
        req.finish()

    def render_GET(self, request):
        log.info("Run Render Get Method.")
        self.reqComplete(request)
        return NOT_DONE_YET

def getServiceOne():
    root = StateHome()
    factory = Site(root)
    return internet.TCPServer(8082, factory)

add another service same as above on port 8083 as:

from twisted.web.server import Site, NOT_DONE_YET
from twisted.web.resource import Resource
import logging
from twisted.application import internet

log = logging.getLogger("State Home")

class StateHome(Resource):
    isLeaf = True


    def reqComplete(self, req):
        req.write("Successfully Called second service.")
        req.finish()

    def render_GET(self, request):
        log.info("Run Render Get Method.")
        self.reqComplete(request)
        return NOT_DONE_YET

def getServiceSecond():
    root = StateHome()
    factory = Site(root)
    return internet.TCPServer(8083, factory)

To run serviceexample.tac file using twistd program, use command twistd -y serviceexample.tac -n. After this, open browser and enter url localhost:8082 and localhost:8083. You can see result on web page and both TCP servers are active.

The post How Implement Multiservice in Twisted. appeared first on Lintel Technologies Blog.


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>