Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / examples / echoclient.py
1 #!/usr/bin/env python
2
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
5
6
7 from twisted.internet.protocol import ClientFactory
8 from twisted.protocols.basic import LineReceiver
9 from twisted.internet import reactor
10 import sys
11
12 class EchoClient(LineReceiver):
13     end="Bye-bye!"
14     def connectionMade(self):
15         self.sendLine("Hello, world!")
16         self.sendLine("What a fine day it is.")
17         self.sendLine(self.end)
18
19     def lineReceived(self, line):
20         print "receive:", line
21         if line==self.end:
22             self.transport.loseConnection()
23
24 class EchoClientFactory(ClientFactory):
25     protocol = EchoClient
26
27     def clientConnectionFailed(self, connector, reason):
28         print 'connection failed:', reason.getErrorMessage()
29         reactor.stop()
30
31     def clientConnectionLost(self, connector, reason):
32         print 'connection lost:', reason.getErrorMessage()
33         reactor.stop()
34
35 def main():
36     factory = EchoClientFactory()
37     reactor.connectTCP('localhost', 8000, factory)
38     reactor.run()
39
40 if __name__ == '__main__':
41     main()