summaryrefslogtreecommitdiff
path: root/demo/ssl/twistedsslclient.py
blob: e4b79c046935858a69958f0dcf47d78a77e5a948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
"""
Demonstrates M2Crypto.SSL.TwistedProtocolWrapper

Copyright (c) 2005 Open Source Applications Foundation. All rights reserved.
"""

import twisted.internet.protocol as protocol
import twisted.protocols.basic as basic
import twisted.internet.reactor as reactor
import M2Crypto.SSL.TwistedProtocolWrapper as wrapper
import M2Crypto.SSL as SSL
        
class EchoClient(basic.LineReceiver):
    def connectionMade(self):
        self.sendLine('Hello World!')

    def lineReceived(self, line):
        print 'received: "%s"' % line
        self.transport.loseConnection()
        

class EchoClientFactory(protocol.ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print 'connection failed'
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print 'connection lost'
        reactor.stop()


class ContextFactory:
    def getContext(self):
        return SSL.Context()


if __name__ == '__main__':
    factory = EchoClientFactory()
    wrapper.connectSSL('localhost', 8000, factory, ContextFactory())
    reactor.run() # This will block until reactor.stop() is called