Ticket #2060: connect-generic.py

File connect-generic.py, 1.0 KB (added by rwall, 8 years ago)

A simple example of usage.

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