1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Wrapper module for _socket, providing some additional facilities
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# implemented in Python.
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""\
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis module provides socket operations and some related functions.
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepOn Unix, it supports IP (Internet Protocol) and Unix domain sockets.
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepOn other systems, it only supports IP. Functions specific for a
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsocket are available as methods of the socket object.
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepFunctions:
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsocket() -- create a new socket object
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsocketpair() -- create a pair of new socket objects [*]
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfromfd() -- create a socket object from an open file descriptor [*]
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepgethostname() -- return the current hostname
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepgethostbyname() -- map a hostname to its IP number
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepgethostbyaddr() -- map an IP number or hostname to DNS info
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepgetservbyname() -- map a service name and a protocol name to a port number
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepgetprotobyname() -- map a protocol name (e.g. 'tcp') to a number
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoephtons(), htonl() -- convert 16, 32 bit int from host to network byte order
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepinet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepinet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepssl() -- secure socket layer support (only available if configured)
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsocket.getdefaulttimeout() -- get the default timeout value
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsocket.setdefaulttimeout() -- set the default timeout value
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcreate_connection() -- connects to an address, with an optional timeout and
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       optional source address.
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep [*] not available on all platforms!
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSpecial objects:
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSocketType -- type object for socket objects
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeperror -- exception raised for I/O errors
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoephas_ipv6 -- boolean value indicating if IPv6 is supported
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepInteger constants:
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepAF_INET, AF_UNIX -- socket domains (first argument to socket() call)
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepMany other constants may be defined; these may be used in calls to
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthe setsockopt() and getsockopt() methods.
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport _socket
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom _socket import *
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom functools import partial
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom types import MethodType
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import _ssl
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept ImportError:
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # no SSL support
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    pass
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelse:
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def ssl(sock, keyfile=None, certfile=None):
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # we do an internal import here because the ssl
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # module imports the socket module
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import ssl as _realssl
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        warnings.warn("socket.ssl() is deprecated.  Use ssl.wrap_socket() instead.",
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                      DeprecationWarning, stacklevel=2)
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return _realssl.sslwrap_simple(sock, keyfile, certfile)
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # we need to import the same constants we used to...
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from _ssl import SSLError as sslerror
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from _ssl import \
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         RAND_add, \
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         RAND_egd, \
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         RAND_status, \
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_ZERO_RETURN, \
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_WANT_READ, \
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_WANT_WRITE, \
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_WANT_X509_LOOKUP, \
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_SYSCALL, \
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_SSL, \
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_WANT_CONNECT, \
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_EOF, \
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         SSL_ERROR_INVALID_ERROR_CODE
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport os, sys, warnings
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from cStringIO import StringIO
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept ImportError:
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from StringIO import StringIO
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import errno
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept ImportError:
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errno = None
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepEBADF = getattr(errno, 'EBADF', 9)
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepEINTR = getattr(errno, 'EINTR', 4)
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__ = ["getfqdn", "create_connection"]
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__.extend(os._get_exports_list(_socket))
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep_realsocket = socket
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# WSA error codes
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif sys.platform.lower().startswith("win"):
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab = {}
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10004] = "The operation was interrupted."
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10009] = "A bad file handle was passed."
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10013] = "Permission denied."
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10022] = "An invalid operation was attempted."
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10035] = "The socket operation would block"
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10036] = "A blocking operation is already in progress."
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10048] = "The network address is in use."
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10054] = "The connection has been reset."
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10058] = "The network has been shut down."
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10060] = "The operation timed out."
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10061] = "Connection refused."
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10063] = "The name is too long."
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10064] = "The host is down."
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errorTab[10065] = "The host is unreachable."
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __all__.append("errorTab")
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef getfqdn(name=''):
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """Get fully qualified domain name from name.
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    An empty argument is interpreted as meaning the local host.
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    First the hostname returned by gethostbyaddr() is checked, then
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    possibly existing aliases. In case no FQDN is available, hostname
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from gethostname() is returned.
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    name = name.strip()
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not name or name == '0.0.0.0':
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        name = gethostname()
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hostname, aliases, ipaddrs = gethostbyaddr(name)
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except error:
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    else:
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        aliases.insert(0, hostname)
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for name in aliases:
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if '.' in name:
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                break
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            name = hostname
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return name
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep_socketmethods = (
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'bind', 'connect', 'connect_ex', 'fileno', 'listen',
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'sendall', 'setblocking',
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'settimeout', 'gettimeout', 'shutdown')
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif os.name == "nt":
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _socketmethods = _socketmethods + ('ioctl',)
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif sys.platform == "riscos":
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _socketmethods = _socketmethods + ('sleeptaskw',)
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# All the method names that must be delegated to either the real socket
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# object or the _closedsocket object.
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep_delegate_methods = ("recv", "recvfrom", "recv_into", "recvfrom_into",
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "send", "sendto")
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _closedsocket(object):
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __slots__ = []
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _dummy(*args):
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise error(EBADF, 'Bad file descriptor')
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # All _delegate_methods must also be initialized here.
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __getattr__ = _dummy
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Wrapper around platform socket objects. This implements
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# a platform-independent dup() functionality. The
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# implementation currently relies on reference counting
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# to close the underlying socket object.
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _socketobject(object):
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __doc__ = _realsocket.__doc__
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __slots__ = ["_sock", "__weakref__"] + list(_delegate_methods)
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if _sock is None:
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            _sock = _realsocket(family, type, proto)
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._sock = _sock
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for method in _delegate_methods:
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            setattr(self, method, getattr(_sock, method))
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def close(self, _closedsocket=_closedsocket,
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep              _delegate_methods=_delegate_methods, setattr=setattr):
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # This function should not reference any globals. See issue #808164.
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._sock = _closedsocket()
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dummy = self._sock._dummy
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for method in _delegate_methods:
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            setattr(self, method, dummy)
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    close.__doc__ = _realsocket.close.__doc__
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def accept(self):
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sock, addr = self._sock.accept()
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return _socketobject(_sock=sock), addr
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    accept.__doc__ = _realsocket.accept.__doc__
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def dup(self):
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """dup() -> socket object
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Return a new socket object connected to the same system resource."""
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return _socketobject(_sock=self._sock)
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def makefile(self, mode='r', bufsize=-1):
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """makefile([mode[, bufsize]]) -> file object
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Return a regular file object corresponding to the socket.  The mode
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        and bufsize arguments are as for the built-in open() function."""
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return _fileobject(self._sock, mode, bufsize)
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    family = property(lambda self: self._sock.family, doc="the socket family")
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    type = property(lambda self: self._sock.type, doc="the socket type")
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    proto = property(lambda self: self._sock.proto, doc="the socket protocol")
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef meth(name,self,*args):
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return getattr(self._sock,name)(*args)
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfor _m in _socketmethods:
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    p = partial(meth,_m)
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    p.__name__ = _m
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    p.__doc__ = getattr(_realsocket,_m).__doc__
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    m = MethodType(p,None,_socketobject)
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    setattr(_socketobject,_m,m)
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsocket = SocketType = _socketobject
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _fileobject(object):
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """Faux file object attached to a socket object."""
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    default_bufsize = 8192
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    name = "<socket>"
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __slots__ = ["mode", "bufsize", "softspace",
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 # "closed" is a property, see below
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf", "_wbuf_len",
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 "_close"]
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, sock, mode='rb', bufsize=-1, close=False):
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._sock = sock
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.mode = mode # Not actually used in this version
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if bufsize < 0:
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            bufsize = self.default_bufsize
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.bufsize = bufsize
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.softspace = False
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # _rbufsize is the suggested recv buffer size.  It is *strictly*
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # obeyed within readline() for recv calls.  If it is larger than
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # default_bufsize it will be used for recv calls within read().
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if bufsize == 0:
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._rbufsize = 1
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        elif bufsize == 1:
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._rbufsize = self.default_bufsize
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._rbufsize = bufsize
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._wbufsize = bufsize
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # We use StringIO for the read buffer to avoid holding a list
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # of variously sized string objects which have been known to
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # fragment the heap due to how they are malloc()ed and often
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # realloc()ed down much smaller than their original allocation.
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._rbuf = StringIO()
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._wbuf = [] # A list of strings
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._wbuf_len = 0
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._close = close
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _getclosed(self):
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self._sock is None
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    closed = property(_getclosed, doc="True if the file is closed")
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def close(self):
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if self._sock:
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.flush()
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if self._close:
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._sock.close()
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._sock = None
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __del__(self):
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.close()
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # close() may fail if __init__ didn't complete
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def flush(self):
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self._wbuf:
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            data = "".join(self._wbuf)
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._wbuf = []
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._wbuf_len = 0
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            buffer_size = max(self._rbufsize, self.default_bufsize)
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            data_size = len(data)
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            write_offset = 0
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            view = memoryview(data)
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                while write_offset < data_size:
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self._sock.sendall(view[write_offset:write_offset+buffer_size])
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    write_offset += buffer_size
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            finally:
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if write_offset < data_size:
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    remainder = data[write_offset:]
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del view, data  # explicit free
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self._wbuf.append(remainder)
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self._wbuf_len = len(remainder)
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def fileno(self):
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self._sock.fileno()
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def write(self, data):
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = str(data) # XXX Should really reject non-string non-buffers
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not data:
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._wbuf.append(data)
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._wbuf_len += len(data)
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if (self._wbufsize == 0 or
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            (self._wbufsize == 1 and '\n' in data) or
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            (self._wbufsize > 1 and self._wbuf_len >= self._wbufsize)):
324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.flush()
325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def writelines(self, list):
327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX We could do better here for very long lists
328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX Should really reject non-string non-buffers
329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lines = filter(None, map(str, list))
330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._wbuf_len += sum(map(len, lines))
331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._wbuf.extend(lines)
332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if (self._wbufsize <= 1 or
333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._wbuf_len >= self._wbufsize):
334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.flush()
335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def read(self, size=-1):
337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Use max, disallow tiny reads in a loop as they are very inefficient.
338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # We never leave read() with any leftover data from a new recv() call
339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # in our internal buffer.
340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        rbufsize = max(self._rbufsize, self.default_bufsize)
341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Our use of StringIO rather than lists of string objects returned by
342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # recv() minimizes memory usage and fragmentation that occurs when
343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # rbufsize is large compared to the typical return value of recv().
344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        buf = self._rbuf
345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        buf.seek(0, 2)  # seek end
346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if size < 0:
347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Read until EOF
348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._rbuf = StringIO()  # reset _rbuf.  we consume it via buf.
349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while True:
350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    data = self._sock.recv(rbufsize)
352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                except error, e:
353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if e.args[0] == EINTR:
354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        continue
355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise
356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if not data:
357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    break
358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf.write(data)
359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return buf.getvalue()
360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Read until size bytes or EOF seen, whichever comes first
362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            buf_len = buf.tell()
363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if buf_len >= size:
364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # Already have size bytes in our buffer?  Extract and return.
365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf.seek(0)
366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                rv = buf.read(size)
367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rbuf = StringIO()
368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rbuf.write(buf.read())
369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return rv
370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._rbuf = StringIO()  # reset _rbuf.  we consume it via buf.
372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while True:
373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                left = size - buf_len
374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # recv() will malloc the amount of memory given as its
375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # parameter even though it often returns much less data
376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # than that.  The returned data string is short lived
377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # as we copy it into a StringIO and free it.  This avoids
378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # fragmentation issues on many platforms.
379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    data = self._sock.recv(left)
381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                except error, e:
382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if e.args[0] == EINTR:
383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        continue
384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise
385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if not data:
386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    break
387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                n = len(data)
388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if n == size and not buf_len:
389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # Shortcut.  Avoid buffer data copies when:
390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # - We have no data in our buffer.
391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # AND
392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # - Our call to recv returned exactly the
393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    #   number of bytes we were asked to read.
394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return data
395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if n == left:
396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    buf.write(data)
397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del data  # explicit free
398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    break
399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                assert n <= left, "recv(%d) returned %d bytes" % (left, n)
400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf.write(data)
401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf_len += n
402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del data  # explicit free
403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                #assert buf_len == buf.tell()
404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return buf.getvalue()
405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def readline(self, size=-1):
407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        buf = self._rbuf
408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        buf.seek(0, 2)  # seek end
409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if buf.tell() > 0:
410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # check if we already have it in our buffer
411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            buf.seek(0)
412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            bline = buf.readline(size)
413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if bline.endswith('\n') or len(bline) == size:
414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rbuf = StringIO()
415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rbuf.write(buf.read())
416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return bline
417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            del bline
418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if size < 0:
419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Read until \n or EOF, whichever comes first
420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if self._rbufsize <= 1:
421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # Speed up unbuffered case
422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf.seek(0)
423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buffers = [buf.read()]
424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rbuf = StringIO()  # reset _rbuf.  we consume it via buf.
425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                data = None
426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                recv = self._sock.recv
427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                while True:
428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    try:
429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        while data != "\n":
430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            data = recv(1)
431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            if not data:
432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                break
433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            buffers.append(data)
434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    except error, e:
435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # The try..except to catch EINTR was moved outside the
436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # recv loop to avoid the per byte overhead.
437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        if e.args[0] == EINTR:
438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            continue
439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        raise
440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    break
441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "".join(buffers)
442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            buf.seek(0, 2)  # seek end
444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._rbuf = StringIO()  # reset _rbuf.  we consume it via buf.
445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while True:
446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    data = self._sock.recv(self._rbufsize)
448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                except error, e:
449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if e.args[0] == EINTR:
450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        continue
451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise
452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if not data:
453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    break
454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                nl = data.find('\n')
455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if nl >= 0:
456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    nl += 1
457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    buf.write(data[:nl])
458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self._rbuf.write(data[nl:])
459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del data
460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    break
461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf.write(data)
462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return buf.getvalue()
463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Read until size bytes or \n or EOF seen, whichever comes first
465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            buf.seek(0, 2)  # seek end
466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            buf_len = buf.tell()
467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if buf_len >= size:
468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf.seek(0)
469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                rv = buf.read(size)
470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rbuf = StringIO()
471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rbuf.write(buf.read())
472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return rv
473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._rbuf = StringIO()  # reset _rbuf.  we consume it via buf.
474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while True:
475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    data = self._sock.recv(self._rbufsize)
477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                except error, e:
478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if e.args[0] == EINTR:
479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        continue
480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise
481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if not data:
482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    break
483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                left = size - buf_len
484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # did we just receive a newline?
485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                nl = data.find('\n', 0, left)
486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if nl >= 0:
487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    nl += 1
488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # save the excess data to _rbuf
489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self._rbuf.write(data[nl:])
490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if buf_len:
491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        buf.write(data[:nl])
492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        break
493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    else:
494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # Shortcut.  Avoid data copy through buf when returning
495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # a substring of our first recv().
496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return data[:nl]
497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                n = len(data)
498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if n == size and not buf_len:
499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # Shortcut.  Avoid data copy through buf when
500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # returning exactly all of our first recv().
501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return data
502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if n >= left:
503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    buf.write(data[:left])
504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self._rbuf.write(data[left:])
505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    break
506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf.write(data)
507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                buf_len += n
508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                #assert buf_len == buf.tell()
509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return buf.getvalue()
510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def readlines(self, sizehint=0):
512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        total = 0
513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        list = []
514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while True:
515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            line = self.readline()
516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if not line:
517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                break
518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            list.append(line)
519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            total += len(line)
520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if sizehint and total >= sizehint:
521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                break
522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return list
523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Iterator protocols
525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __iter__(self):
527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self
528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def next(self):
530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        line = self.readline()
531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not line:
532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raise StopIteration
533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return line
534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep_GLOBAL_DEFAULT_TIMEOUT = object()
536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                      source_address=None):
539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """Connect to *address* and return the socket object.
540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Convenience function.  Connect to *address* (a 2-tuple ``(host,
542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    port)``) and return the socket object.  Passing the optional
543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    *timeout* parameter will set the timeout on the socket instance
544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    before attempting to connect.  If no *timeout* is supplied, the
545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    global default timeout setting returned by :func:`getdefaulttimeout`
546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    is used.  If *source_address* is set it must be a tuple of (host, port)
547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for the socket to bind as a source address before making the connection.
548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    An host of '' or port 0 tells the OS to use the default.
549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    host, port = address
552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    err = None
553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        af, socktype, proto, canonname, sa = res
555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sock = None
556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sock = socket(af, socktype, proto)
558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                sock.settimeout(timeout)
560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if source_address:
561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                sock.bind(source_address)
562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sock.connect(sa)
563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return sock
564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except error as _:
566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            err = _
567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if sock is not None:
568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                sock.close()
569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if err is not None:
571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise err
572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    else:
573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise error("getaddrinfo returns an empty list")
574