Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / examples / simple.tac
1 # You can run this .tac file directly with:
2 #    twistd -ny simple.tac
3
4 from twisted.application import service, internet
5 from twisted.protocols import wire
6 from twisted.internet import protocol
7 from twisted.python import util
8
9 application = service.Application('test')
10 s = service.IServiceCollection(application)
11 factory = protocol.ServerFactory()
12 factory.protocol = wire.Echo
13 internet.TCPServer(8080, factory).setServiceParent(s)
14
15 internet.TCPServer(8081, factory).setServiceParent(s)
16 internet.TimerService(5, util.println, "--MARK--").setServiceParent(s)
17
18 class Foo(protocol.Protocol):
19     def connectionMade(self):
20         self.transport.write('lalala\n')
21     def dataReceived(self, data):
22         print `data`
23
24 factory = protocol.ClientFactory()
25 factory.protocol = Foo
26 internet.TCPClient('localhost', 8081, factory).setServiceParent(s)
27
28 class FooService(service.Service):
29     def startService(self):
30         service.Service.startService(self)
31         print 'lala, starting'
32     def stopService(self):
33         service.Service.stopService(self)
34         print 'lala, stopping'
35         print self.parent.getServiceNamed(self.name) is self
36
37 foo = FooService()
38 foo.setName('foo')
39 foo.setServiceParent(s)