Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 1 | # -*- Mode: Python -*- |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 2 | # Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 3 | # Author: Sam Rushing <rushing@nightmare.com> |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 4 | |
| 5 | # ====================================================================== |
| 6 | # Copyright 1996 by Sam Rushing |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 7 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 8 | # All Rights Reserved |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 9 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 10 | # Permission to use, copy, modify, and distribute this software and |
| 11 | # its documentation for any purpose and without fee is hereby |
| 12 | # granted, provided that the above copyright notice appear in all |
| 13 | # copies and that both that copyright notice and this permission |
| 14 | # notice appear in supporting documentation, and that the name of Sam |
| 15 | # Rushing not be used in advertising or publicity pertaining to |
| 16 | # distribution of the software without specific, written prior |
| 17 | # permission. |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 18 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 19 | # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 20 | # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
| 21 | # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 22 | # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 23 | # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 25 | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 26 | # ====================================================================== |
| 27 | |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 28 | """Basic infrastructure for asynchronous socket service clients and servers. |
| 29 | |
| 30 | There are only two ways to have a program on a single processor do "more |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 31 | than one thing at a time". Multi-threaded programming is the simplest and |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 32 | most popular way to do it, but there is another very different technique, |
| 33 | that lets you have nearly all the advantages of multi-threading, without |
| 34 | actually using multiple threads. it's really only practical if your program |
| 35 | is largely I/O bound. If your program is CPU bound, then pre-emptive |
| 36 | scheduled threads are probably what you really need. Network servers are |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 37 | rarely CPU-bound, however. |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 38 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 39 | If your operating system supports the select() system call in its I/O |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 40 | library (and nearly all do), then you can use it to juggle multiple |
| 41 | communication channels at once; doing other work while your I/O is taking |
| 42 | place in the "background." Although this strategy can seem strange and |
| 43 | complex, especially at first, it is in many ways easier to understand and |
| 44 | control than multi-threaded programming. The module documented here solves |
| 45 | many of the difficult problems for you, making the task of building |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 46 | sophisticated high-performance network servers and clients a snap. |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 47 | """ |
| 48 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 49 | import select |
| 50 | import socket |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 51 | import sys |
Thomas Heller | d8ce87a | 2002-09-24 17:30:31 +0000 | [diff] [blame] | 52 | import time |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 53 | import warnings |
| 54 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 55 | import os |
Giampaolo Rodolà | 76fc8c7 | 2010-08-23 21:53:41 +0000 | [diff] [blame] | 56 | from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 57 | ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, errorcode |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 58 | |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 59 | try: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 60 | socket_map |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 61 | except NameError: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 62 | socket_map = {} |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 63 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 64 | def _strerror(err): |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 65 | try: |
Giampaolo Rodolà | 82e02b5 | 2010-05-18 20:11:58 +0000 | [diff] [blame] | 66 | return os.strerror(err) |
| 67 | except (ValueError, OverflowError, NameError): |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 68 | if err in errorcode: |
| 69 | return errorcode[err] |
| 70 | return "Unknown error %s" %err |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 71 | |
Neal Norwitz | 4ce69a5 | 2005-09-01 00:45:28 +0000 | [diff] [blame] | 72 | class ExitNow(Exception): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 73 | pass |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 74 | |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 75 | _reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit) |
| 76 | |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 77 | def read(obj): |
| 78 | try: |
| 79 | obj.handle_read_event() |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 80 | except _reraised_exceptions: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 81 | raise |
| 82 | except: |
| 83 | obj.handle_error() |
| 84 | |
| 85 | def write(obj): |
| 86 | try: |
| 87 | obj.handle_write_event() |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 88 | except _reraised_exceptions: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 89 | raise |
| 90 | except: |
| 91 | obj.handle_error() |
| 92 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 93 | def _exception(obj): |
Andrew M. Kuchling | 0fff6c8 | 2004-07-10 17:36:11 +0000 | [diff] [blame] | 94 | try: |
| 95 | obj.handle_expt_event() |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 96 | except _reraised_exceptions: |
Andrew M. Kuchling | 0fff6c8 | 2004-07-10 17:36:11 +0000 | [diff] [blame] | 97 | raise |
| 98 | except: |
| 99 | obj.handle_error() |
| 100 | |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 101 | def readwrite(obj, flags): |
| 102 | try: |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 103 | if flags & select.POLLIN: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 104 | obj.handle_read_event() |
| 105 | if flags & select.POLLOUT: |
| 106 | obj.handle_write_event() |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 107 | if flags & select.POLLPRI: |
| 108 | obj.handle_expt_event() |
Josiah Carlson | 0abc64d | 2009-06-03 19:48:02 +0000 | [diff] [blame] | 109 | if flags & (select.POLLHUP | select.POLLERR | select.POLLNVAL): |
| 110 | obj.handle_close() |
| 111 | except socket.error as e: |
| 112 | if e.args[0] not in (EBADF, ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED): |
| 113 | obj.handle_error() |
| 114 | else: |
| 115 | obj.handle_close() |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 116 | except _reraised_exceptions: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 117 | raise |
| 118 | except: |
| 119 | obj.handle_error() |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 121 | def poll(timeout=0.0, map=None): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 122 | if map is None: |
| 123 | map = socket_map |
| 124 | if map: |
| 125 | r = []; w = []; e = [] |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 126 | for fd, obj in list(map.items()): |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 127 | is_r = obj.readable() |
| 128 | is_w = obj.writable() |
| 129 | if is_r: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 130 | r.append(fd) |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 131 | if is_w: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 132 | w.append(fd) |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 133 | if is_r or is_w: |
| 134 | e.append(fd) |
Thomas Heller | d8ce87a | 2002-09-24 17:30:31 +0000 | [diff] [blame] | 135 | if [] == r == w == e: |
| 136 | time.sleep(timeout) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 137 | return |
| 138 | |
| 139 | try: |
| 140 | r, w, e = select.select(r, w, e, timeout) |
| 141 | except select.error as err: |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 142 | if err.args[0] != EINTR: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 143 | raise |
| 144 | else: |
| 145 | return |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 146 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 147 | for fd in r: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 148 | obj = map.get(fd) |
| 149 | if obj is None: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 150 | continue |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 151 | read(obj) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 152 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 153 | for fd in w: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 154 | obj = map.get(fd) |
| 155 | if obj is None: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 156 | continue |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 157 | write(obj) |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 158 | |
Andrew M. Kuchling | 0fff6c8 | 2004-07-10 17:36:11 +0000 | [diff] [blame] | 159 | for fd in e: |
| 160 | obj = map.get(fd) |
| 161 | if obj is None: |
| 162 | continue |
| 163 | _exception(obj) |
| 164 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 165 | def poll2(timeout=0.0, map=None): |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 166 | # Use the poll() support added to the select module in Python 2.0 |
| 167 | if map is None: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 168 | map = socket_map |
Martin v. Löwis | f6cc07c | 2001-09-19 17:31:47 +0000 | [diff] [blame] | 169 | if timeout is not None: |
| 170 | # timeout is in milliseconds |
| 171 | timeout = int(timeout*1000) |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 172 | pollster = select.poll() |
| 173 | if map: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 174 | for fd, obj in list(map.items()): |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 175 | flags = 0 |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 176 | if obj.readable(): |
Andrew M. Kuchling | 6fe93cd | 2004-07-07 12:23:53 +0000 | [diff] [blame] | 177 | flags |= select.POLLIN | select.POLLPRI |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 178 | if obj.writable(): |
Andrew M. Kuchling | 6fe93cd | 2004-07-07 12:23:53 +0000 | [diff] [blame] | 179 | flags |= select.POLLOUT |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 180 | if flags: |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 181 | # Only check for exceptions if object was either readable |
| 182 | # or writable. |
| 183 | flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 184 | pollster.register(fd, flags) |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 185 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 186 | r = pollster.poll(timeout) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 187 | except select.error as err: |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 188 | if err.args[0] != EINTR: |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 189 | raise |
| 190 | r = [] |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 191 | for fd, flags in r: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 192 | obj = map.get(fd) |
| 193 | if obj is None: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 194 | continue |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 195 | readwrite(obj, flags) |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 196 | |
Andrew M. Kuchling | 6c2871e | 2003-10-22 14:38:27 +0000 | [diff] [blame] | 197 | poll3 = poll2 # Alias for backward compatibility |
| 198 | |
Michael W. Hudson | d5cf143 | 2004-06-30 09:02:33 +0000 | [diff] [blame] | 199 | def loop(timeout=30.0, use_poll=False, map=None, count=None): |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 200 | if map is None: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 201 | map = socket_map |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 202 | |
Andrew M. Kuchling | 6c2871e | 2003-10-22 14:38:27 +0000 | [diff] [blame] | 203 | if use_poll and hasattr(select, 'poll'): |
| 204 | poll_fun = poll2 |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 205 | else: |
| 206 | poll_fun = poll |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 207 | |
Michael W. Hudson | d5cf143 | 2004-06-30 09:02:33 +0000 | [diff] [blame] | 208 | if count is None: |
| 209 | while map: |
| 210 | poll_fun(timeout, map) |
| 211 | |
| 212 | else: |
| 213 | while map and count > 0: |
| 214 | poll_fun(timeout, map) |
| 215 | count = count - 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 216 | |
| 217 | class dispatcher: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 218 | |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 219 | debug = False |
| 220 | connected = False |
| 221 | accepting = False |
| 222 | closing = False |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 223 | addr = None |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 224 | ignore_log_types = frozenset(['warning']) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 226 | def __init__(self, sock=None, map=None): |
Andrew M. Kuchling | f9ca409 | 2003-10-22 13:48:27 +0000 | [diff] [blame] | 227 | if map is None: |
| 228 | self._map = socket_map |
| 229 | else: |
| 230 | self._map = map |
| 231 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 232 | self._fileno = None |
| 233 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 234 | if sock: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 235 | # Set to nonblocking just to make sure for cases where we |
| 236 | # get a socket from a blocking source. |
| 237 | sock.setblocking(0) |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 238 | self.set_socket(sock, map) |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 239 | self.connected = True |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 240 | # The constructor no longer requires that the socket |
| 241 | # passed be connected. |
Jeremy Hylton | 2a05bc7 | 2001-12-14 16:15:11 +0000 | [diff] [blame] | 242 | try: |
| 243 | self.addr = sock.getpeername() |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 244 | except socket.error as err: |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 245 | if err.args[0] == ENOTCONN: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 246 | # To handle the case where we got an unconnected |
| 247 | # socket. |
| 248 | self.connected = False |
| 249 | else: |
| 250 | # The socket is broken in some unknown way, alert |
| 251 | # the user and remove it from the map (to prevent |
| 252 | # polling of broken sockets). |
| 253 | self.del_channel(map) |
| 254 | raise |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 255 | else: |
| 256 | self.socket = None |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 258 | def __repr__(self): |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 259 | status = [self.__class__.__module__+"."+self.__class__.__name__] |
| 260 | if self.accepting and self.addr: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 261 | status.append('listening') |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 262 | elif self.connected: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 263 | status.append('connected') |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 264 | if self.addr is not None: |
| 265 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 266 | status.append('%s:%d' % self.addr) |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 267 | except TypeError: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 268 | status.append(repr(self.addr)) |
| 269 | return '<%s at %#x>' % (' '.join(status), id(self)) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 270 | |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 271 | __str__ = __repr__ |
| 272 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 273 | def add_channel(self, map=None): |
| 274 | #self.log_info('adding channel %s' % self) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 275 | if map is None: |
Andrew M. Kuchling | f9ca409 | 2003-10-22 13:48:27 +0000 | [diff] [blame] | 276 | map = self._map |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 277 | map[self._fileno] = self |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 279 | def del_channel(self, map=None): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 280 | fd = self._fileno |
| 281 | if map is None: |
Andrew M. Kuchling | f9ca409 | 2003-10-22 13:48:27 +0000 | [diff] [blame] | 282 | map = self._map |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 283 | if fd in map: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 284 | #self.log_info('closing channel %d:%s' % (fd, self)) |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 285 | del map[fd] |
Raymond Hettinger | 3dc3484 | 2004-02-08 11:32:50 +0000 | [diff] [blame] | 286 | self._fileno = None |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 287 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 288 | def create_socket(self, family, type): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 289 | self.family_and_type = family, type |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 290 | sock = socket.socket(family, type) |
| 291 | sock.setblocking(0) |
| 292 | self.set_socket(sock) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 294 | def set_socket(self, sock, map=None): |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 295 | self.socket = sock |
| 296 | ## self.__dict__['socket'] = sock |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 297 | self._fileno = sock.fileno() |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 298 | self.add_channel(map) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 300 | def set_reuse_addr(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 301 | # try to re-use a server port if possible |
| 302 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 303 | self.socket.setsockopt( |
Andrew M. Kuchling | 9d499f2 | 2004-08-13 20:06:57 +0000 | [diff] [blame] | 304 | socket.SOL_SOCKET, socket.SO_REUSEADDR, |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 305 | self.socket.getsockopt(socket.SOL_SOCKET, |
Andrew M. Kuchling | 9d499f2 | 2004-08-13 20:06:57 +0000 | [diff] [blame] | 306 | socket.SO_REUSEADDR) | 1 |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 307 | ) |
Fred Drake | 9f9b593 | 2001-05-11 18:28:54 +0000 | [diff] [blame] | 308 | except socket.error: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 309 | pass |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 310 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 311 | # ================================================== |
| 312 | # predicates for select() |
| 313 | # these are used as filters for the lists of sockets |
| 314 | # to pass to select(). |
| 315 | # ================================================== |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 316 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 317 | def readable(self): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 318 | return True |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 319 | |
Andrew M. Kuchling | 419af88 | 2004-03-21 19:52:01 +0000 | [diff] [blame] | 320 | def writable(self): |
| 321 | return True |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 322 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 323 | # ================================================== |
| 324 | # socket object methods. |
| 325 | # ================================================== |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 326 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 327 | def listen(self, num): |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 328 | self.accepting = True |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 329 | if os.name == 'nt' and num > 5: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 330 | num = 5 |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 331 | return self.socket.listen(num) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 333 | def bind(self, addr): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 334 | self.addr = addr |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 335 | return self.socket.bind(addr) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 337 | def connect(self, address): |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 338 | self.connected = False |
Jeremy Hylton | e16e54f | 2001-10-29 16:44:37 +0000 | [diff] [blame] | 339 | err = self.socket.connect_ex(address) |
Giampaolo Rodolà | 76fc8c7 | 2010-08-23 21:53:41 +0000 | [diff] [blame] | 340 | if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) \ |
| 341 | or err == EINVAL and os.name in ('nt', 'ce'): |
Jeremy Hylton | e16e54f | 2001-10-29 16:44:37 +0000 | [diff] [blame] | 342 | return |
| 343 | if err in (0, EISCONN): |
| 344 | self.addr = address |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 345 | self.handle_connect_event() |
Jeremy Hylton | f24339f | 2001-10-30 14:16:17 +0000 | [diff] [blame] | 346 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 347 | raise socket.error(err, errorcode[err]) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 349 | def accept(self): |
Jeremy Hylton | f32e459 | 2002-04-04 21:02:24 +0000 | [diff] [blame] | 350 | # XXX can return either an address pair or None |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 351 | try: |
| 352 | conn, addr = self.socket.accept() |
| 353 | return conn, addr |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 354 | except socket.error as why: |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 355 | if why.args[0] == EWOULDBLOCK: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 356 | pass |
| 357 | else: |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 358 | raise |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 359 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 360 | def send(self, data): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 361 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 362 | result = self.socket.send(data) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 363 | return result |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 364 | except socket.error as why: |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 365 | if why.args[0] == EWOULDBLOCK: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 366 | return 0 |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 367 | elif why.args[0] in (ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 368 | self.handle_close() |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 369 | return 0 |
| 370 | else: |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 371 | raise |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 373 | def recv(self, buffer_size): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 374 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 375 | data = self.socket.recv(buffer_size) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 376 | if not data: |
| 377 | # a closed connection is indicated by signaling |
| 378 | # a read condition, and having recv() return 0. |
| 379 | self.handle_close() |
Guido van Rossum | df4a743 | 2007-07-18 20:57:44 +0000 | [diff] [blame] | 380 | return b'' |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 381 | else: |
| 382 | return data |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 383 | except socket.error as why: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 384 | # winsock sometimes throws ENOTCONN |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 385 | if why.args[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED]: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 386 | self.handle_close() |
Guido van Rossum | df4a743 | 2007-07-18 20:57:44 +0000 | [diff] [blame] | 387 | return b'' |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 388 | else: |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 389 | raise |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 390 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 391 | def close(self): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 392 | self.connected = False |
| 393 | self.accepting = False |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 394 | self.del_channel() |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 395 | try: |
| 396 | self.socket.close() |
| 397 | except socket.error as why: |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 398 | if why.args[0] not in (ENOTCONN, EBADF): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 399 | raise |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 400 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 401 | # cheap inheritance, used to pass all other attribute |
| 402 | # references to the underlying socket object. |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 403 | def __getattr__(self, attr): |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 404 | try: |
| 405 | retattr = getattr(self.socket, attr) |
| 406 | except AttributeError: |
| 407 | raise AttributeError("%s instance has no attribute '%s'" |
| 408 | %(self.__class__.__name__, attr)) |
| 409 | else: |
Giampaolo Rodolà | d61e397 | 2010-05-06 20:02:37 +0000 | [diff] [blame] | 410 | msg = "%(me)s.%(attr)s is deprecated; use %(me)s.socket.%(attr)s " \ |
| 411 | "instead" % {'me' : self.__class__.__name__, 'attr' : attr} |
| 412 | warnings.warn(msg, DeprecationWarning, stacklevel=2) |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 413 | return retattr |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 414 | |
Andrew M. Kuchling | c07fb2f | 2003-02-14 01:13:01 +0000 | [diff] [blame] | 415 | # log and log_info may be overridden to provide more sophisticated |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 416 | # logging and warning methods. In general, log is for 'hit' logging |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 417 | # and 'log_info' is for informational, warning and error logging. |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 418 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 419 | def log(self, message): |
| 420 | sys.stderr.write('log: %s\n' % str(message)) |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 421 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 422 | def log_info(self, message, type='info'): |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 423 | if type not in self.ignore_log_types: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 424 | print('%s: %s' % (type, message)) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 426 | def handle_read_event(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 427 | if self.accepting: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 428 | # accepting sockets are never connected, they "spawn" new |
| 429 | # sockets that are connected |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 430 | self.handle_accept() |
| 431 | elif not self.connected: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 432 | self.handle_connect_event() |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 433 | self.handle_read() |
| 434 | else: |
| 435 | self.handle_read() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 436 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 437 | def handle_connect_event(self): |
Giampaolo Rodolà | 934abdd | 2010-08-04 09:02:27 +0000 | [diff] [blame] | 438 | err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) |
| 439 | if err != 0: |
| 440 | raise socket.error(err, _strerror(err)) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 441 | self.handle_connect() |
Giampaolo Rodolà | 934abdd | 2010-08-04 09:02:27 +0000 | [diff] [blame] | 442 | self.connected = True |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 443 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 444 | def handle_write_event(self): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 445 | if self.accepting: |
| 446 | # Accepting sockets shouldn't get a write event. |
| 447 | # We will pretend it didn't happen. |
| 448 | return |
| 449 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 450 | if not self.connected: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 451 | #check for errors |
| 452 | err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) |
| 453 | if err != 0: |
| 454 | raise socket.error(err, _strerror(err)) |
| 455 | |
| 456 | self.handle_connect_event() |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 457 | self.handle_write() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 459 | def handle_expt_event(self): |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 460 | # handle_expt_event() is called if there might be an error on the |
| 461 | # socket, or if there is OOB data |
| 462 | # check for the error condition first |
| 463 | err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) |
| 464 | if err != 0: |
| 465 | # we can get here when select.select() says that there is an |
| 466 | # exceptional condition on the socket |
| 467 | # since there is an error, we'll go ahead and close the socket |
| 468 | # like we would in a subclassed handle_read() that received no |
| 469 | # data |
| 470 | self.handle_close() |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 471 | else: |
| 472 | self.handle_expt() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 473 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 474 | def handle_error(self): |
Jeremy Hylton | a8b5f7d | 2001-08-10 14:30:35 +0000 | [diff] [blame] | 475 | nil, t, v, tbinfo = compact_traceback() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 476 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 477 | # sometimes a user repr method will crash. |
| 478 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 479 | self_repr = repr(self) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 480 | except: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 481 | self_repr = '<__repr__(self) failed for object at %0x>' % id(self) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 482 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 483 | self.log_info( |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 484 | 'uncaptured python exception, closing channel %s (%s:%s %s)' % ( |
| 485 | self_repr, |
| 486 | t, |
| 487 | v, |
| 488 | tbinfo |
| 489 | ), |
| 490 | 'error' |
| 491 | ) |
Josiah Carlson | 9f2f833 | 2008-07-07 05:04:12 +0000 | [diff] [blame] | 492 | self.handle_close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 493 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 494 | def handle_expt(self): |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 495 | self.log_info('unhandled incoming priority event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 496 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 497 | def handle_read(self): |
| 498 | self.log_info('unhandled read event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 499 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 500 | def handle_write(self): |
| 501 | self.log_info('unhandled write event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 502 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 503 | def handle_connect(self): |
| 504 | self.log_info('unhandled connect event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 506 | def handle_accept(self): |
| 507 | self.log_info('unhandled accept event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 509 | def handle_close(self): |
| 510 | self.log_info('unhandled close event', 'warning') |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 511 | self.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 512 | |
| 513 | # --------------------------------------------------------------------------- |
| 514 | # adds simple buffered output capability, useful for simple clients. |
| 515 | # [for more sophisticated usage use asynchat.async_chat] |
| 516 | # --------------------------------------------------------------------------- |
| 517 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 518 | class dispatcher_with_send(dispatcher): |
| 519 | |
Andrew M. Kuchling | 67867ea | 2004-03-21 20:03:18 +0000 | [diff] [blame] | 520 | def __init__(self, sock=None, map=None): |
| 521 | dispatcher.__init__(self, sock, map) |
Guido van Rossum | df4a743 | 2007-07-18 20:57:44 +0000 | [diff] [blame] | 522 | self.out_buffer = b'' |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 523 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 524 | def initiate_send(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 525 | num_sent = 0 |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 526 | num_sent = dispatcher.send(self, self.out_buffer[:512]) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 527 | self.out_buffer = self.out_buffer[num_sent:] |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 528 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 529 | def handle_write(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 530 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 532 | def writable(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 533 | return (not self.connected) or len(self.out_buffer) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 534 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 535 | def send(self, data): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 536 | if self.debug: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 537 | self.log_info('sending %s' % repr(data)) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 538 | self.out_buffer = self.out_buffer + data |
| 539 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 540 | |
| 541 | # --------------------------------------------------------------------------- |
| 542 | # used for debugging. |
| 543 | # --------------------------------------------------------------------------- |
| 544 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 545 | def compact_traceback(): |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 546 | t, v, tb = sys.exc_info() |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 547 | tbinfo = [] |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 548 | if not tb: # Must have a traceback |
| 549 | raise AssertionError("traceback does not exist") |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 550 | while tb: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 551 | tbinfo.append(( |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 552 | tb.tb_frame.f_code.co_filename, |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 553 | tb.tb_frame.f_code.co_name, |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 554 | str(tb.tb_lineno) |
| 555 | )) |
| 556 | tb = tb.tb_next |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 557 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 558 | # just to be safe |
| 559 | del tb |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 560 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 561 | file, function, line = tbinfo[-1] |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 562 | info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo]) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 563 | return (file, function, line), t, v, info |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 564 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 565 | def close_all(map=None, ignore_all=False): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 566 | if map is None: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 567 | map = socket_map |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 568 | for x in list(map.values()): |
| 569 | try: |
| 570 | x.close() |
| 571 | except OSError as x: |
Georg Brandl | f112369 | 2008-07-20 07:31:30 +0000 | [diff] [blame] | 572 | if x.args[0] == EBADF: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 573 | pass |
| 574 | elif not ignore_all: |
| 575 | raise |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 576 | except _reraised_exceptions: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 577 | raise |
| 578 | except: |
| 579 | if not ignore_all: |
| 580 | raise |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 581 | map.clear() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 582 | |
| 583 | # Asynchronous File I/O: |
| 584 | # |
| 585 | # After a little research (reading man pages on various unixen, and |
| 586 | # digging through the linux kernel), I've determined that select() |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 587 | # isn't meant for doing asynchronous file i/o. |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 588 | # Heartening, though - reading linux/mm/filemap.c shows that linux |
| 589 | # supports asynchronous read-ahead. So _MOST_ of the time, the data |
| 590 | # will be sitting in memory for us already when we go to read it. |
| 591 | # |
| 592 | # What other OS's (besides NT) support async file i/o? [VMS?] |
| 593 | # |
| 594 | # Regardless, this is useful for pipes, and stdin/stdout... |
| 595 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 596 | if os.name == 'posix': |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 597 | import fcntl |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 598 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 599 | class file_wrapper: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 600 | # Here we override just enough to make a file |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 601 | # look like a socket for the purposes of asyncore. |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 602 | # The passed fd is automatically os.dup()'d |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 603 | |
| 604 | def __init__(self, fd): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 605 | self.fd = os.dup(fd) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 606 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 607 | def recv(self, *args): |
Jeremy Hylton | f32e459 | 2002-04-04 21:02:24 +0000 | [diff] [blame] | 608 | return os.read(self.fd, *args) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 609 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 610 | def send(self, *args): |
Jeremy Hylton | f32e459 | 2002-04-04 21:02:24 +0000 | [diff] [blame] | 611 | return os.write(self.fd, *args) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 612 | |
Georg Brandl | cbb0ae4 | 2010-07-28 08:19:35 +0000 | [diff] [blame] | 613 | def getsockopt(self, level, optname, buflen=None): |
| 614 | if (level == socket.SOL_SOCKET and |
| 615 | optname == socket.SO_ERROR and |
| 616 | not buflen): |
| 617 | return 0 |
| 618 | raise NotImplementedError("Only asyncore specific behaviour " |
| 619 | "implemented.") |
| 620 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 621 | read = recv |
| 622 | write = send |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 623 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 624 | def close(self): |
Andrew M. Kuchling | dfa74b9 | 2004-07-10 15:51:19 +0000 | [diff] [blame] | 625 | os.close(self.fd) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 626 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 627 | def fileno(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 628 | return self.fd |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 629 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 630 | class file_dispatcher(dispatcher): |
| 631 | |
Andrew M. Kuchling | 67867ea | 2004-03-21 20:03:18 +0000 | [diff] [blame] | 632 | def __init__(self, fd, map=None): |
| 633 | dispatcher.__init__(self, None, map) |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 634 | self.connected = True |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 635 | try: |
| 636 | fd = fd.fileno() |
| 637 | except AttributeError: |
| 638 | pass |
Andrew M. Kuchling | dfa74b9 | 2004-07-10 15:51:19 +0000 | [diff] [blame] | 639 | self.set_file(fd) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 640 | # set it to non-blocking mode |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 641 | flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) |
Fred Drake | a94414a | 2001-05-10 15:33:31 +0000 | [diff] [blame] | 642 | flags = flags | os.O_NONBLOCK |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 643 | fcntl.fcntl(fd, fcntl.F_SETFL, flags) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 644 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 645 | def set_file(self, fd): |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 646 | self.socket = file_wrapper(fd) |
Josiah Carlson | aae55cb | 2008-11-19 18:22:41 +0000 | [diff] [blame] | 647 | self._fileno = self.socket.fileno() |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 648 | self.add_channel() |