Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 1 | # Wrapper module for _socket, providing some additional facilities |
| 2 | # implemented in Python. |
| 3 | |
| 4 | """\ |
| 5 | This module provides socket operations and some related functions. |
| 6 | On Unix, it supports IP (Internet Protocol) and Unix domain sockets. |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 7 | On other systems, it only supports IP. Functions specific for a |
Martin v. Löwis | af484d5 | 2000-09-30 11:34:30 +0000 | [diff] [blame] | 8 | socket are available as methods of the socket object. |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 9 | |
| 10 | Functions: |
| 11 | |
| 12 | socket() -- create a new socket object |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 13 | socketpair() -- create a pair of new socket objects [*] |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 14 | fromfd() -- create a socket object from an open file descriptor [*] |
| 15 | gethostname() -- return the current hostname |
| 16 | gethostbyname() -- map a hostname to its IP number |
| 17 | gethostbyaddr() -- map an IP number or hostname to DNS info |
| 18 | getservbyname() -- map a service name and a protocol name to a port number |
| 19 | getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number |
| 20 | ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order |
| 21 | htons(), htonl() -- convert 16, 32 bit int from host to network byte order |
| 22 | inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format |
| 23 | inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89) |
| 24 | ssl() -- secure socket layer support (only available if configured) |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 25 | socket.getdefaulttimeout() -- get the default timeout value |
| 26 | socket.setdefaulttimeout() -- set the default timeout value |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 27 | |
| 28 | [*] not available on all platforms! |
| 29 | |
| 30 | Special objects: |
| 31 | |
| 32 | SocketType -- type object for socket objects |
| 33 | error -- exception raised for I/O errors |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 34 | has_ipv6 -- boolean value indicating if IPv6 is supported |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 35 | |
| 36 | Integer constants: |
| 37 | |
| 38 | AF_INET, AF_UNIX -- socket domains (first argument to socket() call) |
| 39 | SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument) |
| 40 | |
| 41 | Many other constants may be defined; these may be used in calls to |
| 42 | the setsockopt() and getsockopt() methods. |
| 43 | """ |
| 44 | |
Tim Peters | 18e6778 | 2002-02-17 04:25:24 +0000 | [diff] [blame] | 45 | import _socket |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 46 | from _socket import * |
Tim Peters | 18e6778 | 2002-02-17 04:25:24 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 48 | _have_ssl = False |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 49 | try: |
Guido van Rossum | de7cade | 2002-08-08 15:25:28 +0000 | [diff] [blame] | 50 | import _ssl |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 51 | from _ssl import * |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 52 | _have_ssl = True |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 53 | except ImportError: |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 54 | pass |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 55 | |
| 56 | import os, sys |
| 57 | |
Fred Drake | 70d566b | 2003-04-29 19:50:25 +0000 | [diff] [blame] | 58 | try: |
| 59 | from errno import EBADF |
| 60 | except ImportError: |
| 61 | EBADF = 9 |
| 62 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 63 | __all__ = ["getfqdn"] |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 64 | __all__.extend(os._get_exports_list(_socket)) |
Guido van Rossum | de7cade | 2002-08-08 15:25:28 +0000 | [diff] [blame] | 65 | if _have_ssl: |
| 66 | __all__.extend(os._get_exports_list(_ssl)) |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 68 | _realsocket = socket |
Martin v. Löwis | 1867f24 | 2003-06-14 13:30:53 +0000 | [diff] [blame] | 69 | if _have_ssl: |
| 70 | _realssl = ssl |
| 71 | def ssl(sock, keyfile=None, certfile=None): |
| 72 | if hasattr(sock, "_sock"): |
| 73 | sock = sock._sock |
| 74 | return _realssl(sock, keyfile, certfile) |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 75 | |
| 76 | # WSA error codes |
| 77 | if sys.platform.lower().startswith("win"): |
| 78 | errorTab = {} |
| 79 | errorTab[10004] = "The operation was interrupted." |
| 80 | errorTab[10009] = "A bad file handle was passed." |
| 81 | errorTab[10013] = "Permission denied." |
| 82 | errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT |
| 83 | errorTab[10022] = "An invalid operation was attempted." |
| 84 | errorTab[10035] = "The socket operation would block" |
| 85 | errorTab[10036] = "A blocking operation is already in progress." |
| 86 | errorTab[10048] = "The network address is in use." |
| 87 | errorTab[10054] = "The connection has been reset." |
| 88 | errorTab[10058] = "The network has been shut down." |
| 89 | errorTab[10060] = "The operation timed out." |
| 90 | errorTab[10061] = "Connection refused." |
| 91 | errorTab[10063] = "The name is too long." |
| 92 | errorTab[10064] = "The host is down." |
| 93 | errorTab[10065] = "The host is unreachable." |
Skip Montanaro | 64de1a4 | 2001-03-18 19:53:21 +0000 | [diff] [blame] | 94 | __all__.append("errorTab") |
Guido van Rossum | de7cade | 2002-08-08 15:25:28 +0000 | [diff] [blame] | 95 | |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def getfqdn(name=''): |
| 99 | """Get fully qualified domain name from name. |
| 100 | |
| 101 | An empty argument is interpreted as meaning the local host. |
| 102 | |
| 103 | First the hostname returned by gethostbyaddr() is checked, then |
| 104 | possibly existing aliases. In case no FQDN is available, hostname |
| 105 | is returned. |
| 106 | """ |
| 107 | name = name.strip() |
Peter Schneider-Kamp | 2d2785a | 2000-08-16 20:30:21 +0000 | [diff] [blame] | 108 | if not name or name == '0.0.0.0': |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 109 | name = gethostname() |
| 110 | try: |
| 111 | hostname, aliases, ipaddrs = gethostbyaddr(name) |
| 112 | except error: |
| 113 | pass |
| 114 | else: |
| 115 | aliases.insert(0, hostname) |
| 116 | for name in aliases: |
| 117 | if '.' in name: |
| 118 | break |
| 119 | else: |
| 120 | name = hostname |
| 121 | return name |
| 122 | |
| 123 | |
| 124 | # |
| 125 | # These classes are used by the socket() defined on Windows and BeOS |
| 126 | # platforms to provide a best-effort implementation of the cleanup |
| 127 | # semantics needed when sockets can't be dup()ed. |
| 128 | # |
| 129 | # These are not actually used on other platforms. |
| 130 | # |
| 131 | |
Guido van Rossum | e5e5059 | 2001-08-18 01:23:20 +0000 | [diff] [blame] | 132 | _socketmethods = ( |
| 133 | 'bind', 'connect', 'connect_ex', 'fileno', 'listen', |
| 134 | 'getpeername', 'getsockname', 'getsockopt', 'setsockopt', |
Skip Montanaro | c689918 | 2003-04-29 19:27:26 +0000 | [diff] [blame] | 135 | 'sendall', 'setblocking', |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 136 | 'settimeout', 'gettimeout', 'shutdown') |
Guido van Rossum | e5e5059 | 2001-08-18 01:23:20 +0000 | [diff] [blame] | 137 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 138 | if sys.platform == "riscos": |
| 139 | _socketmethods = _socketmethods + ('sleeptaskw',) |
| 140 | |
Guido van Rossum | 715f970 | 2002-08-08 18:11:36 +0000 | [diff] [blame] | 141 | class _closedsocket(object): |
| 142 | __slots__ = [] |
Skip Montanaro | c689918 | 2003-04-29 19:27:26 +0000 | [diff] [blame] | 143 | def _dummy(*args): |
Fred Drake | 70d566b | 2003-04-29 19:50:25 +0000 | [diff] [blame] | 144 | raise error(EBADF, 'Bad file descriptor') |
Skip Montanaro | c689918 | 2003-04-29 19:27:26 +0000 | [diff] [blame] | 145 | send = recv = sendto = recvfrom = __getattr__ = _dummy |
Guido van Rossum | 715f970 | 2002-08-08 18:11:36 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 147 | class _socketobject(object): |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 149 | __doc__ = _realsocket.__doc__ |
| 150 | |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 151 | __slots__ = ["_sock", "send", "recv", "sendto", "recvfrom", |
| 152 | "__weakref__"] |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 154 | def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None): |
| 155 | if _sock is None: |
Guido van Rossum | 7c3b634 | 2002-08-08 15:22:12 +0000 | [diff] [blame] | 156 | _sock = _realsocket(family, type, proto) |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 157 | self._sock = _sock |
Skip Montanaro | c689918 | 2003-04-29 19:27:26 +0000 | [diff] [blame] | 158 | self.send = self._sock.send |
| 159 | self.recv = self._sock.recv |
| 160 | self.sendto = self._sock.sendto |
| 161 | self.recvfrom = self._sock.recvfrom |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 162 | |
| 163 | def close(self): |
Guido van Rossum | 715f970 | 2002-08-08 18:11:36 +0000 | [diff] [blame] | 164 | self._sock = _closedsocket() |
Skip Montanaro | c689918 | 2003-04-29 19:27:26 +0000 | [diff] [blame] | 165 | self.send = self.recv = self.sendto = self.recvfrom = self._sock._dummy |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 166 | close.__doc__ = _realsocket.close.__doc__ |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 167 | |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 168 | def accept(self): |
| 169 | sock, addr = self._sock.accept() |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 170 | return _socketobject(_sock=sock), addr |
| 171 | accept.__doc__ = _realsocket.accept.__doc__ |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 172 | |
| 173 | def dup(self): |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 174 | """dup() -> socket object |
| 175 | |
| 176 | Return a new socket object connected to the same system resource.""" |
| 177 | return _socketobject(_sock=self._sock) |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 178 | |
| 179 | def makefile(self, mode='r', bufsize=-1): |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 180 | """makefile([mode[, bufsize]]) -> file object |
| 181 | |
| 182 | Return a regular file object corresponding to the socket. The mode |
| 183 | and bufsize arguments are as for the built-in open() function.""" |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 184 | return _fileobject(self._sock, mode, bufsize) |
| 185 | |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 186 | _s = ("def %s(self, *args): return self._sock.%s(*args)\n\n" |
| 187 | "%s.__doc__ = _realsocket.%s.__doc__\n") |
Guido van Rossum | e5e5059 | 2001-08-18 01:23:20 +0000 | [diff] [blame] | 188 | for _m in _socketmethods: |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 189 | exec _s % (_m, _m, _m, _m) |
Tim Peters | d7e8a0d | 2002-08-08 20:07:03 +0000 | [diff] [blame] | 190 | del _m, _s |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 191 | |
Skip Montanaro | 89feabc | 2003-03-30 04:54:24 +0000 | [diff] [blame] | 192 | socket = SocketType = _socketobject |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 194 | class _fileobject(object): |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 195 | """Faux file object attached to a socket object.""" |
| 196 | |
| 197 | default_bufsize = 8192 |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 198 | name = "<socket>" |
| 199 | |
| 200 | __slots__ = ["mode", "bufsize", "softspace", |
| 201 | # "closed" is a property, see below |
| 202 | "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf"] |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 204 | def __init__(self, sock, mode='rb', bufsize=-1): |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 205 | self._sock = sock |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 206 | self.mode = mode # Not actually used in this version |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 207 | if bufsize < 0: |
| 208 | bufsize = self.default_bufsize |
Guido van Rossum | 7c3b634 | 2002-08-08 15:22:12 +0000 | [diff] [blame] | 209 | self.bufsize = bufsize |
| 210 | self.softspace = False |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 211 | if bufsize == 0: |
| 212 | self._rbufsize = 1 |
| 213 | elif bufsize == 1: |
| 214 | self._rbufsize = self.default_bufsize |
| 215 | else: |
| 216 | self._rbufsize = bufsize |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 217 | self._wbufsize = bufsize |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 218 | self._rbuf = "" # A string |
| 219 | self._wbuf = [] # A list of strings |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 221 | def _getclosed(self): |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 222 | return self._sock is None |
Guido van Rossum | c18993f | 2002-08-08 15:16:20 +0000 | [diff] [blame] | 223 | closed = property(_getclosed, doc="True if the file is closed") |
| 224 | |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 225 | def close(self): |
| 226 | try: |
| 227 | if self._sock: |
| 228 | self.flush() |
| 229 | finally: |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 230 | self._sock = None |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 231 | |
| 232 | def __del__(self): |
Guido van Rossum | 65f8ced | 2003-05-29 14:36:57 +0000 | [diff] [blame] | 233 | try: |
| 234 | self.close() |
| 235 | except: |
| 236 | # close() may fail if __init__ didn't complete |
| 237 | pass |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 238 | |
| 239 | def flush(self): |
| 240 | if self._wbuf: |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 241 | buffer = "".join(self._wbuf) |
| 242 | self._wbuf = [] |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 243 | self._sock.sendall(buffer) |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 244 | |
Neal Norwitz | 2b34290 | 2002-06-13 22:18:39 +0000 | [diff] [blame] | 245 | def fileno(self): |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 246 | return self._sock.fileno() |
| 247 | |
| 248 | def write(self, data): |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 249 | data = str(data) # XXX Should really reject non-string non-buffers |
| 250 | if not data: |
| 251 | return |
| 252 | self._wbuf.append(data) |
| 253 | if (self._wbufsize == 0 or |
| 254 | self._wbufsize == 1 and '\n' in data or |
| 255 | self._get_wbuf_len() >= self._wbufsize): |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 256 | self.flush() |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 257 | |
| 258 | def writelines(self, list): |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 259 | # XXX We could do better here for very long lists |
| 260 | # XXX Should really reject non-string non-buffers |
| 261 | self._wbuf.extend(filter(None, map(str, list))) |
| 262 | if (self._wbufsize <= 1 or |
| 263 | self._get_wbuf_len() >= self._wbufsize): |
| 264 | self.flush() |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 266 | def _get_wbuf_len(self): |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 267 | buf_len = 0 |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 268 | for x in self._wbuf: |
| 269 | buf_len += len(x) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 270 | return buf_len |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 271 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 272 | def read(self, size=-1): |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 273 | data = self._rbuf |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 274 | if size < 0: |
| 275 | # Read until EOF |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 276 | buffers = [] |
| 277 | if data: |
| 278 | buffers.append(data) |
| 279 | self._rbuf = "" |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 280 | if self._rbufsize <= 1: |
| 281 | recv_size = self.default_bufsize |
| 282 | else: |
| 283 | recv_size = self._rbufsize |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 284 | while True: |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 285 | data = self._sock.recv(recv_size) |
| 286 | if not data: |
| 287 | break |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 288 | buffers.append(data) |
| 289 | return "".join(buffers) |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 290 | else: |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 291 | # Read until size bytes or EOF seen, whichever comes first |
| 292 | buf_len = len(data) |
| 293 | if buf_len >= size: |
| 294 | self._rbuf = data[size:] |
| 295 | return data[:size] |
| 296 | buffers = [] |
| 297 | if data: |
| 298 | buffers.append(data) |
| 299 | self._rbuf = "" |
| 300 | while True: |
| 301 | left = size - buf_len |
| 302 | recv_size = max(self._rbufsize, left) |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 303 | data = self._sock.recv(recv_size) |
| 304 | if not data: |
| 305 | break |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 306 | buffers.append(data) |
| 307 | n = len(data) |
| 308 | if n >= left: |
| 309 | self._rbuf = data[left:] |
| 310 | buffers[-1] = data[:left] |
| 311 | break |
| 312 | buf_len += n |
| 313 | return "".join(buffers) |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 315 | def readline(self, size=-1): |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 316 | data = self._rbuf |
| 317 | if size < 0: |
| 318 | # Read until \n or EOF, whichever comes first |
Guido van Rossum | 48b7969 | 2002-08-08 17:34:19 +0000 | [diff] [blame] | 319 | if self._rbufsize <= 1: |
| 320 | # Speed up unbuffered case |
| 321 | assert data == "" |
| 322 | buffers = [] |
| 323 | recv = self._sock.recv |
| 324 | while data != "\n": |
| 325 | data = recv(1) |
| 326 | if not data: |
| 327 | break |
| 328 | buffers.append(data) |
| 329 | return "".join(buffers) |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 330 | nl = data.find('\n') |
| 331 | if nl >= 0: |
| 332 | nl += 1 |
| 333 | self._rbuf = data[nl:] |
| 334 | return data[:nl] |
| 335 | buffers = [] |
| 336 | if data: |
| 337 | buffers.append(data) |
| 338 | self._rbuf = "" |
| 339 | while True: |
| 340 | data = self._sock.recv(self._rbufsize) |
| 341 | if not data: |
| 342 | break |
| 343 | buffers.append(data) |
| 344 | nl = data.find('\n') |
| 345 | if nl >= 0: |
| 346 | nl += 1 |
| 347 | self._rbuf = data[nl:] |
| 348 | buffers[-1] = data[:nl] |
| 349 | break |
| 350 | return "".join(buffers) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 351 | else: |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 352 | # Read until size bytes or \n or EOF seen, whichever comes first |
| 353 | nl = data.find('\n', 0, size) |
| 354 | if nl >= 0: |
| 355 | nl += 1 |
| 356 | self._rbuf = data[nl:] |
| 357 | return data[:nl] |
| 358 | buf_len = len(data) |
| 359 | if buf_len >= size: |
| 360 | self._rbuf = data[size:] |
| 361 | return data[:size] |
| 362 | buffers = [] |
| 363 | if data: |
| 364 | buffers.append(data) |
| 365 | self._rbuf = "" |
| 366 | while True: |
| 367 | data = self._sock.recv(self._rbufsize) |
| 368 | if not data: |
| 369 | break |
| 370 | buffers.append(data) |
| 371 | left = size - buf_len |
| 372 | nl = data.find('\n', 0, left) |
| 373 | if nl >= 0: |
| 374 | nl += 1 |
| 375 | self._rbuf = data[nl:] |
| 376 | buffers[-1] = data[:nl] |
| 377 | break |
| 378 | n = len(data) |
| 379 | if n >= left: |
| 380 | self._rbuf = data[left:] |
| 381 | buffers[-1] = data[:left] |
| 382 | break |
| 383 | buf_len += n |
| 384 | return "".join(buffers) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 385 | |
| 386 | def readlines(self, sizehint=0): |
Martin v. Löwis | 6df27f8 | 2000-09-19 11:25:58 +0000 | [diff] [blame] | 387 | total = 0 |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 388 | list = [] |
Guido van Rossum | fb3deec | 2002-08-08 17:16:09 +0000 | [diff] [blame] | 389 | while True: |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 390 | line = self.readline() |
Neal Norwitz | 2b34290 | 2002-06-13 22:18:39 +0000 | [diff] [blame] | 391 | if not line: |
| 392 | break |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 393 | list.append(line) |
Martin v. Löwis | 6df27f8 | 2000-09-19 11:25:58 +0000 | [diff] [blame] | 394 | total += len(line) |
| 395 | if sizehint and total >= sizehint: |
| 396 | break |
Fred Drake | a6070f0 | 2000-08-16 14:14:32 +0000 | [diff] [blame] | 397 | return list |
Guido van Rossum | 443fec3 | 2002-08-08 01:02:16 +0000 | [diff] [blame] | 398 | |
| 399 | # Iterator protocols |
| 400 | |
| 401 | def __iter__(self): |
| 402 | return self |
| 403 | |
| 404 | def next(self): |
| 405 | line = self.readline() |
| 406 | if not line: |
| 407 | raise StopIteration |
| 408 | return line |