Annotation of MumbleDicebot/mumble_client.py, Revision 1.1
1.1 ! rubenllo 1: from twisted.internet import ssl, reactor, protocol
! 2:
! 3:
! 4: class MumbleClientFactory(protocol.ClientFactory):
! 5: protocol = None
! 6:
! 7: def __init__(self, protocol, nickname, password):
! 8: self.nickname = nickname
! 9: self.password = password
! 10: self.protocol = protocol
! 11:
! 12: def clientConnectionFailed(self, connector, reason):
! 13: print "Connection failed"
! 14: reactor.stop()
! 15:
! 16: def clientConnectionLost(self, connector, reason):
! 17: print "Connection lost"
! 18: reactor.stop()
! 19:
! 20:
! 21: def load_certificate(cert_file):
! 22: from OpenSSL import crypto, SSL
! 23: p12 = crypto.load_pkcs12(file(cert_file, 'rb').read())
! 24:
! 25: class CtxFactory(ssl.ClientContextFactory):
! 26: def getContext(self):
! 27: self.method = SSL.SSLv23_METHOD
! 28: ctx = ssl.ClientContextFactory.getContext(self)
! 29: ctx.use_certificate(p12.get_certificate())
! 30: ctx.use_privatekey(p12.get_privatekey())
! 31: return ctx
! 32:
! 33: return CtxFactory
! 34:
! 35:
! 36: def create_client(protocol, user, pw=""):
! 37: return MumbleClientFactory(protocol, user, pw)
! 38:
! 39:
! 40: def start_client(factory, ip, port, certificate=None):
! 41: # Loads certificate if available
! 42: if certificate:
! 43: ctx = load_certificate(certificate)
! 44: else:
! 45: ctx = ssl.ClientContextFactory
! 46:
! 47: reactor.connectSSL(ip, port, factory, ctx())
! 48: reactor.run()
CVSweb