~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/examples/echoclient_ssl.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

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