| 1 | from twisted.protocols.basic import LineReceiver |
|---|
| 2 | from twisted.internet import endpoints, reactor |
|---|
| 3 | from twisted.internet.address import IPv4Address, UNIXAddress |
|---|
| 4 | from twisted.internet.interfaces import IClientEndpoint, IServerEndpoint |
|---|
| 5 | from twisted.internet.protocol import ClientFactory, ServerFactory |
|---|
| 6 | |
|---|
| 7 | class MyServerProtocol(LineReceiver): |
|---|
| 8 | def connectionMade(self): |
|---|
| 9 | self.sendLine("Welcome") |
|---|
| 10 | |
|---|
| 11 | def lineReceived(self, line): |
|---|
| 12 | print "SERVER RECEIVED: ", line, "VIA", self.transport.getHost() |
|---|
| 13 | |
|---|
| 14 | class MyClientProtocol(LineReceiver): |
|---|
| 15 | def connectionMade(self): |
|---|
| 16 | self.sendLine("Hello") |
|---|
| 17 | |
|---|
| 18 | def lineReceived(self, line): |
|---|
| 19 | print "CLIENT RECEIVED: ", line, "VIA", self.transport.getHost() |
|---|
| 20 | |
|---|
| 21 | class MyServerFactory(ServerFactory): |
|---|
| 22 | protocol = MyServerProtocol |
|---|
| 23 | |
|---|
| 24 | class MyClientFactory(ClientFactory): |
|---|
| 25 | protocol = MyClientProtocol |
|---|
| 26 | |
|---|
| 27 | for addr in (UNIXAddress("/tmp/test.socket"), |
|---|
| 28 | IPv4Address("TCP", "127.0.0.1", 10080),): |
|---|
| 29 | IServerEndpoint(addr).listen(MyServerFactory().buildProtocol) |
|---|
| 30 | IClientEndpoint(addr).connect(MyClientFactory().buildProtocol) |
|---|
| 31 | |
|---|
| 32 | reactor.callLater(0.1, reactor.stop) |
|---|
| 33 | reactor.run() |
|---|