File: simple.tac

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (46 lines) | stat: -rw-r--r-- 1,213 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# You can run this .tac file directly with:
#    twistd -ny simple.tac


from twisted.application import internet, service
from twisted.internet import protocol
from twisted.protocols import wire
from twisted.python import util

application = service.Application("test")
s = service.IServiceCollection(application)
factory = protocol.ServerFactory()
factory.protocol = wire.Echo
internet.TCPServer(8080, factory).setServiceParent(s)

internet.TCPServer(8081, factory).setServiceParent(s)
internet.TimerService(5, util.println, "--MARK--").setServiceParent(s)


class Foo(protocol.Protocol):
    def connectionMade(self):
        self.transport.write(b"lalala\n")

    def dataReceived(self, data):
        print(data)


factory = protocol.ClientFactory()
factory.protocol = Foo
internet.TCPClient("localhost", 8081, factory).setServiceParent(s)


class FooService(service.Service):
    def startService(self):
        service.Service.startService(self)
        print("lala, starting")

    def stopService(self):
        service.Service.stopService(self)
        print("lala, stopping")
        print(self.parent.getServiceNamed(self.name) is self)


foo = FooService()
foo.setName("foo")
foo.setServiceParent(s)