| |
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| <html><head><title>Python: module gdata.tlslite.integration.TLSTwistedProtocolWrapper</title> |
| </head><body bgcolor="#f0f0f8"> |
| |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| <tr bgcolor="#7799ee"> |
| <td valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="gdata.html"><font color="#ffffff">gdata</font></a>.<a href="gdata.tlslite.html"><font color="#ffffff">tlslite</font></a>.<a href="gdata.tlslite.integration.html"><font color="#ffffff">integration</font></a>.TLSTwistedProtocolWrapper</strong></big></big></font></td |
| ><td align=right valign=bottom |
| ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/usr/local/google/home/afshar/src/external-gdata-release/google3/src/gdata/tlslite/integration/TLSTwistedProtocolWrapper.py">/usr/local/google/home/afshar/src/external-gdata-release/google3/src/gdata/tlslite/integration/TLSTwistedProtocolWrapper.py</a></font></td></tr></table> |
| <p><tt>TLS Lite + Twisted.</tt></p> |
| <p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#aa55cc"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> |
| |
| <tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td> |
| <td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="errno.html">errno</a><br> |
| </td><td width="25%" valign=top><a href="socket.html">socket</a><br> |
| </td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#ee77aa"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| |
| <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| <td width="100%"><dl> |
| <dt><font face="helvetica, arial"><a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a> |
| </font></dt><dd> |
| <dl> |
| <dt><font face="helvetica, arial"><a href="gdata.tlslite.integration.TLSTwistedProtocolWrapper.html#TLSTwistedProtocolWrapper">TLSTwistedProtocolWrapper</a>(<a href="twisted.protocols.policies.html#ProtocolWrapper">twisted.protocols.policies.ProtocolWrapper</a>, <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a>) |
| </font></dt></dl> |
| </dd> |
| <dt><font face="helvetica, arial"><a href="twisted.protocols.policies.html#ProtocolWrapper">twisted.protocols.policies.ProtocolWrapper</a>(<a href="twisted.internet.protocol.html#Protocol">twisted.internet.protocol.Protocol</a>) |
| </font></dt><dd> |
| <dl> |
| <dt><font face="helvetica, arial"><a href="gdata.tlslite.integration.TLSTwistedProtocolWrapper.html#TLSTwistedProtocolWrapper">TLSTwistedProtocolWrapper</a>(<a href="twisted.protocols.policies.html#ProtocolWrapper">twisted.protocols.policies.ProtocolWrapper</a>, <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a>) |
| </font></dt></dl> |
| </dd> |
| </dl> |
| <p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#ffc8d8"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#000000" face="helvetica, arial"><a name="TLSTwistedProtocolWrapper">class <strong>TLSTwistedProtocolWrapper</strong></a>(<a href="twisted.protocols.policies.html#ProtocolWrapper">twisted.protocols.policies.ProtocolWrapper</a>, <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a>)</font></td></tr> |
| |
| <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| <td colspan=2><tt>This class can wrap Twisted protocols to add TLS support.<br> |
| <br> |
| Below is a complete example of using TLS Lite with a Twisted echo<br> |
| server.<br> |
| <br> |
| There are two server implementations below. Echo is the original<br> |
| protocol, which is oblivious to TLS. Echo1 subclasses Echo and<br> |
| negotiates TLS when the client connects. Echo2 subclasses Echo and<br> |
| negotiates TLS when the client sends "STARTTLS"::<br> |
| <br> |
| from twisted.internet.protocol import Protocol, Factory<br> |
| from twisted.internet import reactor<br> |
| from twisted.protocols.policies import WrappingFactory<br> |
| from twisted.protocols.basic import LineReceiver<br> |
| from twisted.python import log<br> |
| from twisted.python.failure import Failure<br> |
| import sys<br> |
| from tlslite.api import *<br> |
| <br> |
| s = open("./serverX509Cert.pem").read()<br> |
| x509 = X509()<br> |
| x509.parse(s)<br> |
| certChain = X509CertChain([x509])<br> |
| <br> |
| s = open("./serverX509Key.pem").read()<br> |
| privateKey = parsePEMKey(s, private=True)<br> |
| <br> |
| verifierDB = VerifierDB("verifierDB")<br> |
| verifierDB.open()<br> |
| <br> |
| class Echo(LineReceiver):<br> |
| def <a href="#TLSTwistedProtocolWrapper-connectionMade">connectionMade</a>(self):<br> |
| self.<strong>transport</strong>.<a href="#TLSTwistedProtocolWrapper-write">write</a>("Welcome to the echo server!\r\n")<br> |
| <br> |
| def lineReceived(self, line):<br> |
| self.<strong>transport</strong>.<a href="#TLSTwistedProtocolWrapper-write">write</a>(line + "\r\n")<br> |
| <br> |
| class Echo1(Echo):<br> |
| def <a href="#TLSTwistedProtocolWrapper-connectionMade">connectionMade</a>(self):<br> |
| if not self.<strong>transport</strong>.tlsStarted:<br> |
| self.<strong>transport</strong>.<a href="#TLSTwistedProtocolWrapper-setServerHandshakeOp">setServerHandshakeOp</a>(certChain=certChain,<br> |
| privateKey=privateKey,<br> |
| verifierDB=verifierDB)<br> |
| else:<br> |
| Echo.<a href="#TLSTwistedProtocolWrapper-connectionMade">connectionMade</a>(self)<br> |
| <br> |
| def <a href="#TLSTwistedProtocolWrapper-connectionLost">connectionLost</a>(self, reason):<br> |
| pass #Handle any TLS exceptions here<br> |
| <br> |
| class Echo2(Echo):<br> |
| def lineReceived(self, data):<br> |
| if data == "STARTTLS":<br> |
| self.<strong>transport</strong>.<a href="#TLSTwistedProtocolWrapper-setServerHandshakeOp">setServerHandshakeOp</a>(certChain=certChain,<br> |
| privateKey=privateKey,<br> |
| verifierDB=verifierDB)<br> |
| else:<br> |
| Echo.lineReceived(self, data)<br> |
| <br> |
| def <a href="#TLSTwistedProtocolWrapper-connectionLost">connectionLost</a>(self, reason):<br> |
| pass #Handle any TLS exceptions here<br> |
| <br> |
| factory = Factory()<br> |
| factory.protocol = Echo1<br> |
| #factory.protocol = Echo2<br> |
| <br> |
| wrappingFactory = WrappingFactory(factory)<br> |
| wrappingFactory.protocol = <a href="#TLSTwistedProtocolWrapper">TLSTwistedProtocolWrapper</a><br> |
| <br> |
| log.startLogging(sys.stdout)<br> |
| reactor.listenTCP(1079, wrappingFactory)<br> |
| reactor.run()<br> |
| <br> |
| This class works as follows:<br> |
| <br> |
| Data comes in and is given to the <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">AsyncStateMachine</a> for handling.<br> |
| <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">AsyncStateMachine</a> will forward events to this class, and we'll<br> |
| pass them on to the ProtocolHandler, which will proxy them to the<br> |
| wrapped protocol. The wrapped protocol may then call back into<br> |
| this class, and these calls will be proxied into the<br> |
| <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">AsyncStateMachine</a>.<br> |
| <br> |
| The call graph looks like this:<br> |
| - self.<strong>dataReceived</strong><br> |
| - <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">AsyncStateMachine</a>.inReadEvent<br> |
| - out(Connect|Close|Read)Event<br> |
| - <a href="twisted.protocols.policies.html#ProtocolWrapper">ProtocolWrapper</a>.(connectionMade|loseConnection|dataReceived)<br> |
| - self.(loseConnection|write|writeSequence)<br> |
| - <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">AsyncStateMachine</a>.(setCloseOp|setWriteOp)<br> </tt></td></tr> |
| <tr><td> </td> |
| <td width="100%"><dl><dt>Method resolution order:</dt> |
| <dd><a href="gdata.tlslite.integration.TLSTwistedProtocolWrapper.html#TLSTwistedProtocolWrapper">TLSTwistedProtocolWrapper</a></dd> |
| <dd><a href="twisted.protocols.policies.html#ProtocolWrapper">twisted.protocols.policies.ProtocolWrapper</a></dd> |
| <dd><a href="twisted.internet.protocol.html#Protocol">twisted.internet.protocol.Protocol</a></dd> |
| <dd><a href="twisted.internet.protocol.html#BaseProtocol">twisted.internet.protocol.BaseProtocol</a></dd> |
| <dd><a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a></dd> |
| </dl> |
| <hr> |
| Methods defined here:<br> |
| <dl><dt><a name="TLSTwistedProtocolWrapper-__init__"><strong>__init__</strong></a>(self, factory, wrappedProtocol)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-connectionLost"><strong>connectionLost</strong></a>(self, reason)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-connectionMade"><strong>connectionMade</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-dataReceived"><strong>dataReceived</strong></a>(self, data)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-loseConnection"><strong>loseConnection</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-outCloseEvent"><strong>outCloseEvent</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-outConnectEvent"><strong>outConnectEvent</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-outReadEvent"><strong>outReadEvent</strong></a>(self, data)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-setServerHandshakeOp"><strong>setServerHandshakeOp</strong></a>(self, **args)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-write"><strong>write</strong></a>(self, data)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-writeSequence"><strong>writeSequence</strong></a>(self, seq)</dt></dl> |
| |
| <hr> |
| Methods inherited from <a href="twisted.protocols.policies.html#ProtocolWrapper">twisted.protocols.policies.ProtocolWrapper</a>:<br> |
| <dl><dt><a name="TLSTwistedProtocolWrapper-__getattr__"><strong>__getattr__</strong></a>(self, name)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-getHost"><strong>getHost</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-getPeer"><strong>getPeer</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-logPrefix"><strong>logPrefix</strong></a>(self)</dt><dd><tt>Use a customized log prefix mentioning both the wrapped protocol and<br> |
| the current one.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-makeConnection"><strong>makeConnection</strong></a>(self, transport)</dt><dd><tt>When a connection is made, register this wrapper with its factory,<br> |
| save the real transport, and connect the wrapped protocol to this<br> |
| L{<a href="twisted.protocols.policies.html#ProtocolWrapper">ProtocolWrapper</a>} to intercept any transport calls it makes.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-registerProducer"><strong>registerProducer</strong></a>(self, producer, streaming)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-stopConsuming"><strong>stopConsuming</strong></a>(self)</dt></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-unregisterProducer"><strong>unregisterProducer</strong></a>(self)</dt></dl> |
| |
| <hr> |
| Data and other attributes inherited from <a href="twisted.protocols.policies.html#ProtocolWrapper">twisted.protocols.policies.ProtocolWrapper</a>:<br> |
| <dl><dt><strong>disconnecting</strong> = 0</dl> |
| |
| <hr> |
| Methods inherited from <a href="twisted.internet.protocol.html#Protocol">twisted.internet.protocol.Protocol</a>:<br> |
| <dl><dt><strong>__provides__</strong></dt> |
| <dd><tt>Special descriptor for class __provides__<br> |
| <br> |
| The descriptor caches the implementedBy info, so that<br> |
| we can get declarations for objects without instance-specific<br> |
| interfaces a bit quicker.<br> |
| <br> |
| For example:<br> |
| <br> |
| >>> from zope.interface import Interface<br> |
| >>> class IFooFactory(Interface):<br> |
| ... pass<br> |
| >>> class IFoo(Interface):<br> |
| ... pass<br> |
| >>> class C(object):<br> |
| ... implements(IFoo)<br> |
| ... classProvides(IFooFactory)<br> |
| >>> [i.getName() for i in C.__provides__]<br> |
| ['IFooFactory']<br> |
| <br> |
| >>> [i.getName() for i in C().__provides__]<br> |
| ['IFoo']</tt></dd> |
| </dl> |
| |
| <hr> |
| Data and other attributes inherited from <a href="twisted.internet.protocol.html#Protocol">twisted.internet.protocol.Protocol</a>:<br> |
| <dl><dt><strong>__implemented__</strong> = <implementedBy twisted.internet.protocol.Protocol></dl> |
| |
| <hr> |
| Methods inherited from <a href="twisted.internet.protocol.html#BaseProtocol">twisted.internet.protocol.BaseProtocol</a>:<br> |
| <strong>__providedBy__</strong> = <zope.interface.declarations.Declaration object> |
| <hr> |
| Data and other attributes inherited from <a href="twisted.internet.protocol.html#BaseProtocol">twisted.internet.protocol.BaseProtocol</a>:<br> |
| <dl><dt><strong>connected</strong> = 0</dl> |
| |
| <dl><dt><strong>transport</strong> = None</dl> |
| |
| <hr> |
| Methods inherited from <a href="gdata.tlslite.integration.AsyncStateMachine.html#AsyncStateMachine">gdata.tlslite.integration.AsyncStateMachine.AsyncStateMachine</a>:<br> |
| <dl><dt><a name="TLSTwistedProtocolWrapper-inReadEvent"><strong>inReadEvent</strong></a>(self)</dt><dd><tt>Tell the state machine it can read from the socket.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-inWriteEvent"><strong>inWriteEvent</strong></a>(self)</dt><dd><tt>Tell the state machine it can write to the socket.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-outWriteEvent"><strong>outWriteEvent</strong></a>(self)</dt><dd><tt>Called when a write operation completes.<br> |
| <br> |
| May be overridden in subclass.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-setCloseOp"><strong>setCloseOp</strong></a>(self)</dt><dd><tt>Start a close operation.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-setHandshakeOp"><strong>setHandshakeOp</strong></a>(self, handshaker)</dt><dd><tt>Start a handshake operation.<br> |
| <br> |
| @type handshaker: generator<br> |
| @param handshaker: A generator created by using one of the<br> |
| asynchronous handshake functions (i.e. handshakeServerAsync, or<br> |
| handshakeClientxxx(..., async=True).</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-setWriteOp"><strong>setWriteOp</strong></a>(self, writeBuffer)</dt><dd><tt>Start a write operation.<br> |
| <br> |
| @type writeBuffer: str<br> |
| @param writeBuffer: The string to transmit.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-wantsReadEvent"><strong>wantsReadEvent</strong></a>(self)</dt><dd><tt>If the state machine wants to read.<br> |
| <br> |
| If an operation is active, this returns whether or not the<br> |
| operation wants to read from the socket. If an operation is<br> |
| not active, this returns None.<br> |
| <br> |
| @rtype: bool or None<br> |
| @return: If the state machine wants to read.</tt></dd></dl> |
| |
| <dl><dt><a name="TLSTwistedProtocolWrapper-wantsWriteEvent"><strong>wantsWriteEvent</strong></a>(self)</dt><dd><tt>If the state machine wants to write.<br> |
| <br> |
| If an operation is active, this returns whether or not the<br> |
| operation wants to write to the socket. If an operation is<br> |
| not active, this returns None.<br> |
| <br> |
| @rtype: bool or None<br> |
| @return: If the state machine wants to write.</tt></dd></dl> |
| |
| </td></tr></table></td></tr></table> |
| </body></html> |