| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | # -*- Mode: Python -*- | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  | #   Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | #   Author: Sam Rushing <rushing@nightmare.com> | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # ====================================================================== | 
					
						
							|  |  |  | # Copyright 1996 by Sam Rushing | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | #                         All Rights Reserved | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | # Permission to use, copy, modify, and distribute this software and | 
					
						
							|  |  |  | # its documentation for any purpose and without fee is hereby | 
					
						
							|  |  |  | # granted, provided that the above copyright notice appear in all | 
					
						
							|  |  |  | # copies and that both that copyright notice and this permission | 
					
						
							|  |  |  | # notice appear in supporting documentation, and that the name of Sam | 
					
						
							|  |  |  | # Rushing not be used in advertising or publicity pertaining to | 
					
						
							|  |  |  | # distribution of the software without specific, written prior | 
					
						
							|  |  |  | # permission. | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | 
					
						
							|  |  |  | # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN | 
					
						
							|  |  |  | # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR | 
					
						
							|  |  |  | # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS | 
					
						
							|  |  |  | # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | 
					
						
							|  |  |  | # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | 
					
						
							|  |  |  | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 
					
						
							|  |  |  | # ====================================================================== | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-04 15:39:30 +00:00
										 |  |  | """Basic infrastructure for asynchronous socket service clients and servers.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | There are only two ways to have a program on a single processor do "more | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  | than one thing at a time".  Multi-threaded programming is the simplest and | 
					
						
							| 
									
										
										
										
											2000-02-04 15:39:30 +00:00
										 |  |  | most popular way to do it, but there is another very different technique, | 
					
						
							|  |  |  | that lets you have nearly all the advantages of multi-threading, without | 
					
						
							|  |  |  | actually using multiple threads. it's really only practical if your program | 
					
						
							|  |  |  | is largely I/O bound. If your program is CPU bound, then pre-emptive | 
					
						
							|  |  |  | scheduled threads are probably what you really need. Network servers are | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  | rarely CPU-bound, however. | 
					
						
							| 
									
										
										
										
											2000-02-04 15:39:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  | If your operating system supports the select() system call in its I/O | 
					
						
							| 
									
										
										
										
											2000-02-04 15:39:30 +00:00
										 |  |  | library (and nearly all do), then you can use it to juggle multiple | 
					
						
							|  |  |  | communication channels at once; doing other work while your I/O is taking | 
					
						
							|  |  |  | place in the "background."  Although this strategy can seem strange and | 
					
						
							|  |  |  | complex, especially at first, it is in many ways easier to understand and | 
					
						
							|  |  |  | control than multi-threaded programming. The module documented here solves | 
					
						
							|  |  |  | many of the difficult problems for you, making the task of building | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  | sophisticated high-performance network servers and clients a snap. | 
					
						
							| 
									
										
										
										
											2000-02-04 15:39:30 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | import select | 
					
						
							|  |  |  | import socket | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2002-09-24 17:30:31 +00:00
										 |  |  | import time | 
					
						
							| 
									
										
										
										
											2010-05-06 18:06:30 +00:00
										 |  |  | import warnings | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2010-08-23 21:53:41 +00:00
										 |  |  | from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ | 
					
						
							| 
									
										
										
										
											2011-10-23 23:49:42 +02:00
										 |  |  |      ENOTCONN, ESHUTDOWN, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \ | 
					
						
							| 
									
										
										
										
											2011-03-03 14:10:58 +00:00
										 |  |  |      errorcode | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 13:57:47 +00:00
										 |  |  | _DISCONNECTED = frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, | 
					
						
							|  |  |  |                            EBADF)) | 
					
						
							| 
									
										
										
										
											2010-09-15 21:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-08 20:30:39 +00:00
										 |  |  | try: | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     socket_map | 
					
						
							| 
									
										
										
										
											2000-09-08 20:30:39 +00:00
										 |  |  | except NameError: | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     socket_map = {} | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  | def _strerror(err): | 
					
						
							| 
									
										
										
										
											2010-05-06 18:06:30 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2010-05-18 20:11:58 +00:00
										 |  |  |         return os.strerror(err) | 
					
						
							|  |  |  |     except (ValueError, OverflowError, NameError): | 
					
						
							| 
									
										
										
										
											2010-05-06 18:06:30 +00:00
										 |  |  |         if err in errorcode: | 
					
						
							|  |  |  |             return errorcode[err] | 
					
						
							|  |  |  |         return "Unknown error %s" %err | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-01 00:45:28 +00:00
										 |  |  | class ExitNow(Exception): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-09-08 20:30:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  | _reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  | def read(obj): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         obj.handle_read_event() | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |     except _reraised_exceptions: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |         raise | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         obj.handle_error() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def write(obj): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         obj.handle_write_event() | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |     except _reraised_exceptions: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |         raise | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         obj.handle_error() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  | def _exception(obj): | 
					
						
							| 
									
										
										
										
											2004-07-10 17:36:11 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         obj.handle_expt_event() | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |     except _reraised_exceptions: | 
					
						
							| 
									
										
										
										
											2004-07-10 17:36:11 +00:00
										 |  |  |         raise | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         obj.handle_error() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  | def readwrite(obj, flags): | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |         if flags & select.POLLIN: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |             obj.handle_read_event() | 
					
						
							|  |  |  |         if flags & select.POLLOUT: | 
					
						
							|  |  |  |             obj.handle_write_event() | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |         if flags & select.POLLPRI: | 
					
						
							|  |  |  |             obj.handle_expt_event() | 
					
						
							| 
									
										
										
										
											2009-06-03 19:48:02 +00:00
										 |  |  |         if flags & (select.POLLHUP | select.POLLERR | select.POLLNVAL): | 
					
						
							|  |  |  |             obj.handle_close() | 
					
						
							|  |  |  |     except socket.error as e: | 
					
						
							| 
									
										
										
										
											2011-03-03 13:57:47 +00:00
										 |  |  |         if e.args[0] not in _DISCONNECTED: | 
					
						
							| 
									
										
										
										
											2009-06-03 19:48:02 +00:00
										 |  |  |             obj.handle_error() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             obj.handle_close() | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |     except _reraised_exceptions: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |         raise | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         obj.handle_error() | 
					
						
							| 
									
										
										
										
											2000-09-08 20:30:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  | def poll(timeout=0.0, map=None): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     if map is None: | 
					
						
							|  |  |  |         map = socket_map | 
					
						
							|  |  |  |     if map: | 
					
						
							|  |  |  |         r = []; w = []; e = [] | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         for fd, obj in list(map.items()): | 
					
						
							| 
									
										
										
										
											2004-09-01 14:04:51 +00:00
										 |  |  |             is_r = obj.readable() | 
					
						
							|  |  |  |             is_w = obj.writable() | 
					
						
							|  |  |  |             if is_r: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |                 r.append(fd) | 
					
						
							| 
									
										
										
										
											2011-07-14 20:00:49 +02:00
										 |  |  |             # accepting sockets should not be writable | 
					
						
							|  |  |  |             if is_w and not obj.accepting: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |                 w.append(fd) | 
					
						
							| 
									
										
										
										
											2004-09-01 14:04:51 +00:00
										 |  |  |             if is_r or is_w: | 
					
						
							|  |  |  |                 e.append(fd) | 
					
						
							| 
									
										
										
										
											2002-09-24 17:30:31 +00:00
										 |  |  |         if [] == r == w == e: | 
					
						
							|  |  |  |             time.sleep(timeout) | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             r, w, e = select.select(r, w, e, timeout) | 
					
						
							| 
									
										
										
										
											2011-10-23 23:49:42 +02:00
										 |  |  |         except InterruptedError: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for fd in r: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |             obj = map.get(fd) | 
					
						
							|  |  |  |             if obj is None: | 
					
						
							| 
									
										
										
										
											2001-10-09 10:10:33 +00:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |             read(obj) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for fd in w: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |             obj = map.get(fd) | 
					
						
							|  |  |  |             if obj is None: | 
					
						
							| 
									
										
										
										
											2001-10-09 10:10:33 +00:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |             write(obj) | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-10 17:36:11 +00:00
										 |  |  |         for fd in e: | 
					
						
							|  |  |  |             obj = map.get(fd) | 
					
						
							|  |  |  |             if obj is None: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             _exception(obj) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  | def poll2(timeout=0.0, map=None): | 
					
						
							| 
									
										
										
										
											2001-01-24 15:50:19 +00:00
										 |  |  |     # Use the poll() support added to the select module in Python 2.0 | 
					
						
							|  |  |  |     if map is None: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         map = socket_map | 
					
						
							| 
									
										
										
										
											2001-09-19 17:31:47 +00:00
										 |  |  |     if timeout is not None: | 
					
						
							|  |  |  |         # timeout is in milliseconds | 
					
						
							|  |  |  |         timeout = int(timeout*1000) | 
					
						
							| 
									
										
										
										
											2001-01-24 15:50:19 +00:00
										 |  |  |     pollster = select.poll() | 
					
						
							|  |  |  |     if map: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         for fd, obj in list(map.items()): | 
					
						
							| 
									
										
										
										
											2004-09-01 14:04:51 +00:00
										 |  |  |             flags = 0 | 
					
						
							| 
									
										
										
										
											2001-01-24 15:50:19 +00:00
										 |  |  |             if obj.readable(): | 
					
						
							| 
									
										
										
										
											2004-07-07 12:23:53 +00:00
										 |  |  |                 flags |= select.POLLIN | select.POLLPRI | 
					
						
							| 
									
										
										
										
											2011-07-14 20:00:49 +02:00
										 |  |  |             # accepting sockets should not be writable | 
					
						
							|  |  |  |             if obj.writable() and not obj.accepting: | 
					
						
							| 
									
										
										
										
											2004-07-07 12:23:53 +00:00
										 |  |  |                 flags |= select.POLLOUT | 
					
						
							| 
									
										
										
										
											2001-01-24 15:50:19 +00:00
										 |  |  |             if flags: | 
					
						
							|  |  |  |                 pollster.register(fd, flags) | 
					
						
							| 
									
										
										
										
											2001-10-29 16:32:19 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             r = pollster.poll(timeout) | 
					
						
							| 
									
										
										
										
											2011-10-23 23:49:42 +02:00
										 |  |  |         except InterruptedError: | 
					
						
							| 
									
										
										
										
											2001-10-29 16:32:19 +00:00
										 |  |  |             r = [] | 
					
						
							| 
									
										
										
										
											2001-01-24 15:50:19 +00:00
										 |  |  |         for fd, flags in r: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |             obj = map.get(fd) | 
					
						
							|  |  |  |             if obj is None: | 
					
						
							| 
									
										
										
										
											2001-10-09 10:10:33 +00:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |             readwrite(obj, flags) | 
					
						
							| 
									
										
										
										
											2001-01-24 15:50:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-10-22 14:38:27 +00:00
										 |  |  | poll3 = poll2                           # Alias for backward compatibility | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-30 09:02:33 +00:00
										 |  |  | def loop(timeout=30.0, use_poll=False, map=None, count=None): | 
					
						
							| 
									
										
										
										
											2001-01-24 15:50:19 +00:00
										 |  |  |     if map is None: | 
					
						
							| 
									
										
										
										
											2002-09-08 00:14:54 +00:00
										 |  |  |         map = socket_map | 
					
						
							| 
									
										
										
										
											2001-01-24 15:50:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-10-22 14:38:27 +00:00
										 |  |  |     if use_poll and hasattr(select, 'poll'): | 
					
						
							|  |  |  |         poll_fun = poll2 | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     else: | 
					
						
							|  |  |  |         poll_fun = poll | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-30 09:02:33 +00:00
										 |  |  |     if count is None: | 
					
						
							|  |  |  |         while map: | 
					
						
							|  |  |  |             poll_fun(timeout, map) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         while map and count > 0: | 
					
						
							|  |  |  |             poll_fun(timeout, map) | 
					
						
							|  |  |  |             count = count - 1 | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class dispatcher: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-11 14:01:46 +00:00
										 |  |  |     debug = False | 
					
						
							| 
									
										
										
										
											2004-03-21 19:46:16 +00:00
										 |  |  |     connected = False | 
					
						
							|  |  |  |     accepting = False | 
					
						
							| 
									
										
										
										
											2011-02-11 14:01:46 +00:00
										 |  |  |     closing = False | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     addr = None | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |     ignore_log_types = frozenset(['warning']) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def __init__(self, sock=None, map=None): | 
					
						
							| 
									
										
										
										
											2003-10-22 13:48:27 +00:00
										 |  |  |         if map is None: | 
					
						
							|  |  |  |             self._map = socket_map | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self._map = map | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         self._fileno = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         if sock: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             # Set to nonblocking just to make sure for cases where we | 
					
						
							|  |  |  |             # get a socket from a blocking source. | 
					
						
							|  |  |  |             sock.setblocking(0) | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             self.set_socket(sock, map) | 
					
						
							| 
									
										
										
										
											2004-03-21 19:46:16 +00:00
										 |  |  |             self.connected = True | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             # The constructor no longer requires that the socket | 
					
						
							|  |  |  |             # passed be connected. | 
					
						
							| 
									
										
										
										
											2001-12-14 16:15:11 +00:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 self.addr = sock.getpeername() | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             except socket.error as err: | 
					
						
							| 
									
										
										
										
											2008-07-20 07:31:30 +00:00
										 |  |  |                 if err.args[0] == ENOTCONN: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |                     # To handle the case where we got an unconnected | 
					
						
							|  |  |  |                     # socket. | 
					
						
							|  |  |  |                     self.connected = False | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     # The socket is broken in some unknown way, alert | 
					
						
							|  |  |  |                     # the user and remove it from the map (to prevent | 
					
						
							|  |  |  |                     # polling of broken sockets). | 
					
						
							|  |  |  |                     self.del_channel(map) | 
					
						
							|  |  |  |                     raise | 
					
						
							| 
									
										
										
										
											2001-10-29 16:32:19 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self.socket = None | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											2001-10-18 17:33:19 +00:00
										 |  |  |         status = [self.__class__.__module__+"."+self.__class__.__name__] | 
					
						
							|  |  |  |         if self.accepting and self.addr: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             status.append('listening') | 
					
						
							| 
									
										
										
										
											2001-10-18 17:33:19 +00:00
										 |  |  |         elif self.connected: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             status.append('connected') | 
					
						
							| 
									
										
										
										
											2001-10-18 17:33:19 +00:00
										 |  |  |         if self.addr is not None: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |                 status.append('%s:%d' % self.addr) | 
					
						
							| 
									
										
										
										
											2001-10-18 17:33:19 +00:00
										 |  |  |             except TypeError: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |                 status.append(repr(self.addr)) | 
					
						
							|  |  |  |         return '<%s at %#x>' % (' '.join(status), id(self)) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-06 18:06:30 +00:00
										 |  |  |     __str__ = __repr__ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def add_channel(self, map=None): | 
					
						
							|  |  |  |         #self.log_info('adding channel %s' % self) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         if map is None: | 
					
						
							| 
									
										
										
										
											2003-10-22 13:48:27 +00:00
										 |  |  |             map = self._map | 
					
						
							| 
									
										
										
										
											2002-09-13 14:09:26 +00:00
										 |  |  |         map[self._fileno] = self | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def del_channel(self, map=None): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         fd = self._fileno | 
					
						
							|  |  |  |         if map is None: | 
					
						
							| 
									
										
										
										
											2003-10-22 13:48:27 +00:00
										 |  |  |             map = self._map | 
					
						
							| 
									
										
										
										
											2006-08-18 22:13:04 +00:00
										 |  |  |         if fd in map: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             #self.log_info('closing channel %d:%s' % (fd, self)) | 
					
						
							| 
									
										
										
										
											2002-09-13 14:09:26 +00:00
										 |  |  |             del map[fd] | 
					
						
							| 
									
										
										
										
											2004-02-08 11:32:50 +00:00
										 |  |  |         self._fileno = None | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-25 22:21:22 +00:00
										 |  |  |     def create_socket(self, family=socket.AF_INET, type=socket.SOCK_STREAM): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         self.family_and_type = family, type | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         sock = socket.socket(family, type) | 
					
						
							|  |  |  |         sock.setblocking(0) | 
					
						
							|  |  |  |         self.set_socket(sock) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def set_socket(self, sock, map=None): | 
					
						
							| 
									
										
										
										
											2001-10-29 16:32:19 +00:00
										 |  |  |         self.socket = sock | 
					
						
							|  |  |  | ##        self.__dict__['socket'] = sock | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         self._fileno = sock.fileno() | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         self.add_channel(map) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def set_reuse_addr(self): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         # try to re-use a server port if possible | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             self.socket.setsockopt( | 
					
						
							| 
									
										
										
										
											2004-08-13 20:06:57 +00:00
										 |  |  |                 socket.SOL_SOCKET, socket.SO_REUSEADDR, | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |                 self.socket.getsockopt(socket.SOL_SOCKET, | 
					
						
							| 
									
										
										
										
											2004-08-13 20:06:57 +00:00
										 |  |  |                                        socket.SO_REUSEADDR) | 1 | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2001-05-11 18:28:54 +00:00
										 |  |  |         except socket.error: | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             pass | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     # ================================================== | 
					
						
							|  |  |  |     # predicates for select() | 
					
						
							|  |  |  |     # these are used as filters for the lists of sockets | 
					
						
							|  |  |  |     # to pass to select(). | 
					
						
							|  |  |  |     # ================================================== | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def readable(self): | 
					
						
							| 
									
										
										
										
											2002-04-04 22:55:58 +00:00
										 |  |  |         return True | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-21 19:52:01 +00:00
										 |  |  |     def writable(self): | 
					
						
							|  |  |  |         return True | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # ================================================== | 
					
						
							|  |  |  |     # socket object methods. | 
					
						
							|  |  |  |     # ================================================== | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def listen(self, num): | 
					
						
							| 
									
										
										
										
											2004-03-21 19:46:16 +00:00
										 |  |  |         self.accepting = True | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         if os.name == 'nt' and num > 5: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             num = 5 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         return self.socket.listen(num) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def bind(self, addr): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         self.addr = addr | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         return self.socket.bind(addr) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def connect(self, address): | 
					
						
							| 
									
										
										
										
											2004-03-21 19:46:16 +00:00
										 |  |  |         self.connected = False | 
					
						
							| 
									
										
										
										
											2001-10-29 16:44:37 +00:00
										 |  |  |         err = self.socket.connect_ex(address) | 
					
						
							| 
									
										
										
										
											2010-08-23 21:53:41 +00:00
										 |  |  |         if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) \ | 
					
						
							|  |  |  |         or err == EINVAL and os.name in ('nt', 'ce'): | 
					
						
							| 
									
										
										
										
											2001-10-29 16:44:37 +00:00
										 |  |  |             return | 
					
						
							|  |  |  |         if err in (0, EISCONN): | 
					
						
							|  |  |  |             self.addr = address | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             self.handle_connect_event() | 
					
						
							| 
									
										
										
										
											2001-10-30 14:16:17 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2007-08-30 01:19:48 +00:00
										 |  |  |             raise socket.error(err, errorcode[err]) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def accept(self): | 
					
						
							| 
									
										
										
										
											2002-04-04 21:02:24 +00:00
										 |  |  |         # XXX can return either an address pair or None | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             conn, addr = self.socket.accept() | 
					
						
							| 
									
										
										
										
											2010-10-04 21:08:36 +00:00
										 |  |  |         except TypeError: | 
					
						
							|  |  |  |             return None | 
					
						
							| 
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 |  |  |         except socket.error as why: | 
					
						
							| 
									
										
										
										
											2011-03-03 13:57:47 +00:00
										 |  |  |             if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN): | 
					
						
							| 
									
										
										
										
											2010-10-04 21:08:36 +00:00
										 |  |  |                 return None | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2004-07-07 20:54:48 +00:00
										 |  |  |                 raise | 
					
						
							| 
									
										
										
										
											2010-10-04 21:08:36 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             return conn, addr | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def send(self, data): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             result = self.socket.send(data) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             return result | 
					
						
							| 
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 |  |  |         except socket.error as why: | 
					
						
							| 
									
										
										
										
											2008-07-20 07:31:30 +00:00
										 |  |  |             if why.args[0] == EWOULDBLOCK: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |                 return 0 | 
					
						
							| 
									
										
										
										
											2010-09-15 21:59:04 +00:00
										 |  |  |             elif why.args[0] in _DISCONNECTED: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |                 self.handle_close() | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |                 return 0 | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2004-07-07 20:54:48 +00:00
										 |  |  |                 raise | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def recv(self, buffer_size): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             data = self.socket.recv(buffer_size) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             if not data: | 
					
						
							|  |  |  |                 # a closed connection is indicated by signaling | 
					
						
							|  |  |  |                 # a read condition, and having recv() return 0. | 
					
						
							|  |  |  |                 self.handle_close() | 
					
						
							| 
									
										
										
										
											2007-07-18 20:57:44 +00:00
										 |  |  |                 return b'' | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 return data | 
					
						
							| 
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 |  |  |         except socket.error as why: | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             # winsock sometimes throws ENOTCONN | 
					
						
							| 
									
										
										
										
											2010-09-15 21:59:04 +00:00
										 |  |  |             if why.args[0] in _DISCONNECTED: | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |                 self.handle_close() | 
					
						
							| 
									
										
										
										
											2007-07-18 20:57:44 +00:00
										 |  |  |                 return b'' | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2004-07-07 20:54:48 +00:00
										 |  |  |                 raise | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def close(self): | 
					
						
							| 
									
										
										
										
											2011-02-11 14:01:46 +00:00
										 |  |  |         self.connected = False | 
					
						
							|  |  |  |         self.accepting = False | 
					
						
							|  |  |  |         self.del_channel() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self.socket.close() | 
					
						
							|  |  |  |         except socket.error as why: | 
					
						
							|  |  |  |             if why.args[0] not in (ENOTCONN, EBADF): | 
					
						
							|  |  |  |                 raise | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # cheap inheritance, used to pass all other attribute | 
					
						
							|  |  |  |     # references to the underlying socket object. | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def __getattr__(self, attr): | 
					
						
							| 
									
										
										
										
											2010-05-06 18:06:30 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             retattr = getattr(self.socket, attr) | 
					
						
							|  |  |  |         except AttributeError: | 
					
						
							|  |  |  |             raise AttributeError("%s instance has no attribute '%s'" | 
					
						
							|  |  |  |                                  %(self.__class__.__name__, attr)) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2010-05-06 20:02:37 +00:00
										 |  |  |             msg = "%(me)s.%(attr)s is deprecated; use %(me)s.socket.%(attr)s " \ | 
					
						
							|  |  |  |                   "instead" % {'me' : self.__class__.__name__, 'attr' : attr} | 
					
						
							|  |  |  |             warnings.warn(msg, DeprecationWarning, stacklevel=2) | 
					
						
							| 
									
										
										
										
											2010-05-06 18:06:30 +00:00
										 |  |  |             return retattr | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-14 01:13:01 +00:00
										 |  |  |     # log and log_info may be overridden to provide more sophisticated | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     # logging and warning methods. In general, log is for 'hit' logging | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |     # and 'log_info' is for informational, warning and error logging. | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def log(self, message): | 
					
						
							|  |  |  |         sys.stderr.write('log: %s\n' % str(message)) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def log_info(self, message, type='info'): | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |         if type not in self.ignore_log_types: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print('%s: %s' % (type, message)) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_read_event(self): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         if self.accepting: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             # accepting sockets are never connected, they "spawn" new | 
					
						
							|  |  |  |             # sockets that are connected | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             self.handle_accept() | 
					
						
							|  |  |  |         elif not self.connected: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             self.handle_connect_event() | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             self.handle_read() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.handle_read() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |     def handle_connect_event(self): | 
					
						
							| 
									
										
										
										
											2010-08-04 09:02:27 +00:00
										 |  |  |         err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) | 
					
						
							|  |  |  |         if err != 0: | 
					
						
							|  |  |  |             raise socket.error(err, _strerror(err)) | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         self.handle_connect() | 
					
						
							| 
									
										
										
										
											2010-08-04 09:02:27 +00:00
										 |  |  |         self.connected = True | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_write_event(self): | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         if self.accepting: | 
					
						
							|  |  |  |             # Accepting sockets shouldn't get a write event. | 
					
						
							|  |  |  |             # We will pretend it didn't happen. | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         if not self.connected: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             #check for errors | 
					
						
							|  |  |  |             err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) | 
					
						
							|  |  |  |             if err != 0: | 
					
						
							|  |  |  |                 raise socket.error(err, _strerror(err)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             self.handle_connect_event() | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         self.handle_write() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_expt_event(self): | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |         # handle_expt_event() is called if there might be an error on the | 
					
						
							|  |  |  |         # socket, or if there is OOB data | 
					
						
							|  |  |  |         # check for the error condition first | 
					
						
							|  |  |  |         err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) | 
					
						
							|  |  |  |         if err != 0: | 
					
						
							|  |  |  |             # we can get here when select.select() says that there is an | 
					
						
							|  |  |  |             # exceptional condition on the socket | 
					
						
							|  |  |  |             # since there is an error, we'll go ahead and close the socket | 
					
						
							|  |  |  |             # like we would in a subclassed handle_read() that received no | 
					
						
							|  |  |  |             # data | 
					
						
							|  |  |  |             self.handle_close() | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self.handle_expt() | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_error(self): | 
					
						
							| 
									
										
										
										
											2001-08-10 14:30:35 +00:00
										 |  |  |         nil, t, v, tbinfo = compact_traceback() | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # sometimes a user repr method will crash. | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             self_repr = repr(self) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         except: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             self_repr = '<__repr__(self) failed for object at %0x>' % id(self) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         self.log_info( | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             'uncaptured python exception, closing channel %s (%s:%s %s)' % ( | 
					
						
							|  |  |  |                 self_repr, | 
					
						
							|  |  |  |                 t, | 
					
						
							|  |  |  |                 v, | 
					
						
							|  |  |  |                 tbinfo | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |             'error' | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2008-07-07 05:04:12 +00:00
										 |  |  |         self.handle_close() | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_expt(self): | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |         self.log_info('unhandled incoming priority event', 'warning') | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_read(self): | 
					
						
							|  |  |  |         self.log_info('unhandled read event', 'warning') | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_write(self): | 
					
						
							|  |  |  |         self.log_info('unhandled write event', 'warning') | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_connect(self): | 
					
						
							|  |  |  |         self.log_info('unhandled connect event', 'warning') | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_accept(self): | 
					
						
							| 
									
										
										
										
											2010-10-04 21:08:36 +00:00
										 |  |  |         pair = self.accept() | 
					
						
							|  |  |  |         if pair is not None: | 
					
						
							|  |  |  |             self.handle_accepted(*pair) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def handle_accepted(self, sock, addr): | 
					
						
							|  |  |  |         sock.close() | 
					
						
							|  |  |  |         self.log_info('unhandled accepted event', 'warning') | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_close(self): | 
					
						
							|  |  |  |         self.log_info('unhandled close event', 'warning') | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         self.close() | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # --------------------------------------------------------------------------- | 
					
						
							|  |  |  | # adds simple buffered output capability, useful for simple clients. | 
					
						
							|  |  |  | # [for more sophisticated usage use asynchat.async_chat] | 
					
						
							|  |  |  | # --------------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  | class dispatcher_with_send(dispatcher): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-21 20:03:18 +00:00
										 |  |  |     def __init__(self, sock=None, map=None): | 
					
						
							|  |  |  |         dispatcher.__init__(self, sock, map) | 
					
						
							| 
									
										
										
										
											2007-07-18 20:57:44 +00:00
										 |  |  |         self.out_buffer = b'' | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def initiate_send(self): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         num_sent = 0 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         num_sent = dispatcher.send(self, self.out_buffer[:512]) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         self.out_buffer = self.out_buffer[num_sent:] | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def handle_write(self): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         self.initiate_send() | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def writable(self): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         return (not self.connected) or len(self.out_buffer) | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     def send(self, data): | 
					
						
							| 
									
										
										
										
											2011-02-11 14:01:46 +00:00
										 |  |  |         if self.debug: | 
					
						
							|  |  |  |             self.log_info('sending %s' % repr(data)) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         self.out_buffer = self.out_buffer + data | 
					
						
							|  |  |  |         self.initiate_send() | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # --------------------------------------------------------------------------- | 
					
						
							|  |  |  | # used for debugging. | 
					
						
							|  |  |  | # --------------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  | def compact_traceback(): | 
					
						
							| 
									
										
										
										
											2002-09-13 14:09:26 +00:00
										 |  |  |     t, v, tb = sys.exc_info() | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     tbinfo = [] | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |     if not tb: # Must have a traceback | 
					
						
							|  |  |  |         raise AssertionError("traceback does not exist") | 
					
						
							| 
									
										
										
										
											2002-09-13 14:09:26 +00:00
										 |  |  |     while tb: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         tbinfo.append(( | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             tb.tb_frame.f_code.co_filename, | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |             tb.tb_frame.f_code.co_name, | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             str(tb.tb_lineno) | 
					
						
							|  |  |  |             )) | 
					
						
							|  |  |  |         tb = tb.tb_next | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # just to be safe | 
					
						
							|  |  |  |     del tb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     file, function, line = tbinfo[-1] | 
					
						
							| 
									
										
										
										
											2002-09-13 14:09:26 +00:00
										 |  |  |     info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo]) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     return (file, function, line), t, v, info | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  | def close_all(map=None, ignore_all=False): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     if map is None: | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         map = socket_map | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |     for x in list(map.values()): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             x.close() | 
					
						
							|  |  |  |         except OSError as x: | 
					
						
							| 
									
										
										
										
											2008-07-20 07:31:30 +00:00
										 |  |  |             if x.args[0] == EBADF: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |                 pass | 
					
						
							|  |  |  |             elif not ignore_all: | 
					
						
							|  |  |  |                 raise | 
					
						
							| 
									
										
										
										
											2009-04-12 15:35:44 +00:00
										 |  |  |         except _reraised_exceptions: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             raise | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             if not ignore_all: | 
					
						
							|  |  |  |                 raise | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     map.clear() | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Asynchronous File I/O: | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # After a little research (reading man pages on various unixen, and | 
					
						
							|  |  |  | # digging through the linux kernel), I've determined that select() | 
					
						
							| 
									
										
										
										
											2003-10-20 14:01:56 +00:00
										 |  |  | # isn't meant for doing asynchronous file i/o. | 
					
						
							| 
									
										
										
										
											1999-01-12 20:19:27 +00:00
										 |  |  | # Heartening, though - reading linux/mm/filemap.c shows that linux | 
					
						
							|  |  |  | # supports asynchronous read-ahead.  So _MOST_ of the time, the data | 
					
						
							|  |  |  | # will be sitting in memory for us already when we go to read it. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # What other OS's (besides NT) support async file i/o?  [VMS?] | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Regardless, this is useful for pipes, and stdin/stdout... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if os.name == 'posix': | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |     import fcntl | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class file_wrapper: | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         # Here we override just enough to make a file | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         # look like a socket for the purposes of asyncore. | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         # The passed fd is automatically os.dup()'d | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def __init__(self, fd): | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             self.fd = os.dup(fd) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         def recv(self, *args): | 
					
						
							| 
									
										
										
										
											2002-04-04 21:02:24 +00:00
										 |  |  |             return os.read(self.fd, *args) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         def send(self, *args): | 
					
						
							| 
									
										
										
										
											2002-04-04 21:02:24 +00:00
										 |  |  |             return os.write(self.fd, *args) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-28 08:19:35 +00:00
										 |  |  |         def getsockopt(self, level, optname, buflen=None): | 
					
						
							|  |  |  |             if (level == socket.SOL_SOCKET and | 
					
						
							|  |  |  |                 optname == socket.SO_ERROR and | 
					
						
							|  |  |  |                 not buflen): | 
					
						
							|  |  |  |                 return 0 | 
					
						
							|  |  |  |             raise NotImplementedError("Only asyncore specific behaviour " | 
					
						
							|  |  |  |                                       "implemented.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |         read = recv | 
					
						
							|  |  |  |         write = send | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         def close(self): | 
					
						
							| 
									
										
										
										
											2004-07-10 15:51:19 +00:00
										 |  |  |             os.close(self.fd) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         def fileno(self): | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             return self.fd | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |     class file_dispatcher(dispatcher): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-21 20:03:18 +00:00
										 |  |  |         def __init__(self, fd, map=None): | 
					
						
							|  |  |  |             dispatcher.__init__(self, None, map) | 
					
						
							| 
									
										
										
										
											2004-03-21 19:46:16 +00:00
										 |  |  |             self.connected = True | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 fd = fd.fileno() | 
					
						
							|  |  |  |             except AttributeError: | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2004-07-10 15:51:19 +00:00
										 |  |  |             self.set_file(fd) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             # set it to non-blocking mode | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) | 
					
						
							| 
									
										
										
										
											2001-05-10 15:33:31 +00:00
										 |  |  |             flags = flags | os.O_NONBLOCK | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |             fcntl.fcntl(fd, fcntl.F_SETFL, flags) | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-12 04:57:29 +00:00
										 |  |  |         def set_file(self, fd): | 
					
						
							|  |  |  |             self.socket = file_wrapper(fd) | 
					
						
							| 
									
										
										
										
											2008-11-19 18:22:41 +00:00
										 |  |  |             self._fileno = self.socket.fileno() | 
					
						
							| 
									
										
										
										
											2000-09-11 04:00:46 +00:00
										 |  |  |             self.add_channel() |