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