1 __socket = __import__('socket')
3 __all__ = __socket.__all__
4 __patched__ = ['fromfd', 'socketpair', 'ssl', 'socket']
6 from eventlet.patcher import slurp_properties
7 slurp_properties(__socket, globals(),
8 ignore=__patched__, srckeys=dir(__socket))
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
20 __original_fromfd__ = __socket.fromfd
23 return socket(__original_fromfd__(*args))
24 except AttributeError:
28 __original_socketpair__ = __socket.socketpair
30 def socketpair(*args):
31 one, two = __original_socketpair__(*args)
32 return socket(one), socket(two)
33 except AttributeError:
37 def _convert_to_sslerror(ex):
38 """ Transliterates SSL.SysCallErrors to socket.sslerrors"""
39 return sslerror((ex.args[0], ex.args[1]))
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. """
46 def __init__(self, green_ssl_obj):
47 """ Should only be called by a 'green' socket.ssl """
48 self.connection = green_ssl_obj
50 # if it's already connected, do the handshake
51 self.connection.getpeername()
56 self.connection.do_handshake()
57 except _SSL.SysCallError as e:
58 raise _convert_to_sslerror(e)
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."""
64 return self.connection.read(n)
65 except _SSL.ZeroReturnError:
67 except _SSL.SysCallError as e:
68 raise _convert_to_sslerror(e)
71 """Writes the string s to the on the object's SSL connection.
72 The return value is the number of bytes written. """
74 return self.connection.write(s)
75 except _SSL.SysCallError as e:
76 raise _convert_to_sslerror(e)
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())
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())
92 from eventlet.green import ssl as ssl_module
93 sslerror = __socket.sslerror
95 except AttributeError:
96 # if the real socket module doesn't have the ssl method or sslerror
97 # exception, we can't emulate them
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)