[Twisted-Python] Using service.Application
Itamar Shtull-Trauring
itamar at itamarst.org
Mon Dec 15 12:16:57 EST 2003
On Mon, 2003-12-15 at 05:14, Richard.Townsend at edl.uk.eds.com wrote:
> I had the following demo script for Twisted 1.0.6:
>
> #-----------------
> import sys
> from twisted.internet import app, reactor
> from twisted.python import log, logfile
> from twisted.web import server, resource
>
> class Simple(resource.Resource):
> isLeaf = True
>
> def render(self, request):
> print "Request:", request
> return "<html>Hello</html>"
>
>
> if __name__ == '__main__':
>
> f = sys.stdout
> log.startLogging(f)
>
> site = server.Site(Simple())
> application = app.Application('web')
> application.bindPorts()
> application.listenTCP(8080, site)
> application.run(save=0)
> #-----------------
>
> This works OK with the following client:
>
> #-----------------
> from twisted.internet import reactor
> from twisted.web.client import getPage
> from twisted.python.util import println
>
>
> class Client:
>
> def __init__(self):
> self.sendReq()
> reactor.run()
>
>
> def handleCallback(self, value):
> println("Success:", value)
> reactor.stop()
>
>
> def handleErrback(self, error):
> println("Error:", error)
> reactor.stop()
>
>
> def sendReq(self):
> uri = "http://localhost:8080/Simple"
> deferred = getPage(uri)
> deferred.addCallbacks(self.handleCallback,
> self.handleErrback)
>
>
> if __name__ == "__main__":
>
> Client()
> #-----------------
>
> However, under Twisted 1.1.1 this raises:
> exceptions.DeprecationWarning: twisted.internet.app is deprecated,
> use twisted.application instead.
>
> So I tried to create a new version of the server:
>
> #-----------------
> import sys
> from twisted.application import service, internet
> from twisted.internet import reactor
> from twisted.python import log
> from twisted.web import server, resource
>
>
> class Simple(resource.Resource):
> isLeaf = True
>
> def render(self, request):
> print "Request:", request
> return "<html>Hello</html>"
>
>
> if __name__ == '__main__':
>
> f = sys.stdout
> log.startLogging(f)
>
> root = Simple()
> site = server.Site(root)
> application = service.Application('web')
> sc = service.IServiceCollection(application)
> i = internet.TCPServer(8080, site)
> i.setServiceParent(sc)
> reactor.run()
reactor.run() is not how you start a service. You could:
sc.startService()
reactor.addSystemEventTrigger('before', 'shutdown',
sc.stopService)
reactor.run()
or, better yet, use twistd - see
http://twistedmatrix.com/documents/howto/application (last bit).
--
Itamar Shtull-Trauring http://itamarst.org
Looking for a job: http://itamarst.org/resume.html
More information about the Twisted-Python
mailing list