~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to doc/core/examples/echoclient.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-01-16 19:56:10 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060116195610-ykmxbia4mnnod9o2
Tags: 2.1.0-0ubuntu2
debian/copyright: Include copyright for python 2.3; some 2.3 files
are included in the upstream tarball, but not in the binary packages.

Show diffs side-by-side

added added

removed removed

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