~ubuntu-branches/ubuntu/raring/m2crypto/raring

« back to all changes in this revision

Viewing changes to demo/ssl/twistedsslclient.py

  • Committer: Barry Warsaw
  • Date: 2011-10-25 18:26:01 UTC
  • mfrom: (24.1.4 merge)
  • Revision ID: barry@python.org-20111025182601-uag0ee3eh3ahtofm
Merge from Debian with remaining deltas.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""
 
3
Demonstrates M2Crypto.SSL.TwistedProtocolWrapper
 
4
 
 
5
Copyright (c) 2005 Open Source Applications Foundation. All rights reserved.
 
6
"""
 
7
 
 
8
import twisted.internet.protocol as protocol
 
9
import twisted.protocols.basic as basic
 
10
import twisted.internet.reactor as reactor
 
11
import M2Crypto.SSL.TwistedProtocolWrapper as wrapper
 
12
import M2Crypto.SSL as SSL
 
13
        
 
14
class EchoClient(basic.LineReceiver):
 
15
    def connectionMade(self):
 
16
        self.sendLine('Hello World!')
 
17
 
 
18
    def lineReceived(self, line):
 
19
        print 'received: "%s"' % line
 
20
        self.transport.loseConnection()
 
21
        
 
22
 
 
23
class EchoClientFactory(protocol.ClientFactory):
 
24
    protocol = EchoClient
 
25
 
 
26
    def clientConnectionFailed(self, connector, reason):
 
27
        print 'connection failed'
 
28
        reactor.stop()
 
29
 
 
30
    def clientConnectionLost(self, connector, reason):
 
31
        print 'connection lost'
 
32
        reactor.stop()
 
33
 
 
34
 
 
35
class ContextFactory:
 
36
    def getContext(self):
 
37
        return SSL.Context()
 
38
 
 
39
 
 
40
if __name__ == '__main__':
 
41
    factory = EchoClientFactory()
 
42
    wrapper.connectSSL('localhost', 8000, factory, ContextFactory())
 
43
    reactor.run() # This will block until reactor.stop() is called