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))
12 from eventlet.hubs import get_hub
13 from eventlet.greenio import GreenSocket as socket
14 from eventlet.greenio import SSL as _SSL # for exceptions
15 from eventlet.greenio import _GLOBAL_DEFAULT_TIMEOUT
16 from eventlet.greenio import _fileobject
19 __original_fromfd__ = __socket.fromfd
22 return socket(__original_fromfd__(*args))
23 except AttributeError:
27 __original_socketpair__ = __socket.socketpair
29 def socketpair(*args):
30 one, two = __original_socketpair__(*args)
31 return socket(one), socket(two)
32 except AttributeError:
36 def _convert_to_sslerror(ex):
37 """ Transliterates SSL.SysCallErrors to socket.sslerrors"""
38 return sslerror((ex.args[0], ex.args[1]))
41 class GreenSSLObject(object):
42 """ Wrapper object around the SSLObjects returned by socket.ssl, which have a
43 slightly different interface from SSL.Connection objects. """
45 def __init__(self, green_ssl_obj):
46 """ Should only be called by a 'green' socket.ssl """
47 self.connection = green_ssl_obj
49 # if it's already connected, do the handshake
50 self.connection.getpeername()
55 self.connection.do_handshake()
56 except _SSL.SysCallError as e:
57 raise _convert_to_sslerror(e)
59 def read(self, n=1024):
60 """If n is provided, read n bytes from the SSL connection, otherwise read
61 until EOF. The return value is a string of the bytes read."""
63 return self.connection.read(n)
64 except _SSL.ZeroReturnError:
66 except _SSL.SysCallError as e:
67 raise _convert_to_sslerror(e)
70 """Writes the string s to the on the object's SSL connection.
71 The return value is the number of bytes written. """
73 return self.connection.write(s)
74 except _SSL.SysCallError as e:
75 raise _convert_to_sslerror(e)
78 """ Returns a string describing the server's certificate. Useful for debugging
79 purposes; do not parse the content of this string because its format can't be
80 parsed unambiguously. """
81 return str(self.connection.get_peer_certificate().get_subject())
84 """Returns a string describing the issuer of the server's certificate. Useful
85 for debugging purposes; do not parse the content of this string because its
86 format can't be parsed unambiguously."""
87 return str(self.connection.get_peer_certificate().get_issuer())