gdata.tlslite.integration.TLSTwistedProtocolWrapper
index
/usr/lib/python2.7/dist-packages/gdata/tlslite/integration/TLSTwistedProtocolWrapper.py

TLS Lite + Twisted.

 
Modules
       
errno
socket

 
Classes
       
gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine
TLSTwistedProtocolWrapper(twisted.protocols.policies.ProtocolWrapper, gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine)
twisted.protocols.policies.ProtocolWrapper(twisted.internet.protocol.Protocol)
TLSTwistedProtocolWrapper(twisted.protocols.policies.ProtocolWrapper, gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine)

 
class TLSTwistedProtocolWrapper(twisted.protocols.policies.ProtocolWrapper, gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine)
    This class can wrap Twisted protocols to add TLS support.
 
Below is a complete example of using TLS Lite with a Twisted echo
server.
 
There are two server implementations below.  Echo is the original
protocol, which is oblivious to TLS.  Echo1 subclasses Echo and
negotiates TLS when the client connects.  Echo2 subclasses Echo and
negotiates TLS when the client sends "STARTTLS"::
 
    from twisted.internet.protocol import Protocol, Factory
    from twisted.internet import reactor
    from twisted.protocols.policies import WrappingFactory
    from twisted.protocols.basic import LineReceiver
    from twisted.python import log
    from twisted.python.failure import Failure
    import sys
    from tlslite.api import *
 
    s = open("./serverX509Cert.pem").read()
    x509 = X509()
    x509.parse(s)
    certChain = X509CertChain([x509])
 
    s = open("./serverX509Key.pem").read()
    privateKey = parsePEMKey(s, private=True)
 
    verifierDB = VerifierDB("verifierDB")
    verifierDB.open()
 
    class Echo(LineReceiver):
        def connectionMade(self):
            self.transport.write("Welcome to the echo server!\r\n")
 
        def lineReceived(self, line):
            self.transport.write(line + "\r\n")
 
    class Echo1(Echo):
        def connectionMade(self):
            if not self.transport.tlsStarted:
                self.transport.setServerHandshakeOp(certChain=certChain,
                                                    privateKey=privateKey,
                                                    verifierDB=verifierDB)
            else:
                Echo.connectionMade(self)
 
        def connectionLost(self, reason):
            pass #Handle any TLS exceptions here
 
    class Echo2(Echo):
        def lineReceived(self, data):
            if data == "STARTTLS":
                self.transport.setServerHandshakeOp(certChain=certChain,
                                                    privateKey=privateKey,
                                                    verifierDB=verifierDB)
            else:
                Echo.lineReceived(self, data)
 
        def connectionLost(self, reason):
            pass #Handle any TLS exceptions here
 
    factory = Factory()
    factory.protocol = Echo1
    #factory.protocol = Echo2
 
    wrappingFactory = WrappingFactory(factory)
    wrappingFactory.protocol = TLSTwistedProtocolWrapper
 
    log.startLogging(sys.stdout)
    reactor.listenTCP(1079, wrappingFactory)
    reactor.run()
 
This class works as follows:
 
Data comes in and is given to the AsyncStateMachine for handling.
AsyncStateMachine will forward events to this class, and we'll
pass them on to the ProtocolHandler, which will proxy them to the
wrapped protocol.  The wrapped protocol may then call back into
this class, and these calls will be proxied into the
AsyncStateMachine.
 
The call graph looks like this:
 - self.dataReceived
   - AsyncStateMachine.inReadEvent
     - out(Connect|Close|Read)Event
       - ProtocolWrapper.(connectionMade|loseConnection|dataReceived)
         - self.(loseConnection|write|writeSequence)
           - AsyncStateMachine.(setCloseOp|setWriteOp)
 
 
Method resolution order:
TLSTwistedProtocolWrapper
twisted.protocols.policies.ProtocolWrapper
twisted.internet.protocol.Protocol
twisted.internet.protocol.BaseProtocol
gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine

Methods defined here:
__init__(self, factory, wrappedProtocol)
connectionLost(self, reason)
connectionMade(self)
dataReceived(self, data)
loseConnection(self)
outCloseEvent(self)
outConnectEvent(self)
outReadEvent(self, data)
setServerHandshakeOp(self, **args)
write(self, data)
writeSequence(self, seq)

Methods inherited from twisted.protocols.policies.ProtocolWrapper:
__getattr__(self, name)
getHost(self)
getPeer(self)
logPrefix(self)
Use a customized log prefix mentioning both the wrapped protocol and
the current one.
makeConnection(self, transport)
When a connection is made, register this wrapper with its factory,
save the real transport, and connect the wrapped protocol to this
L{ProtocolWrapper} to intercept any transport calls it makes.
registerProducer(self, producer, streaming)
stopConsuming(self)
unregisterProducer(self)

Data and other attributes inherited from twisted.protocols.policies.ProtocolWrapper:
disconnecting = 0

Methods inherited from twisted.internet.protocol.Protocol:
__provides__
Special descriptor for class __provides__
 
The descriptor caches the implementedBy info, so that
we can get declarations for objects without instance-specific
interfaces a bit quicker.

Data and other attributes inherited from twisted.internet.protocol.Protocol:
__implemented__ = <implementedBy twisted.internet.protocol.Protocol>

Methods inherited from twisted.internet.protocol.BaseProtocol:
__providedBy__ = <zope.interface.declarations.Declaration object>
Data and other attributes inherited from twisted.internet.protocol.BaseProtocol:
connected = 0
transport = None

Methods inherited from gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine:
inReadEvent(self)
Tell the state machine it can read from the socket.
inWriteEvent(self)
Tell the state machine it can write to the socket.
outWriteEvent(self)
Called when a write operation completes.
 
May be overridden in subclass.
setCloseOp(self)
Start a close operation.
setHandshakeOp(self, handshaker)
Start a handshake operation.
 
@type handshaker: generator
@param handshaker: A generator created by using one of the
asynchronous handshake functions (i.e. handshakeServerAsync, or
handshakeClientxxx(..., async=True).
setWriteOp(self, writeBuffer)
Start a write operation.
 
@type writeBuffer: str
@param writeBuffer: The string to transmit.
wantsReadEvent(self)
If the state machine wants to read.
 
If an operation is active, this returns whether or not the
operation wants to read from the socket.  If an operation is
not active, this returns None.
 
@rtype: bool or None
@return: If the state machine wants to read.
wantsWriteEvent(self)
If the state machine wants to write.
 
If an operation is active, this returns whether or not the
operation wants to write to the socket.  If an operation is
not active, this returns None.
 
@rtype: bool or None
@return: If the state machine wants to write.