~ubuntu-branches/ubuntu/trusty/twisted/trusty

« back to all changes in this revision

Viewing changes to twisted/protocols/tls.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-16 17:44:26 UTC
  • mfrom: (44.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130116174426-jbisu7qb8o8ncsys
Tags: 12.3.0-1ubuntu1
* Build python3 packages.
* Add '3' as the suffix for the binaries using python3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
to run TLS over unusual transports, such as UNIX sockets and stdio.
36
36
"""
37
37
 
 
38
from __future__ import division, absolute_import
38
39
 
39
40
from OpenSSL.SSL import Error, ZeroReturnError, WantReadError
40
41
from OpenSSL.SSL import TLSv1_METHOD, Context, Connection
41
42
 
42
43
try:
43
44
    Connection(Context(TLSv1_METHOD), None)
44
 
except TypeError, e:
 
45
except TypeError as e:
45
46
    if str(e) != "argument must be an int, or have a fileno() method.":
46
47
        raise
47
48
    raise ImportError("twisted.protocols.tls requires pyOpenSSL 0.10 or newer.")
48
49
 
49
 
from zope.interface import implements, providedBy, directlyProvides
 
50
from zope.interface import implementer, providedBy, directlyProvides
50
51
 
 
52
from twisted.python.compat import unicode
51
53
from twisted.python.failure import Failure
52
54
from twisted.python import log
53
 
from twisted.python.reflect import safe_str
 
55
from twisted.python._reflectpy3 import safe_str
54
56
from twisted.internet.interfaces import ISystemHandle, ISSLTransport
55
57
from twisted.internet.interfaces import IPushProducer, ILoggingContext
56
58
from twisted.internet.main import CONNECTION_LOST
59
61
from twisted.protocols.policies import ProtocolWrapper, WrappingFactory
60
62
 
61
63
 
 
64
@implementer(IPushProducer)
62
65
class _PullToPush(object):
63
66
    """
64
67
    An adapter that converts a non-streaming to a streaming producer.
83
86
    @ivar _coopTask: the result of calling L{cooperate}, the task driving the
84
87
                     streaming producer.
85
88
    """
86
 
    implements(IPushProducer)
87
89
 
88
90
    _finished = False
89
91
 
157
159
 
158
160
 
159
161
 
 
162
@implementer(IPushProducer)
160
163
class _ProducerMembrane(object):
161
164
    """
162
165
    Stand-in for producer registered with a L{TLSMemoryBIOProtocol} transport.
166
169
 
167
170
    @ivar _producer: The application-layer producer.
168
171
    """
169
 
    implements(IPushProducer)
170
172
 
171
173
    _producerPaused = False
172
174
 
205
207
 
206
208
 
207
209
 
 
210
@implementer(ISystemHandle, ISSLTransport)
208
211
class TLSMemoryBIOProtocol(ProtocolWrapper):
209
212
    """
210
213
    L{TLSMemoryBIOProtocol} is a protocol wrapper which uses OpenSSL via a
259
262
        or C{None} if no producer has been registered or a previous one was
260
263
        unregistered.
261
264
    """
262
 
    implements(ISystemHandle, ISSLTransport)
263
265
 
264
266
    _reason = None
265
267
    _handshakeDone = False
365
367
                # Passing in None means the user protocol's connnectionLost
366
368
                # will get called with reason from underlying transport:
367
369
                self._tlsShutdownFinished(None)
368
 
            except Error, e:
 
370
            except Error as e:
369
371
                # Something went pretty wrong.  For example, this might be a
370
372
                # handshake failure (because there were no shared ciphers, because
371
373
                # a certificate failed to verify, etc).  TLS can no longer proceed.
483
485
        If C{loseConnection} was called, subsequent calls to C{write} will
484
486
        drop the bytes on the floor.
485
487
        """
 
488
        if isinstance(bytes, unicode):
 
489
            raise TypeError("Must write bytes to a TLS transport, not unicode.")
486
490
        # Writes after loseConnection are not supported, unless a producer has
487
491
        # been registered, in which case writes can happen until the producer
488
492
        # is unregistered:
534
538
        Write a sequence of application bytes by joining them into one string
535
539
        and passing them to L{write}.
536
540
        """
537
 
        self.write("".join(iovec))
 
541
        self.write(b"".join(iovec))
538
542
 
539
543
 
540
544
    def getPeerCertificate(self):