Add python-eventlet 0.16.1
[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 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
17
18 try:
19     __original_fromfd__ = __socket.fromfd
20
21     def fromfd(*args):
22         return socket(__original_fromfd__(*args))
23 except AttributeError:
24     pass
25
26 try:
27     __original_socketpair__ = __socket.socketpair
28
29     def socketpair(*args):
30         one, two = __original_socketpair__(*args)
31         return socket(one), socket(two)
32 except AttributeError:
33     pass
34
35
36 def _convert_to_sslerror(ex):
37     """ Transliterates SSL.SysCallErrors to socket.sslerrors"""
38     return sslerror((ex.args[0], ex.args[1]))
39
40
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. """
44
45     def __init__(self, green_ssl_obj):
46         """ Should only be called by a 'green' socket.ssl """
47         self.connection = green_ssl_obj
48         try:
49             # if it's already connected, do the handshake
50             self.connection.getpeername()
51         except:
52             pass
53         else:
54             try:
55                 self.connection.do_handshake()
56             except _SSL.SysCallError as e:
57                 raise _convert_to_sslerror(e)
58
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."""
62         try:
63             return self.connection.read(n)
64         except _SSL.ZeroReturnError:
65             return ''
66         except _SSL.SysCallError as e:
67             raise _convert_to_sslerror(e)
68
69     def write(self, s):
70         """Writes the string s to the on the object's SSL connection.
71         The return value is the number of bytes written. """
72         try:
73             return self.connection.write(s)
74         except _SSL.SysCallError as e:
75             raise _convert_to_sslerror(e)
76
77     def server(self):
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())
82
83     def issuer(self):
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())