Added python-eventlet 0.15.2 for Ubuntu 14.04
[packages/trusty/python-eventlet.git] / eventlet / eventlet / green / _socket_nodns.py
1 __socket = __import__('socket')
2
3 __all__ = __socket.__all__
4 __patched__ = ['fromfd', 'socketpair', 'ssl', 'socket']
5
6 from eventlet.patcher import slurp_properties
7 slurp_properties(__socket, globals(),
8                  ignore=__patched__, srckeys=dir(__socket))
9
10 os = __import__('os')
11 import sys
12 import warnings
13 from eventlet.hubs import get_hub
14 from eventlet.greenio import GreenSocket as socket
15 from eventlet.greenio import SSL as _SSL  # for exceptions
16 from eventlet.greenio import _GLOBAL_DEFAULT_TIMEOUT
17 from eventlet.greenio import _fileobject
18
19 try:
20     __original_fromfd__ = __socket.fromfd
21
22     def fromfd(*args):
23         return socket(__original_fromfd__(*args))
24 except AttributeError:
25     pass
26
27 try:
28     __original_socketpair__ = __socket.socketpair
29
30     def socketpair(*args):
31         one, two = __original_socketpair__(*args)
32         return socket(one), socket(two)
33 except AttributeError:
34     pass
35
36
37 def _convert_to_sslerror(ex):
38     """ Transliterates SSL.SysCallErrors to socket.sslerrors"""
39     return sslerror((ex.args[0], ex.args[1]))
40
41
42 class GreenSSLObject(object):
43     """ Wrapper object around the SSLObjects returned by socket.ssl, which have a
44     slightly different interface from SSL.Connection objects. """
45
46     def __init__(self, green_ssl_obj):
47         """ Should only be called by a 'green' socket.ssl """
48         self.connection = green_ssl_obj
49         try:
50             # if it's already connected, do the handshake
51             self.connection.getpeername()
52         except:
53             pass
54         else:
55             try:
56                 self.connection.do_handshake()
57             except _SSL.SysCallError as e:
58                 raise _convert_to_sslerror(e)
59
60     def read(self, n=1024):
61         """If n is provided, read n bytes from the SSL connection, otherwise read
62         until EOF. The return value is a string of the bytes read."""
63         try:
64             return self.connection.read(n)
65         except _SSL.ZeroReturnError:
66             return ''
67         except _SSL.SysCallError as e:
68             raise _convert_to_sslerror(e)
69
70     def write(self, s):
71         """Writes the string s to the on the object's SSL connection.
72         The return value is the number of bytes written. """
73         try:
74             return self.connection.write(s)
75         except _SSL.SysCallError as e:
76             raise _convert_to_sslerror(e)
77
78     def server(self):
79         """ Returns a string describing the server's certificate. Useful for debugging
80         purposes; do not parse the content of this string because its format can't be
81         parsed unambiguously. """
82         return str(self.connection.get_peer_certificate().get_subject())
83
84     def issuer(self):
85         """Returns a string describing the issuer of the server's certificate. Useful
86         for debugging purposes; do not parse the content of this string because its
87         format can't be parsed unambiguously."""
88         return str(self.connection.get_peer_certificate().get_issuer())
89
90
91 try:
92     from eventlet.green import ssl as ssl_module
93     sslerror = __socket.sslerror
94     __socket.ssl
95 except AttributeError:
96     # if the real socket module doesn't have the ssl method or sslerror
97     # exception, we can't emulate them
98     pass
99 else:
100     def ssl(sock, certificate=None, private_key=None):
101         warnings.warn("socket.ssl() is deprecated.  Use ssl.wrap_socket() instead.",
102                       DeprecationWarning, stacklevel=2)
103         return ssl_module.sslwrap_simple(sock, private_key, certificate)