2003-03-12 07:26:59 +01:00
|
|
|
# -*- Mode: Python -*-
|
|
|
|
# Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp
|
|
|
|
# Author: Sam Rushing <rushing@nightmare.com>
|
|
|
|
|
|
|
|
# ======================================================================
|
|
|
|
# Copyright 1996 by Sam Rushing
|
|
|
|
#
|
|
|
|
# All Rights Reserved
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
# ======================================================================
|
|
|
|
|
|
|
|
"""Basic infrastructure for asynchronous socket service clients and servers.
|
|
|
|
|
|
|
|
There are only two ways to have a program on a single processor do "more
|
|
|
|
than one thing at a time". Multi-threaded programming is the simplest and
|
|
|
|
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
|
|
|
|
rarely CPU-bound, however.
|
|
|
|
|
|
|
|
If your operating system supports the select() system call in its I/O
|
|
|
|
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
|
|
|
|
sophisticated high-performance network servers and clients a snap.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import exceptions
|
|
|
|
import select
|
|
|
|
import socket
|
|
|
|
import sys
|
2003-04-08 09:04:57 +02:00
|
|
|
import time
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \
|
|
|
|
ENOTCONN, ESHUTDOWN, EINTR, EISCONN
|
|
|
|
|
|
|
|
try:
|
|
|
|
socket_map
|
|
|
|
except NameError:
|
|
|
|
socket_map = {}
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
class ExitNow(exceptions.Exception):
|
2003-03-12 07:26:59 +01:00
|
|
|
pass
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def read(obj):
|
|
|
|
try:
|
|
|
|
obj.handle_read_event()
|
|
|
|
except ExitNow:
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
|
|
|
def write(obj):
|
|
|
|
try:
|
|
|
|
obj.handle_write_event()
|
|
|
|
except ExitNow:
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
|
|
|
def readwrite(obj, flags):
|
|
|
|
try:
|
|
|
|
if flags & select.POLLIN:
|
|
|
|
obj.handle_read_event()
|
|
|
|
if flags & select.POLLOUT:
|
|
|
|
obj.handle_write_event()
|
|
|
|
except ExitNow:
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
|
|
|
def poll(timeout=0.0, map=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
if map is None:
|
|
|
|
map = socket_map
|
|
|
|
if map:
|
|
|
|
r = []; w = []; e = []
|
2003-04-08 09:04:57 +02:00
|
|
|
for fd, obj in map.items():
|
2003-03-12 07:26:59 +01:00
|
|
|
if obj.readable():
|
2003-04-08 09:04:57 +02:00
|
|
|
r.append(fd)
|
2003-03-12 07:26:59 +01:00
|
|
|
if obj.writable():
|
2003-04-08 09:04:57 +02:00
|
|
|
w.append(fd)
|
|
|
|
if [] == r == w == e:
|
|
|
|
time.sleep(timeout)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
r, w, e = select.select(r, w, e, timeout)
|
|
|
|
except select.error, err:
|
|
|
|
if err[0] != EINTR:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
return
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
for fd in r:
|
2003-04-08 09:04:57 +02:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2003-03-12 07:26:59 +01:00
|
|
|
continue
|
2003-04-08 09:04:57 +02:00
|
|
|
read(obj)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
for fd in w:
|
2003-04-08 09:04:57 +02:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2003-03-12 07:26:59 +01:00
|
|
|
continue
|
2003-04-08 09:04:57 +02:00
|
|
|
write(obj)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def poll2(timeout=0.0, map=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
import poll
|
|
|
|
if map is None:
|
2003-04-08 09:04:57 +02:00
|
|
|
map = socket_map
|
2003-03-12 07:26:59 +01:00
|
|
|
if timeout is not None:
|
|
|
|
# timeout is in milliseconds
|
|
|
|
timeout = int(timeout*1000)
|
|
|
|
if map:
|
|
|
|
l = []
|
2003-04-08 09:04:57 +02:00
|
|
|
for fd, obj in map.items():
|
2003-03-12 07:26:59 +01:00
|
|
|
flags = 0
|
|
|
|
if obj.readable():
|
|
|
|
flags = poll.POLLIN
|
|
|
|
if obj.writable():
|
|
|
|
flags = flags | poll.POLLOUT
|
|
|
|
if flags:
|
2003-04-08 09:04:57 +02:00
|
|
|
l.append((fd, flags))
|
|
|
|
r = poll.poll(l, timeout)
|
2003-03-12 07:26:59 +01:00
|
|
|
for fd, flags in r:
|
2003-04-08 09:04:57 +02:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2003-03-12 07:26:59 +01:00
|
|
|
continue
|
2003-04-08 09:04:57 +02:00
|
|
|
readwrite(obj, flags)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def poll3(timeout=0.0, map=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
# Use the poll() support added to the select module in Python 2.0
|
|
|
|
if map is None:
|
2003-04-08 09:04:57 +02:00
|
|
|
map = socket_map
|
2003-03-12 07:26:59 +01:00
|
|
|
if timeout is not None:
|
|
|
|
# timeout is in milliseconds
|
|
|
|
timeout = int(timeout*1000)
|
|
|
|
pollster = select.poll()
|
|
|
|
if map:
|
2003-04-08 09:04:57 +02:00
|
|
|
for fd, obj in map.items():
|
2003-03-12 07:26:59 +01:00
|
|
|
flags = 0
|
|
|
|
if obj.readable():
|
|
|
|
flags = select.POLLIN
|
|
|
|
if obj.writable():
|
|
|
|
flags = flags | select.POLLOUT
|
|
|
|
if flags:
|
|
|
|
pollster.register(fd, flags)
|
|
|
|
try:
|
2003-04-08 09:04:57 +02:00
|
|
|
r = pollster.poll(timeout)
|
2003-03-12 07:26:59 +01:00
|
|
|
except select.error, err:
|
|
|
|
if err[0] != EINTR:
|
|
|
|
raise
|
|
|
|
r = []
|
|
|
|
for fd, flags in r:
|
2003-04-08 09:04:57 +02:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2003-03-12 07:26:59 +01:00
|
|
|
continue
|
2003-04-08 09:04:57 +02:00
|
|
|
readwrite(obj, flags)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def loop(timeout=30.0, use_poll=0, map=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
if map is None:
|
2003-04-08 09:04:57 +02:00
|
|
|
map = socket_map
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
if use_poll:
|
2003-04-08 09:04:57 +02:00
|
|
|
if hasattr(select, 'poll'):
|
2003-03-12 07:26:59 +01:00
|
|
|
poll_fun = poll3
|
|
|
|
else:
|
|
|
|
poll_fun = poll2
|
|
|
|
else:
|
|
|
|
poll_fun = poll
|
|
|
|
|
|
|
|
while map:
|
2003-04-08 09:04:57 +02:00
|
|
|
poll_fun(timeout, map)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
class dispatcher:
|
2003-04-08 09:04:57 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
debug = 0
|
|
|
|
connected = 0
|
|
|
|
accepting = 0
|
|
|
|
closing = 0
|
|
|
|
addr = None
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def __init__(self, sock=None, map=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
if sock:
|
2003-04-08 09:04:57 +02:00
|
|
|
self.set_socket(sock, map)
|
2003-03-12 07:26:59 +01:00
|
|
|
# I think it should inherit this anyway
|
2003-04-08 09:04:57 +02:00
|
|
|
self.socket.setblocking(0)
|
2003-03-12 07:26:59 +01:00
|
|
|
self.connected = 1
|
|
|
|
# XXX Does the constructor require that the socket passed
|
|
|
|
# be connected?
|
|
|
|
try:
|
|
|
|
self.addr = sock.getpeername()
|
|
|
|
except socket.error:
|
|
|
|
# The addr isn't crucial
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.socket = None
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def __repr__(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
status = [self.__class__.__module__+"."+self.__class__.__name__]
|
|
|
|
if self.accepting and self.addr:
|
2003-04-08 09:04:57 +02:00
|
|
|
status.append('listening')
|
2003-03-12 07:26:59 +01:00
|
|
|
elif self.connected:
|
2003-04-08 09:04:57 +02:00
|
|
|
status.append('connected')
|
2003-03-12 07:26:59 +01:00
|
|
|
if self.addr is not None:
|
|
|
|
try:
|
2003-04-08 09:04:57 +02:00
|
|
|
status.append('%s:%d' % self.addr)
|
2003-03-12 07:26:59 +01:00
|
|
|
except TypeError:
|
2003-04-08 09:04:57 +02:00
|
|
|
status.append(repr(self.addr))
|
|
|
|
return '<%s at %#x>' % (' '.join(status), id(self))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def add_channel(self, map=None):
|
|
|
|
#self.log_info('adding channel %s' % self)
|
2003-03-12 07:26:59 +01:00
|
|
|
if map is None:
|
2003-04-08 09:04:57 +02:00
|
|
|
map = socket_map
|
|
|
|
map[self._fileno] = self
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def del_channel(self, map=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
fd = self._fileno
|
|
|
|
if map is None:
|
2003-04-08 09:04:57 +02:00
|
|
|
map = socket_map
|
|
|
|
if map.has_key(fd):
|
|
|
|
#self.log_info('closing channel %d:%s' % (fd, self))
|
|
|
|
del map[fd]
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def create_socket(self, family, type):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.family_and_type = family, type
|
2003-04-08 09:04:57 +02:00
|
|
|
self.socket = socket.socket(family, type)
|
2003-03-12 07:26:59 +01:00
|
|
|
self.socket.setblocking(0)
|
|
|
|
self._fileno = self.socket.fileno()
|
|
|
|
self.add_channel()
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def set_socket(self, sock, map=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.socket = sock
|
|
|
|
## self.__dict__['socket'] = sock
|
|
|
|
self._fileno = sock.fileno()
|
2003-04-08 09:04:57 +02:00
|
|
|
self.add_channel(map)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def set_reuse_addr(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
# try to re-use a server port if possible
|
|
|
|
try:
|
2003-04-08 09:04:57 +02:00
|
|
|
self.socket.setsockopt(
|
2003-03-12 07:26:59 +01:00
|
|
|
socket.SOL_SOCKET, socket.SO_REUSEADDR,
|
2003-04-08 09:04:57 +02:00
|
|
|
self.socket.getsockopt(socket.SOL_SOCKET,
|
|
|
|
socket.SO_REUSEADDR) | 1
|
2003-03-12 07:26:59 +01:00
|
|
|
)
|
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
# predicates for select()
|
|
|
|
# these are used as filters for the lists of sockets
|
|
|
|
# to pass to select().
|
|
|
|
# ==================================================
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def readable(self):
|
|
|
|
return True
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
if os.name == 'mac':
|
|
|
|
# The macintosh will select a listening socket for
|
|
|
|
# write if you let it. What might this mean?
|
2003-04-08 09:04:57 +02:00
|
|
|
def writable(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
return not self.accepting
|
|
|
|
else:
|
2003-04-08 09:04:57 +02:00
|
|
|
def writable(self):
|
|
|
|
return True
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
# socket object methods.
|
|
|
|
# ==================================================
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def listen(self, num):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.accepting = 1
|
|
|
|
if os.name == 'nt' and num > 5:
|
|
|
|
num = 1
|
2003-04-08 09:04:57 +02:00
|
|
|
return self.socket.listen(num)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def bind(self, addr):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.addr = addr
|
2003-04-08 09:04:57 +02:00
|
|
|
return self.socket.bind(addr)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def connect(self, address):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.connected = 0
|
|
|
|
err = self.socket.connect_ex(address)
|
2003-04-08 09:04:57 +02:00
|
|
|
# XXX Should interpret Winsock return values
|
2003-03-12 07:26:59 +01:00
|
|
|
if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
|
|
|
|
return
|
|
|
|
if err in (0, EISCONN):
|
|
|
|
self.addr = address
|
|
|
|
self.connected = 1
|
|
|
|
self.handle_connect()
|
|
|
|
else:
|
|
|
|
raise socket.error, err
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def accept(self):
|
|
|
|
# XXX can return either an address pair or None
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
|
|
|
conn, addr = self.socket.accept()
|
|
|
|
return conn, addr
|
|
|
|
except socket.error, why:
|
|
|
|
if why[0] == EWOULDBLOCK:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise socket.error, why
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def send(self, data):
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
2003-04-08 09:04:57 +02:00
|
|
|
result = self.socket.send(data)
|
2003-03-12 07:26:59 +01:00
|
|
|
return result
|
|
|
|
except socket.error, why:
|
|
|
|
if why[0] == EWOULDBLOCK:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
raise socket.error, why
|
|
|
|
return 0
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def recv(self, buffer_size):
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
2003-04-08 09:04:57 +02:00
|
|
|
data = self.socket.recv(buffer_size)
|
2003-03-12 07:26:59 +01:00
|
|
|
if not data:
|
|
|
|
# a closed connection is indicated by signaling
|
|
|
|
# a read condition, and having recv() return 0.
|
|
|
|
self.handle_close()
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return data
|
|
|
|
except socket.error, why:
|
|
|
|
# winsock sometimes throws ENOTCONN
|
|
|
|
if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]:
|
|
|
|
self.handle_close()
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
raise socket.error, why
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def close(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.del_channel()
|
|
|
|
self.socket.close()
|
|
|
|
|
|
|
|
# cheap inheritance, used to pass all other attribute
|
|
|
|
# references to the underlying socket object.
|
2003-04-08 09:04:57 +02:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.socket, attr)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
# log and log_info may be overridden to provide more sophisticated
|
2003-03-12 07:26:59 +01:00
|
|
|
# logging and warning methods. In general, log is for 'hit' logging
|
|
|
|
# and 'log_info' is for informational, warning and error logging.
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def log(self, message):
|
|
|
|
sys.stderr.write('log: %s\n' % str(message))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def log_info(self, message, type='info'):
|
2003-03-12 07:26:59 +01:00
|
|
|
if __debug__ or type != 'info':
|
|
|
|
print '%s: %s' % (type, message)
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_read_event(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
if self.accepting:
|
|
|
|
# for an accepting socket, getting a read implies
|
|
|
|
# that we are connected
|
|
|
|
if not self.connected:
|
|
|
|
self.connected = 1
|
|
|
|
self.handle_accept()
|
|
|
|
elif not self.connected:
|
|
|
|
self.handle_connect()
|
|
|
|
self.connected = 1
|
|
|
|
self.handle_read()
|
|
|
|
else:
|
|
|
|
self.handle_read()
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_write_event(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
# getting a write implies that we are connected
|
|
|
|
if not self.connected:
|
|
|
|
self.handle_connect()
|
|
|
|
self.connected = 1
|
|
|
|
self.handle_write()
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_expt_event(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.handle_expt()
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_error(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
nil, t, v, tbinfo = compact_traceback()
|
|
|
|
|
|
|
|
# sometimes a user repr method will crash.
|
|
|
|
try:
|
2003-04-08 09:04:57 +02:00
|
|
|
self_repr = repr(self)
|
2003-03-12 07:26:59 +01:00
|
|
|
except:
|
2003-04-08 09:04:57 +02:00
|
|
|
self_repr = '<__repr__(self) failed for object at %0x>' % id(self)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
self.log_info(
|
2003-03-12 07:26:59 +01:00
|
|
|
'uncaptured python exception, closing channel %s (%s:%s %s)' % (
|
|
|
|
self_repr,
|
|
|
|
t,
|
|
|
|
v,
|
|
|
|
tbinfo
|
|
|
|
),
|
|
|
|
'error'
|
|
|
|
)
|
|
|
|
self.close()
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_expt(self):
|
|
|
|
self.log_info('unhandled exception', 'warning')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_read(self):
|
|
|
|
self.log_info('unhandled read event', 'warning')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_write(self):
|
|
|
|
self.log_info('unhandled write event', 'warning')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_connect(self):
|
|
|
|
self.log_info('unhandled connect event', 'warning')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_accept(self):
|
|
|
|
self.log_info('unhandled accept event', 'warning')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_close(self):
|
|
|
|
self.log_info('unhandled close event', 'warning')
|
2003-03-12 07:26:59 +01:00
|
|
|
self.close()
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# adds simple buffered output capability, useful for simple clients.
|
|
|
|
# [for more sophisticated usage use asynchat.async_chat]
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
class dispatcher_with_send(dispatcher):
|
|
|
|
|
|
|
|
def __init__(self, sock=None):
|
|
|
|
dispatcher.__init__(self, sock)
|
2003-03-12 07:26:59 +01:00
|
|
|
self.out_buffer = ''
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def initiate_send(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
num_sent = 0
|
2003-04-08 09:04:57 +02:00
|
|
|
num_sent = dispatcher.send(self, self.out_buffer[:512])
|
2003-03-12 07:26:59 +01:00
|
|
|
self.out_buffer = self.out_buffer[num_sent:]
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def handle_write(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.initiate_send()
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def writable(self):
|
2003-03-12 07:26:59 +01:00
|
|
|
return (not self.connected) or len(self.out_buffer)
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def send(self, data):
|
2003-03-12 07:26:59 +01:00
|
|
|
if self.debug:
|
2003-04-08 09:04:57 +02:00
|
|
|
self.log_info('sending %s' % repr(data))
|
2003-03-12 07:26:59 +01:00
|
|
|
self.out_buffer = self.out_buffer + data
|
|
|
|
self.initiate_send()
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# used for debugging.
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def compact_traceback():
|
|
|
|
t, v, tb = sys.exc_info()
|
2003-03-12 07:26:59 +01:00
|
|
|
tbinfo = []
|
2003-04-08 09:04:57 +02:00
|
|
|
assert tb # Must have a traceback
|
|
|
|
while tb:
|
|
|
|
tbinfo.append((
|
2003-03-12 07:26:59 +01:00
|
|
|
tb.tb_frame.f_code.co_filename,
|
|
|
|
tb.tb_frame.f_code.co_name,
|
|
|
|
str(tb.tb_lineno)
|
|
|
|
))
|
|
|
|
tb = tb.tb_next
|
|
|
|
|
|
|
|
# just to be safe
|
|
|
|
del tb
|
|
|
|
|
|
|
|
file, function, line = tbinfo[-1]
|
2003-04-08 09:04:57 +02:00
|
|
|
info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo])
|
2003-03-12 07:26:59 +01:00
|
|
|
return (file, function, line), t, v, info
|
|
|
|
|
2003-04-08 09:04:57 +02:00
|
|
|
def close_all(map=None):
|
2003-03-12 07:26:59 +01:00
|
|
|
if map is None:
|
2003-04-08 09:04:57 +02:00
|
|
|
map = socket_map
|
|
|
|
for x in map.values():
|
2003-03-12 07:26:59 +01:00
|
|
|
x.socket.close()
|
|
|
|
map.clear()
|