Guest User

Untitled

a guest
May 27th, 2011
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. # -*- test-case-name: twisted.fats.examples.test.test_hello_agi -*-
  2. # Copyright (c) 2006-2008 Alexander Burtsev
  3. # See LICENSE for details
  4.  
  5. """Hello FastAGI application.
  6.  
  7. API Stability: unstable
  8.  
  9. @author: U{Alexander Burtsev<mailto:[email protected]>}
  10.  
  11. $Id: hello_agi.py 25 2008-02-18 15:34:56Z burus $
  12. """
  13. from zope.interface import implements, Interface
  14. from twisted.python import log, components
  15. from twisted.application import internet, service
  16. from twisted.internet.defer import inlineCallbacks, returnValue
  17. from twisted.fats.service import FastAGIFactory, IFastAGIFactory, CallHandler
  18.  
  19.  
  20. class HelloCallHandler(CallHandler):
  21.     """My first call handler.
  22.    """
  23.     def startCall(self):
  24.         log.msg('Hello FastAGI logging system.')
  25.         # Answer the call.
  26.         df = self.agi.answer()
  27.        
  28.         # Say number and stop call session
  29.         df.addCallback(lambda _: self.agi.sayNumber(666))
  30.  
  31.         return df
  32.            
  33.  
  34. class IHelloFastAGIService(Interface):
  35.     """Example service interface
  36.    """
  37.     def my_method(param):
  38.         """Example method
  39.  
  40.        @param param: parameter
  41.        """
  42.  
  43.  
  44. class ExampleService:
  45.     implements(IHelloFastAGIService)
  46.  
  47.  
  48. class HelloFastAGIFactoryFromService(FastAGIFactory):
  49.     """My factory from service.
  50.    Implement service method and use them in the factory if it's
  51.    required.
  52.    """
  53.     implements(IFastAGIFactory)
  54.     handler = HelloCallHandler
  55.  
  56.     def __init__(self, service):
  57.         self.service = service
  58.  
  59.     def my_method(self, param):
  60.         """Adapt method from the service.
  61.        """
  62.         return self.service.my_method(param)
  63.    
  64. components.registerAdapter(HelloFastAGIFactoryFromService,
  65.                            IHelloFastAGIService,
  66.                            IFastAGIFactory)
  67.  
  68. # create application and TCP server with factory.
  69. PORT = 9000
  70. factory = HelloFastAGIFactoryFromService(ExampleService)
  71.    
  72. application = service.Application("HelloFastAGI")
  73. fastagi_service = internet.TCPServer(PORT, factory)
  74. fastagi_service.setServiceParent(application)
Advertisement
Add Comment
Please, Sign In to add comment