2000-09-11 01:00:46 -03:00
|
|
|
# -*- Mode: Python -*-
|
2001-01-14 14:09:23 -04:00
|
|
|
# Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp
|
2000-09-11 01:00:46 -03:00
|
|
|
# Author: Sam Rushing <rushing@nightmare.com>
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
# ======================================================================
|
|
|
|
# Copyright 1996 by Sam Rushing
|
2001-01-14 14:09:23 -04:00
|
|
|
#
|
1999-01-12 16:19:27 -04:00
|
|
|
# All Rights Reserved
|
2001-01-14 14:09:23 -04:00
|
|
|
#
|
1999-01-12 16:19:27 -04: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 14:09:23 -04:00
|
|
|
#
|
1999-01-12 16:19:27 -04: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 11:39:30 -04: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 14:09:23 -04:00
|
|
|
than one thing at a time". Multi-threaded programming is the simplest and
|
2000-02-04 11:39:30 -04: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 14:09:23 -04:00
|
|
|
rarely CPU-bound, however.
|
2000-02-04 11:39:30 -04:00
|
|
|
|
2001-01-14 14:09:23 -04:00
|
|
|
If your operating system supports the select() system call in its I/O
|
2000-02-04 11:39:30 -04: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 14:09:23 -04:00
|
|
|
sophisticated high-performance network servers and clients a snap.
|
2000-02-04 11:39:30 -04:00
|
|
|
"""
|
|
|
|
|
1999-01-12 16:19:27 -04:00
|
|
|
import select
|
|
|
|
import socket
|
|
|
|
import sys
|
2002-09-24 14:30:31 -03:00
|
|
|
import time
|
2010-05-06 15:06:30 -03:00
|
|
|
import warnings
|
|
|
|
|
1999-01-12 16:19:27 -04:00
|
|
|
import os
|
2010-08-23 18:53:41 -03:00
|
|
|
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \
|
2011-10-23 18:49:42 -03:00
|
|
|
ENOTCONN, ESHUTDOWN, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \
|
2011-03-03 10:10:58 -04:00
|
|
|
errorcode
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2014-11-09 19:56:33 -04:00
|
|
|
_DISCONNECTED = frozenset({ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE,
|
|
|
|
EBADF})
|
2010-09-15 18:43:47 -03:00
|
|
|
|
2000-09-08 17:30:39 -03:00
|
|
|
try:
|
2000-09-11 01:00:46 -03:00
|
|
|
socket_map
|
2000-09-08 17:30:39 -03:00
|
|
|
except NameError:
|
2000-09-11 01:00:46 -03:00
|
|
|
socket_map = {}
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2008-07-07 01:15:08 -03:00
|
|
|
def _strerror(err):
|
2010-05-06 15:06:30 -03:00
|
|
|
try:
|
2010-05-18 17:11:58 -03:00
|
|
|
return os.strerror(err)
|
|
|
|
except (ValueError, OverflowError, NameError):
|
2010-05-06 15:06:30 -03:00
|
|
|
if err in errorcode:
|
|
|
|
return errorcode[err]
|
|
|
|
return "Unknown error %s" %err
|
2008-07-07 01:15:08 -03:00
|
|
|
|
2005-08-31 21:45:28 -03:00
|
|
|
class ExitNow(Exception):
|
2000-09-11 01:00:46 -03:00
|
|
|
pass
|
2000-09-08 17:30:39 -03:00
|
|
|
|
2009-04-12 12:35:44 -03:00
|
|
|
_reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit)
|
|
|
|
|
2002-09-07 21:14:54 -03:00
|
|
|
def read(obj):
|
|
|
|
try:
|
|
|
|
obj.handle_read_event()
|
2009-04-12 12:35:44 -03:00
|
|
|
except _reraised_exceptions:
|
2002-09-07 21:14:54 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
|
|
|
def write(obj):
|
|
|
|
try:
|
|
|
|
obj.handle_write_event()
|
2009-04-12 12:35:44 -03:00
|
|
|
except _reraised_exceptions:
|
2002-09-07 21:14:54 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
2008-07-07 01:15:08 -03:00
|
|
|
def _exception(obj):
|
2004-07-10 14:36:11 -03:00
|
|
|
try:
|
|
|
|
obj.handle_expt_event()
|
2009-04-12 12:35:44 -03:00
|
|
|
except _reraised_exceptions:
|
2004-07-10 14:36:11 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
2002-09-07 21:14:54 -03:00
|
|
|
def readwrite(obj, flags):
|
|
|
|
try:
|
2009-04-12 12:35:44 -03:00
|
|
|
if flags & select.POLLIN:
|
2002-09-07 21:14:54 -03:00
|
|
|
obj.handle_read_event()
|
|
|
|
if flags & select.POLLOUT:
|
|
|
|
obj.handle_write_event()
|
2009-04-12 12:35:44 -03:00
|
|
|
if flags & select.POLLPRI:
|
|
|
|
obj.handle_expt_event()
|
2009-06-03 16:48:02 -03:00
|
|
|
if flags & (select.POLLHUP | select.POLLERR | select.POLLNVAL):
|
|
|
|
obj.handle_close()
|
2012-12-18 17:10:48 -04:00
|
|
|
except OSError as e:
|
2011-03-03 09:57:47 -04:00
|
|
|
if e.args[0] not in _DISCONNECTED:
|
2009-06-03 16:48:02 -03:00
|
|
|
obj.handle_error()
|
|
|
|
else:
|
|
|
|
obj.handle_close()
|
2009-04-12 12:35:44 -03:00
|
|
|
except _reraised_exceptions:
|
2002-09-07 21:14:54 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
2000-09-08 17:30:39 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def poll(timeout=0.0, map=None):
|
2000-09-11 01:00:46 -03:00
|
|
|
if map is None:
|
|
|
|
map = socket_map
|
|
|
|
if map:
|
|
|
|
r = []; w = []; e = []
|
2008-07-07 01:15:08 -03:00
|
|
|
for fd, obj in list(map.items()):
|
2004-09-01 11:04:51 -03:00
|
|
|
is_r = obj.readable()
|
|
|
|
is_w = obj.writable()
|
|
|
|
if is_r:
|
2002-09-07 21:14:54 -03:00
|
|
|
r.append(fd)
|
2011-07-14 15:00:49 -03:00
|
|
|
# accepting sockets should not be writable
|
|
|
|
if is_w and not obj.accepting:
|
2002-09-07 21:14:54 -03:00
|
|
|
w.append(fd)
|
2004-09-01 11:04:51 -03:00
|
|
|
if is_r or is_w:
|
|
|
|
e.append(fd)
|
2002-09-24 14:30:31 -03:00
|
|
|
if [] == r == w == e:
|
|
|
|
time.sleep(timeout)
|
2008-07-07 01:15:08 -03:00
|
|
|
return
|
|
|
|
|
2015-03-30 16:16:11 -03:00
|
|
|
r, w, e = select.select(r, w, e, timeout)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
for fd in r:
|
2002-09-07 21:14:54 -03:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2001-10-09 07:10:33 -03:00
|
|
|
continue
|
2002-09-07 21:14:54 -03:00
|
|
|
read(obj)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
for fd in w:
|
2002-09-07 21:14:54 -03:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2001-10-09 07:10:33 -03:00
|
|
|
continue
|
2002-09-07 21:14:54 -03:00
|
|
|
write(obj)
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2004-07-10 14:36:11 -03:00
|
|
|
for fd in e:
|
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
|
|
|
continue
|
|
|
|
_exception(obj)
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def poll2(timeout=0.0, map=None):
|
2001-01-24 11:50:19 -04:00
|
|
|
# Use the poll() support added to the select module in Python 2.0
|
|
|
|
if map is None:
|
2002-09-12 01:57:29 -03:00
|
|
|
map = socket_map
|
2001-09-19 14:31:47 -03:00
|
|
|
if timeout is not None:
|
|
|
|
# timeout is in milliseconds
|
|
|
|
timeout = int(timeout*1000)
|
2001-01-24 11:50:19 -04:00
|
|
|
pollster = select.poll()
|
|
|
|
if map:
|
2008-07-07 01:15:08 -03:00
|
|
|
for fd, obj in list(map.items()):
|
2004-09-01 11:04:51 -03:00
|
|
|
flags = 0
|
2001-01-24 11:50:19 -04:00
|
|
|
if obj.readable():
|
2004-07-07 09:23:53 -03:00
|
|
|
flags |= select.POLLIN | select.POLLPRI
|
2011-07-14 15:00:49 -03:00
|
|
|
# accepting sockets should not be writable
|
|
|
|
if obj.writable() and not obj.accepting:
|
2004-07-07 09:23:53 -03:00
|
|
|
flags |= select.POLLOUT
|
2001-01-24 11:50:19 -04:00
|
|
|
if flags:
|
|
|
|
pollster.register(fd, flags)
|
2015-03-30 16:38:00 -03:00
|
|
|
|
|
|
|
r = pollster.poll(timeout)
|
2001-01-24 11:50:19 -04:00
|
|
|
for fd, flags in r:
|
2002-09-07 21:14:54 -03:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2001-10-09 07:10:33 -03:00
|
|
|
continue
|
2002-09-07 21:14:54 -03:00
|
|
|
readwrite(obj, flags)
|
2001-01-24 11:50:19 -04:00
|
|
|
|
2003-10-22 11:38:27 -03:00
|
|
|
poll3 = poll2 # Alias for backward compatibility
|
|
|
|
|
2004-06-30 06:02:33 -03:00
|
|
|
def loop(timeout=30.0, use_poll=False, map=None, count=None):
|
2001-01-24 11:50:19 -04:00
|
|
|
if map is None:
|
2002-09-07 21:14:54 -03:00
|
|
|
map = socket_map
|
2001-01-24 11:50:19 -04:00
|
|
|
|
2003-10-22 11:38:27 -03:00
|
|
|
if use_poll and hasattr(select, 'poll'):
|
|
|
|
poll_fun = poll2
|
2000-09-11 01:00:46 -03:00
|
|
|
else:
|
|
|
|
poll_fun = poll
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2004-06-30 06:02:33 -03: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 16:19:27 -04:00
|
|
|
|
|
|
|
class dispatcher:
|
2002-09-12 01:57:29 -03:00
|
|
|
|
2011-02-11 10:01:46 -04:00
|
|
|
debug = False
|
2004-03-21 15:46:16 -04:00
|
|
|
connected = False
|
|
|
|
accepting = False
|
2012-03-22 12:17:43 -03:00
|
|
|
connecting = False
|
2011-02-11 10:01:46 -04:00
|
|
|
closing = False
|
2000-09-11 01:00:46 -03:00
|
|
|
addr = None
|
2014-11-09 19:56:33 -04:00
|
|
|
ignore_log_types = frozenset({'warning'})
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def __init__(self, sock=None, map=None):
|
2003-10-22 10:48:27 -03:00
|
|
|
if map is None:
|
|
|
|
self._map = socket_map
|
|
|
|
else:
|
|
|
|
self._map = map
|
|
|
|
|
2008-07-07 01:15:08 -03:00
|
|
|
self._fileno = None
|
|
|
|
|
2000-09-11 01:00:46 -03:00
|
|
|
if sock:
|
2008-07-07 01:15:08 -03: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 01:57:29 -03:00
|
|
|
self.set_socket(sock, map)
|
2004-03-21 15:46:16 -04:00
|
|
|
self.connected = True
|
2008-07-07 01:15:08 -03:00
|
|
|
# The constructor no longer requires that the socket
|
|
|
|
# passed be connected.
|
2001-12-14 12:15:11 -04:00
|
|
|
try:
|
|
|
|
self.addr = sock.getpeername()
|
2012-12-18 17:10:48 -04:00
|
|
|
except OSError as err:
|
2012-03-22 12:17:43 -03:00
|
|
|
if err.args[0] in (ENOTCONN, EINVAL):
|
2008-07-07 01:15:08 -03: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 12:32:19 -04:00
|
|
|
else:
|
|
|
|
self.socket = None
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def __repr__(self):
|
2014-07-22 09:00:37 -03:00
|
|
|
status = [self.__class__.__module__+"."+self.__class__.__qualname__]
|
2001-10-18 14:33:19 -03:00
|
|
|
if self.accepting and self.addr:
|
2002-09-12 01:57:29 -03:00
|
|
|
status.append('listening')
|
2001-10-18 14:33:19 -03:00
|
|
|
elif self.connected:
|
2002-09-12 01:57:29 -03:00
|
|
|
status.append('connected')
|
2001-10-18 14:33:19 -03:00
|
|
|
if self.addr is not None:
|
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
status.append('%s:%d' % self.addr)
|
2001-10-18 14:33:19 -03:00
|
|
|
except TypeError:
|
2002-09-12 01:57:29 -03:00
|
|
|
status.append(repr(self.addr))
|
|
|
|
return '<%s at %#x>' % (' '.join(status), id(self))
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2010-05-06 15:06:30 -03:00
|
|
|
__str__ = __repr__
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def add_channel(self, map=None):
|
|
|
|
#self.log_info('adding channel %s' % self)
|
2000-09-11 01:00:46 -03:00
|
|
|
if map is None:
|
2003-10-22 10:48:27 -03:00
|
|
|
map = self._map
|
2002-09-13 11:09:26 -03:00
|
|
|
map[self._fileno] = self
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def del_channel(self, map=None):
|
2000-09-11 01:00:46 -03:00
|
|
|
fd = self._fileno
|
|
|
|
if map is None:
|
2003-10-22 10:48:27 -03:00
|
|
|
map = self._map
|
2006-08-18 19:13:04 -03:00
|
|
|
if fd in map:
|
2002-09-12 01:57:29 -03:00
|
|
|
#self.log_info('closing channel %d:%s' % (fd, self))
|
2002-09-13 11:09:26 -03:00
|
|
|
del map[fd]
|
2004-02-08 07:32:50 -04:00
|
|
|
self._fileno = None
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2011-02-25 18:21:22 -04:00
|
|
|
def create_socket(self, family=socket.AF_INET, type=socket.SOCK_STREAM):
|
2000-09-11 01:00:46 -03:00
|
|
|
self.family_and_type = family, type
|
2008-07-07 01:15:08 -03:00
|
|
|
sock = socket.socket(family, type)
|
|
|
|
sock.setblocking(0)
|
|
|
|
self.set_socket(sock)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def set_socket(self, sock, map=None):
|
2001-10-29 12:32:19 -04:00
|
|
|
self.socket = sock
|
|
|
|
## self.__dict__['socket'] = sock
|
2000-09-11 01:00:46 -03:00
|
|
|
self._fileno = sock.fileno()
|
2002-09-12 01:57:29 -03:00
|
|
|
self.add_channel(map)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def set_reuse_addr(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
# try to re-use a server port if possible
|
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
self.socket.setsockopt(
|
2004-08-13 17:06:57 -03:00
|
|
|
socket.SOL_SOCKET, socket.SO_REUSEADDR,
|
2002-09-12 01:57:29 -03:00
|
|
|
self.socket.getsockopt(socket.SOL_SOCKET,
|
2004-08-13 17:06:57 -03:00
|
|
|
socket.SO_REUSEADDR) | 1
|
2000-09-11 01:00:46 -03:00
|
|
|
)
|
2012-12-18 17:10:48 -04:00
|
|
|
except OSError:
|
2000-09-11 01:00:46 -03:00
|
|
|
pass
|
2004-07-18 03:16:08 -03:00
|
|
|
|
2000-09-11 01:00:46 -03:00
|
|
|
# ==================================================
|
|
|
|
# predicates for select()
|
|
|
|
# these are used as filters for the lists of sockets
|
|
|
|
# to pass to select().
|
|
|
|
# ==================================================
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def readable(self):
|
2002-04-04 18:55:58 -04:00
|
|
|
return True
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2004-03-21 15:52:01 -04:00
|
|
|
def writable(self):
|
|
|
|
return True
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
# socket object methods.
|
|
|
|
# ==================================================
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def listen(self, num):
|
2004-03-21 15:46:16 -04:00
|
|
|
self.accepting = True
|
2000-09-11 01:00:46 -03:00
|
|
|
if os.name == 'nt' and num > 5:
|
2008-07-07 01:15:08 -03:00
|
|
|
num = 5
|
2002-09-12 01:57:29 -03:00
|
|
|
return self.socket.listen(num)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def bind(self, addr):
|
2000-09-11 01:00:46 -03:00
|
|
|
self.addr = addr
|
2002-09-12 01:57:29 -03:00
|
|
|
return self.socket.bind(addr)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def connect(self, address):
|
2004-03-21 15:46:16 -04:00
|
|
|
self.connected = False
|
2012-03-22 12:17:43 -03:00
|
|
|
self.connecting = True
|
2001-10-29 12:44:37 -04:00
|
|
|
err = self.socket.connect_ex(address)
|
2010-08-23 18:53:41 -03:00
|
|
|
if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) \
|
2016-09-05 19:11:23 -03:00
|
|
|
or err == EINVAL and os.name == 'nt':
|
2012-03-20 12:44:24 -03:00
|
|
|
self.addr = address
|
2001-10-29 12:44:37 -04:00
|
|
|
return
|
|
|
|
if err in (0, EISCONN):
|
|
|
|
self.addr = address
|
2008-07-07 01:15:08 -03:00
|
|
|
self.handle_connect_event()
|
2001-10-30 10:16:17 -04:00
|
|
|
else:
|
2012-12-18 17:10:48 -04:00
|
|
|
raise OSError(err, errorcode[err])
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def accept(self):
|
2002-04-04 17:02:24 -04:00
|
|
|
# XXX can return either an address pair or None
|
2000-09-11 01:00:46 -03:00
|
|
|
try:
|
|
|
|
conn, addr = self.socket.accept()
|
2010-10-04 18:08:36 -03:00
|
|
|
except TypeError:
|
|
|
|
return None
|
2012-12-18 17:10:48 -04:00
|
|
|
except OSError as why:
|
2011-03-03 09:57:47 -04:00
|
|
|
if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN):
|
2010-10-04 18:08:36 -03:00
|
|
|
return None
|
2000-09-11 01:00:46 -03:00
|
|
|
else:
|
2004-07-07 17:54:48 -03:00
|
|
|
raise
|
2010-10-04 18:08:36 -03:00
|
|
|
else:
|
|
|
|
return conn, addr
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def send(self, data):
|
2000-09-11 01:00:46 -03:00
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
result = self.socket.send(data)
|
2000-09-11 01:00:46 -03:00
|
|
|
return result
|
2012-12-18 17:10:48 -04:00
|
|
|
except OSError as why:
|
2008-07-20 04:31:30 -03:00
|
|
|
if why.args[0] == EWOULDBLOCK:
|
2008-07-07 01:15:08 -03:00
|
|
|
return 0
|
2010-09-15 18:59:04 -03:00
|
|
|
elif why.args[0] in _DISCONNECTED:
|
2008-07-07 01:15:08 -03:00
|
|
|
self.handle_close()
|
2000-09-11 01:00:46 -03:00
|
|
|
return 0
|
|
|
|
else:
|
2004-07-07 17:54:48 -03:00
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def recv(self, buffer_size):
|
2000-09-11 01:00:46 -03:00
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
data = self.socket.recv(buffer_size)
|
2000-09-11 01:00:46 -03: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 17:57:44 -03:00
|
|
|
return b''
|
2000-09-11 01:00:46 -03:00
|
|
|
else:
|
|
|
|
return data
|
2012-12-18 17:10:48 -04:00
|
|
|
except OSError as why:
|
2012-12-18 15:14:22 -04:00
|
|
|
# winsock sometimes raises ENOTCONN
|
2010-09-15 18:59:04 -03:00
|
|
|
if why.args[0] in _DISCONNECTED:
|
2000-09-11 01:00:46 -03:00
|
|
|
self.handle_close()
|
2007-07-18 17:57:44 -03:00
|
|
|
return b''
|
2000-09-11 01:00:46 -03:00
|
|
|
else:
|
2004-07-07 17:54:48 -03:00
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def close(self):
|
2011-02-11 10:01:46 -04:00
|
|
|
self.connected = False
|
|
|
|
self.accepting = False
|
2012-03-22 12:17:43 -03:00
|
|
|
self.connecting = False
|
2011-02-11 10:01:46 -04:00
|
|
|
self.del_channel()
|
2013-04-09 12:21:25 -03:00
|
|
|
if self.socket is not None:
|
|
|
|
try:
|
|
|
|
self.socket.close()
|
|
|
|
except OSError as why:
|
|
|
|
if why.args[0] not in (ENOTCONN, EBADF):
|
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2003-02-13 21:13:01 -04:00
|
|
|
# log and log_info may be overridden to provide more sophisticated
|
2000-09-11 01:00:46 -03:00
|
|
|
# logging and warning methods. In general, log is for 'hit' logging
|
2001-01-14 14:09:23 -04:00
|
|
|
# and 'log_info' is for informational, warning and error logging.
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def log(self, message):
|
|
|
|
sys.stderr.write('log: %s\n' % str(message))
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def log_info(self, message, type='info'):
|
2009-04-12 12:35:44 -03:00
|
|
|
if type not in self.ignore_log_types:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('%s: %s' % (type, message))
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_read_event(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
if self.accepting:
|
2008-07-07 01:15:08 -03:00
|
|
|
# accepting sockets are never connected, they "spawn" new
|
|
|
|
# sockets that are connected
|
2000-09-11 01:00:46 -03:00
|
|
|
self.handle_accept()
|
|
|
|
elif not self.connected:
|
2012-03-22 12:17:43 -03:00
|
|
|
if self.connecting:
|
|
|
|
self.handle_connect_event()
|
2000-09-11 01:00:46 -03:00
|
|
|
self.handle_read()
|
|
|
|
else:
|
|
|
|
self.handle_read()
|
|
|
|
|
2008-07-07 01:15:08 -03:00
|
|
|
def handle_connect_event(self):
|
2010-08-04 06:02:27 -03:00
|
|
|
err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
|
|
|
|
if err != 0:
|
2012-12-18 17:10:48 -04:00
|
|
|
raise OSError(err, _strerror(err))
|
2008-07-07 01:15:08 -03:00
|
|
|
self.handle_connect()
|
2010-08-04 06:02:27 -03:00
|
|
|
self.connected = True
|
2012-03-22 12:17:43 -03:00
|
|
|
self.connecting = False
|
2008-07-07 01:15:08 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_write_event(self):
|
2008-07-07 01:15:08 -03:00
|
|
|
if self.accepting:
|
|
|
|
# Accepting sockets shouldn't get a write event.
|
|
|
|
# We will pretend it didn't happen.
|
|
|
|
return
|
|
|
|
|
2000-09-11 01:00:46 -03:00
|
|
|
if not self.connected:
|
2012-03-22 12:17:43 -03:00
|
|
|
if self.connecting:
|
|
|
|
self.handle_connect_event()
|
2000-09-11 01:00:46 -03:00
|
|
|
self.handle_write()
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_expt_event(self):
|
2009-04-12 12:35:44 -03: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 01:15:08 -03:00
|
|
|
else:
|
|
|
|
self.handle_expt()
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_error(self):
|
2001-08-10 11:30:35 -03:00
|
|
|
nil, t, v, tbinfo = compact_traceback()
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
# sometimes a user repr method will crash.
|
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
self_repr = repr(self)
|
2000-09-11 01:00:46 -03:00
|
|
|
except:
|
2002-09-12 01:57:29 -03:00
|
|
|
self_repr = '<__repr__(self) failed for object at %0x>' % id(self)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
self.log_info(
|
2000-09-11 01:00:46 -03:00
|
|
|
'uncaptured python exception, closing channel %s (%s:%s %s)' % (
|
|
|
|
self_repr,
|
|
|
|
t,
|
|
|
|
v,
|
|
|
|
tbinfo
|
|
|
|
),
|
|
|
|
'error'
|
|
|
|
)
|
2008-07-07 02:04:12 -03:00
|
|
|
self.handle_close()
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_expt(self):
|
2009-04-12 12:35:44 -03:00
|
|
|
self.log_info('unhandled incoming priority event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_read(self):
|
|
|
|
self.log_info('unhandled read event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_write(self):
|
|
|
|
self.log_info('unhandled write event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_connect(self):
|
|
|
|
self.log_info('unhandled connect event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_accept(self):
|
2010-10-04 18:08:36 -03: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 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_close(self):
|
|
|
|
self.log_info('unhandled close event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
self.close()
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# adds simple buffered output capability, useful for simple clients.
|
|
|
|
# [for more sophisticated usage use asynchat.async_chat]
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
class dispatcher_with_send(dispatcher):
|
|
|
|
|
2004-03-21 16:03:18 -04:00
|
|
|
def __init__(self, sock=None, map=None):
|
|
|
|
dispatcher.__init__(self, sock, map)
|
2007-07-18 17:57:44 -03:00
|
|
|
self.out_buffer = b''
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def initiate_send(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
num_sent = 0
|
2013-01-01 11:31:54 -04:00
|
|
|
num_sent = dispatcher.send(self, self.out_buffer[:65536])
|
2000-09-11 01:00:46 -03:00
|
|
|
self.out_buffer = self.out_buffer[num_sent:]
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_write(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
self.initiate_send()
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def writable(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
return (not self.connected) or len(self.out_buffer)
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def send(self, data):
|
2011-02-11 10:01:46 -04:00
|
|
|
if self.debug:
|
|
|
|
self.log_info('sending %s' % repr(data))
|
2000-09-11 01:00:46 -03:00
|
|
|
self.out_buffer = self.out_buffer + data
|
|
|
|
self.initiate_send()
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# used for debugging.
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def compact_traceback():
|
2002-09-13 11:09:26 -03:00
|
|
|
t, v, tb = sys.exc_info()
|
2000-09-11 01:00:46 -03:00
|
|
|
tbinfo = []
|
2008-07-07 01:15:08 -03:00
|
|
|
if not tb: # Must have a traceback
|
|
|
|
raise AssertionError("traceback does not exist")
|
2002-09-13 11:09:26 -03:00
|
|
|
while tb:
|
2002-09-12 01:57:29 -03:00
|
|
|
tbinfo.append((
|
2000-09-11 01:00:46 -03:00
|
|
|
tb.tb_frame.f_code.co_filename,
|
2001-01-14 14:09:23 -04:00
|
|
|
tb.tb_frame.f_code.co_name,
|
2000-09-11 01:00:46 -03:00
|
|
|
str(tb.tb_lineno)
|
|
|
|
))
|
|
|
|
tb = tb.tb_next
|
|
|
|
|
|
|
|
# just to be safe
|
|
|
|
del tb
|
|
|
|
|
|
|
|
file, function, line = tbinfo[-1]
|
2002-09-13 11:09:26 -03:00
|
|
|
info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo])
|
2000-09-11 01:00:46 -03:00
|
|
|
return (file, function, line), t, v, info
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2008-07-07 01:15:08 -03:00
|
|
|
def close_all(map=None, ignore_all=False):
|
2000-09-11 01:00:46 -03:00
|
|
|
if map is None:
|
2002-09-12 01:57:29 -03:00
|
|
|
map = socket_map
|
2008-07-07 01:15:08 -03:00
|
|
|
for x in list(map.values()):
|
|
|
|
try:
|
|
|
|
x.close()
|
|
|
|
except OSError as x:
|
2008-07-20 04:31:30 -03:00
|
|
|
if x.args[0] == EBADF:
|
2008-07-07 01:15:08 -03:00
|
|
|
pass
|
|
|
|
elif not ignore_all:
|
|
|
|
raise
|
2009-04-12 12:35:44 -03:00
|
|
|
except _reraised_exceptions:
|
2008-07-07 01:15:08 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
if not ignore_all:
|
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
map.clear()
|
1999-01-12 16:19:27 -04: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 11:01:56 -03:00
|
|
|
# isn't meant for doing asynchronous file i/o.
|
1999-01-12 16:19:27 -04: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 01:00:46 -03:00
|
|
|
class file_wrapper:
|
2008-07-07 01:15:08 -03:00
|
|
|
# Here we override just enough to make a file
|
2000-09-11 01:00:46 -03:00
|
|
|
# look like a socket for the purposes of asyncore.
|
2008-07-07 01:15:08 -03:00
|
|
|
# The passed fd is automatically os.dup()'d
|
2002-09-12 01:57:29 -03:00
|
|
|
|
|
|
|
def __init__(self, fd):
|
2008-07-07 01:15:08 -03:00
|
|
|
self.fd = os.dup(fd)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2014-06-27 18:52:03 -03:00
|
|
|
def __del__(self):
|
|
|
|
if self.fd >= 0:
|
2016-03-22 20:28:08 -03:00
|
|
|
warnings.warn("unclosed file %r" % self, ResourceWarning,
|
|
|
|
source=self)
|
2014-06-27 18:52:03 -03:00
|
|
|
self.close()
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def recv(self, *args):
|
2002-04-04 17:02:24 -04:00
|
|
|
return os.read(self.fd, *args)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def send(self, *args):
|
2002-04-04 17:02:24 -04:00
|
|
|
return os.write(self.fd, *args)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2010-07-28 05:19:35 -03: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 01:00:46 -03:00
|
|
|
read = recv
|
|
|
|
write = send
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def close(self):
|
2014-06-27 18:52:03 -03:00
|
|
|
if self.fd < 0:
|
|
|
|
return
|
2004-07-10 12:51:19 -03:00
|
|
|
os.close(self.fd)
|
2014-06-27 18:52:03 -03:00
|
|
|
self.fd = -1
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def fileno(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
return self.fd
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
class file_dispatcher(dispatcher):
|
|
|
|
|
2004-03-21 16:03:18 -04:00
|
|
|
def __init__(self, fd, map=None):
|
|
|
|
dispatcher.__init__(self, None, map)
|
2004-03-21 15:46:16 -04:00
|
|
|
self.connected = True
|
2008-07-07 01:15:08 -03:00
|
|
|
try:
|
|
|
|
fd = fd.fileno()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2004-07-10 12:51:19 -03:00
|
|
|
self.set_file(fd)
|
2000-09-11 01:00:46 -03:00
|
|
|
# set it to non-blocking mode
|
2014-07-29 17:32:47 -03:00
|
|
|
os.set_blocking(fd, False)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def set_file(self, fd):
|
|
|
|
self.socket = file_wrapper(fd)
|
2008-11-19 14:22:41 -04:00
|
|
|
self._fileno = self.socket.fileno()
|
2000-09-11 01:00:46 -03:00
|
|
|
self.add_channel()
|