| 1 | from twisted.internet import protocol, reactor, ssl |
|---|
| 2 | |
|---|
| 3 | from OpenSSL import SSL |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | class CtxFactory(ssl.ClientContextFactory): |
|---|
| 7 | |
|---|
| 8 | def getContext(self): |
|---|
| 9 | self.method = SSL.SSLv23_METHOD |
|---|
| 10 | ctx = ssl.ClientContextFactory.getContext(self) |
|---|
| 11 | ctx.use_certificate_file('/etc/passwd') |
|---|
| 12 | ctx.use_privatekey_file('/etc/passwd') |
|---|
| 13 | |
|---|
| 14 | return ctx |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | class Protocol(protocol.Protocol): |
|---|
| 18 | |
|---|
| 19 | def connectionMade(self): |
|---|
| 20 | print "connection made" |
|---|
| 21 | |
|---|
| 22 | def connectionLost(self, reason): |
|---|
| 23 | print "connection lost", reason |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | class Factory(protocol.ClientFactory): |
|---|
| 27 | |
|---|
| 28 | protocol = Protocol |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | def timeout(): |
|---|
| 32 | print "TIMEOUT" |
|---|
| 33 | reactor.stop() |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | reactor.connectSSL('twistedmatrix.com', 443, Factory(), CtxFactory()) |
|---|
| 37 | reactor.callLater(3, timeout) |
|---|
| 38 | reactor.run() |
|---|