Back to index

python3.2  3.2.2
Classes | Functions | Variables
asyncore Namespace Reference

Classes

class  ExitNow
class  dispatcher
class  dispatcher_with_send
class  file_wrapper
class  file_dispatcher

Functions

def _strerror
def read
def write
def _exception
def readwrite
def poll
def poll2
def loop
def set_reuse_addr
 self.__dict__['socket'] = sock
def readable
def writable
def listen
def bind
def connect
def accept
def send
def recv
def close
def __getattr__
def log
def log_info
def handle_read_event
def handle_connect_event
def handle_write_event
def handle_expt_event
def handle_error
def handle_expt
def handle_read
def handle_write
def handle_connect
def handle_accept
def handle_accepted
def handle_close
def compact_traceback
def close_all

Variables

tuple _DISCONNECTED
dictionary socket_map = {}
tuple _reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit)
 poll3 = poll2
 accepting
 addr
 connected

Function Documentation

def asyncore.__getattr__ (   self,
  attr 
)

Definition at line 412 of file asyncore.py.

00412 
00413     def __getattr__(self, attr):
00414         try:
00415             retattr = getattr(self.socket, attr)
00416         except AttributeError:
00417             raise AttributeError("%s instance has no attribute '%s'"
00418                                  %(self.__class__.__name__, attr))
00419         else:
00420             msg = "%(me)s.%(attr)s is deprecated; use %(me)s.socket.%(attr)s " \
00421                   "instead" % {'me' : self.__class__.__name__, 'attr' : attr}
00422             warnings.warn(msg, DeprecationWarning, stacklevel=2)
00423             return retattr

Here is the call graph for this function:

Here is the caller graph for this function:

def asyncore._exception (   obj) [private]

Definition at line 97 of file asyncore.py.

00097 
00098 def _exception(obj):
00099     try:
00100         obj.handle_expt_event()
00101     except _reraised_exceptions:
00102         raise
00103     except:
00104         obj.handle_error()

Here is the caller graph for this function:

def asyncore._strerror (   err) [private]

Definition at line 68 of file asyncore.py.

00068 
00069 def _strerror(err):
00070     try:
00071         return os.strerror(err)
00072     except (ValueError, OverflowError, NameError):
00073         if err in errorcode:
00074             return errorcode[err]
00075         return "Unknown error %s" %err

Here is the caller graph for this function:

def asyncore.accept (   self)

Definition at line 355 of file asyncore.py.

00355 
00356     def accept(self):
00357         # XXX can return either an address pair or None
00358         try:
00359             conn, addr = self.socket.accept()
00360         except TypeError:
00361             return None
00362         except socket.error as why:
00363             if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN):
00364                 return None
00365             else:
00366                 raise
00367         else:
00368             return conn, addr

Here is the caller graph for this function:

def asyncore.bind (   self,
  addr 
)

Definition at line 339 of file asyncore.py.

00339 
00340     def bind(self, addr):
00341         self.addr = addr
00342         return self.socket.bind(addr)

Here is the caller graph for this function:

def asyncore.close (   self)

Definition at line 400 of file asyncore.py.

00400 
00401     def close(self):
00402         self.connected = False
00403         self.accepting = False
00404         self.del_channel()
00405         try:
00406             self.socket.close()
00407         except socket.error as why:
00408             if why.args[0] not in (ENOTCONN, EBADF):
00409                 raise

def asyncore.close_all (   map = None,
  ignore_all = False 
)

Definition at line 580 of file asyncore.py.

00580 
00581 def close_all(map=None, ignore_all=False):
00582     if map is None:
00583         map = socket_map
00584     for x in list(map.values()):
00585         try:
00586             x.close()
00587         except OSError as x:
00588             if x.args[0] == EBADF:
00589                 pass
00590             elif not ignore_all:
00591                 raise
00592         except _reraised_exceptions:
00593             raise
00594         except:
00595             if not ignore_all:
00596                 raise
00597     map.clear()
00598 
00599 # Asynchronous File I/O:
00600 #
00601 # After a little research (reading man pages on various unixen, and
00602 # digging through the linux kernel), I've determined that select()
00603 # isn't meant for doing asynchronous file i/o.
00604 # Heartening, though - reading linux/mm/filemap.c shows that linux
00605 # supports asynchronous read-ahead.  So _MOST_ of the time, the data
00606 # will be sitting in memory for us already when we go to read it.
00607 #
00608 # What other OS's (besides NT) support async file i/o?  [VMS?]
00609 #
00610 # Regardless, this is useful for pipes, and stdin/stdout...

Here is the caller graph for this function:

Definition at line 560 of file asyncore.py.

00560 
00561 def compact_traceback():
00562     t, v, tb = sys.exc_info()
00563     tbinfo = []
00564     if not tb: # Must have a traceback
00565         raise AssertionError("traceback does not exist")
00566     while tb:
00567         tbinfo.append((
00568             tb.tb_frame.f_code.co_filename,
00569             tb.tb_frame.f_code.co_name,
00570             str(tb.tb_lineno)
00571             ))
00572         tb = tb.tb_next
00573 
00574     # just to be safe
00575     del tb
00576 
00577     file, function, line = tbinfo[-1]
00578     info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo])
00579     return (file, function, line), t, v, info

Here is the caller graph for this function:

def asyncore.connect (   self,
  address 
)

Definition at line 343 of file asyncore.py.

00343 
00344     def connect(self, address):
00345         self.connected = False
00346         err = self.socket.connect_ex(address)
00347         if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) \
00348         or err == EINVAL and os.name in ('nt', 'ce'):
00349             return
00350         if err in (0, EISCONN):
00351             self.addr = address
00352             self.handle_connect_event()
00353         else:
00354             raise socket.error(err, errorcode[err])

Here is the caller graph for this function:

def asyncore.handle_accept (   self)

Definition at line 515 of file asyncore.py.

00515 
00516     def handle_accept(self):
00517         pair = self.accept()
00518         if pair is not None:
00519             self.handle_accepted(*pair)

Here is the caller graph for this function:

def asyncore.handle_accepted (   self,
  sock,
  addr 
)

Definition at line 520 of file asyncore.py.

00520 
00521     def handle_accepted(self, sock, addr):
00522         sock.close()
00523         self.log_info('unhandled accepted event', 'warning')

Here is the caller graph for this function:

def asyncore.handle_close (   self)

Definition at line 524 of file asyncore.py.

00524 
00525     def handle_close(self):
00526         self.log_info('unhandled close event', 'warning')
00527         self.close()
00528 
00529 # ---------------------------------------------------------------------------
00530 # adds simple buffered output capability, useful for simple clients.
00531 # [for more sophisticated usage use asynchat.async_chat]
00532 # ---------------------------------------------------------------------------

Here is the caller graph for this function:

def asyncore.handle_connect (   self)

Definition at line 512 of file asyncore.py.

00512 
00513     def handle_connect(self):
00514         self.log_info('unhandled connect event', 'warning')

Here is the caller graph for this function:

Definition at line 446 of file asyncore.py.

00446 
00447     def handle_connect_event(self):
00448         err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
00449         if err != 0:
00450             raise socket.error(err, _strerror(err))
00451         self.handle_connect()
00452         self.connected = True

Here is the call graph for this function:

def asyncore.handle_error (   self)

Definition at line 483 of file asyncore.py.

00483 
00484     def handle_error(self):
00485         nil, t, v, tbinfo = compact_traceback()
00486 
00487         # sometimes a user repr method will crash.
00488         try:
00489             self_repr = repr(self)
00490         except:
00491             self_repr = '<__repr__(self) failed for object at %0x>' % id(self)
00492 
00493         self.log_info(
00494             'uncaptured python exception, closing channel %s (%s:%s %s)' % (
00495                 self_repr,
00496                 t,
00497                 v,
00498                 tbinfo
00499                 ),
00500             'error'
00501             )
00502         self.handle_close()

Here is the call graph for this function:

Here is the caller graph for this function:

def asyncore.handle_expt (   self)

Definition at line 503 of file asyncore.py.

00503 
00504     def handle_expt(self):
00505         self.log_info('unhandled incoming priority event', 'warning')

Here is the caller graph for this function:

Definition at line 468 of file asyncore.py.

00468 
00469     def handle_expt_event(self):
00470         # handle_expt_event() is called if there might be an error on the
00471         # socket, or if there is OOB data
00472         # check for the error condition first
00473         err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
00474         if err != 0:
00475             # we can get here when select.select() says that there is an
00476             # exceptional condition on the socket
00477             # since there is an error, we'll go ahead and close the socket
00478             # like we would in a subclassed handle_read() that received no
00479             # data
00480             self.handle_close()
00481         else:
00482             self.handle_expt()

def asyncore.handle_read (   self)

Definition at line 506 of file asyncore.py.

00506 
00507     def handle_read(self):
00508         self.log_info('unhandled read event', 'warning')

Here is the caller graph for this function:

Definition at line 435 of file asyncore.py.

00435 
00436     def handle_read_event(self):
00437         if self.accepting:
00438             # accepting sockets are never connected, they "spawn" new
00439             # sockets that are connected
00440             self.handle_accept()
00441         elif not self.connected:
00442             self.handle_connect_event()
00443             self.handle_read()
00444         else:
00445             self.handle_read()

def asyncore.handle_write (   self)

Definition at line 509 of file asyncore.py.

00509 
00510     def handle_write(self):
00511         self.log_info('unhandled write event', 'warning')

Here is the caller graph for this function:

Definition at line 453 of file asyncore.py.

00453 
00454     def handle_write_event(self):
00455         if self.accepting:
00456             # Accepting sockets shouldn't get a write event.
00457             # We will pretend it didn't happen.
00458             return
00459 
00460         if not self.connected:
00461             #check for errors
00462             err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
00463             if err != 0:
00464                 raise socket.error(err, _strerror(err))
00465 
00466             self.handle_connect_event()
00467         self.handle_write()

Here is the call graph for this function:

def asyncore.listen (   self,
  num 
)

Definition at line 333 of file asyncore.py.

00333 
00334     def listen(self, num):
00335         self.accepting = True
00336         if os.name == 'nt' and num > 5:
00337             num = 5
00338         return self.socket.listen(num)

Here is the caller graph for this function:

def asyncore.log (   self,
  message 
)

Definition at line 428 of file asyncore.py.

00428 
00429     def log(self, message):
00430         sys.stderr.write('log: %s\n' % str(message))

def asyncore.log_info (   self,
  message,
  type = 'info' 
)

Definition at line 431 of file asyncore.py.

00431 
00432     def log_info(self, message, type='info'):
00433         if type not in self.ignore_log_types:
00434             print('%s: %s' % (type, message))

def asyncore.loop (   timeout = 30.0,
  use_poll = False,
  map = None,
  count = None 
)

Definition at line 205 of file asyncore.py.

00205 
00206 def loop(timeout=30.0, use_poll=False, map=None, count=None):
00207     if map is None:
00208         map = socket_map
00209 
00210     if use_poll and hasattr(select, 'poll'):
00211         poll_fun = poll2
00212     else:
00213         poll_fun = poll
00214 
00215     if count is None:
00216         while map:
00217             poll_fun(timeout, map)
00218 
00219     else:
00220         while map and count > 0:
00221             poll_fun(timeout, map)
00222             count = count - 1

Here is the caller graph for this function:

def asyncore.poll (   timeout = 0.0,
  map = None 
)

Definition at line 125 of file asyncore.py.

00125 
00126 def poll(timeout=0.0, map=None):
00127     if map is None:
00128         map = socket_map
00129     if map:
00130         r = []; w = []; e = []
00131         for fd, obj in list(map.items()):
00132             is_r = obj.readable()
00133             is_w = obj.writable()
00134             if is_r:
00135                 r.append(fd)
00136             # accepting sockets should not be writable
00137             if is_w and not obj.accepting:
00138                 w.append(fd)
00139             if is_r or is_w:
00140                 e.append(fd)
00141         if [] == r == w == e:
00142             time.sleep(timeout)
00143             return
00144 
00145         try:
00146             r, w, e = select.select(r, w, e, timeout)
00147         except select.error as err:
00148             if err.args[0] != EINTR:
00149                 raise
00150             else:
00151                 return
00152 
00153         for fd in r:
00154             obj = map.get(fd)
00155             if obj is None:
00156                 continue
00157             read(obj)
00158 
00159         for fd in w:
00160             obj = map.get(fd)
00161             if obj is None:
00162                 continue
00163             write(obj)
00164 
00165         for fd in e:
00166             obj = map.get(fd)
00167             if obj is None:
00168                 continue
00169             _exception(obj)

Here is the call graph for this function:

Here is the caller graph for this function:

def asyncore.poll2 (   timeout = 0.0,
  map = None 
)

Definition at line 170 of file asyncore.py.

00170 
00171 def poll2(timeout=0.0, map=None):
00172     # Use the poll() support added to the select module in Python 2.0
00173     if map is None:
00174         map = socket_map
00175     if timeout is not None:
00176         # timeout is in milliseconds
00177         timeout = int(timeout*1000)
00178     pollster = select.poll()
00179     if map:
00180         for fd, obj in list(map.items()):
00181             flags = 0
00182             if obj.readable():
00183                 flags |= select.POLLIN | select.POLLPRI
00184             # accepting sockets should not be writable
00185             if obj.writable() and not obj.accepting:
00186                 flags |= select.POLLOUT
00187             if flags:
00188                 # Only check for exceptions if object was either readable
00189                 # or writable.
00190                 flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL
00191                 pollster.register(fd, flags)
00192         try:
00193             r = pollster.poll(timeout)
00194         except select.error as err:
00195             if err.args[0] != EINTR:
00196                 raise
00197             r = []
00198         for fd, flags in r:
00199             obj = map.get(fd)
00200             if obj is None:
00201                 continue
00202             readwrite(obj, flags)

Here is the call graph for this function:

def asyncore.read (   obj)

Definition at line 81 of file asyncore.py.

00081 
00082 def read(obj):
00083     try:
00084         obj.handle_read_event()
00085     except _reraised_exceptions:
00086         raise
00087     except:
00088         obj.handle_error()

Here is the caller graph for this function:

def asyncore.readable (   self)

Definition at line 323 of file asyncore.py.

00323 
00324     def readable(self):
00325         return True

Here is the caller graph for this function:

def asyncore.readwrite (   obj,
  flags 
)

Definition at line 105 of file asyncore.py.

00105 
00106 def readwrite(obj, flags):
00107     try:
00108         if flags & select.POLLIN:
00109             obj.handle_read_event()
00110         if flags & select.POLLOUT:
00111             obj.handle_write_event()
00112         if flags & select.POLLPRI:
00113             obj.handle_expt_event()
00114         if flags & (select.POLLHUP | select.POLLERR | select.POLLNVAL):
00115             obj.handle_close()
00116     except socket.error as e:
00117         if e.args[0] not in _DISCONNECTED:
00118             obj.handle_error()
00119         else:
00120             obj.handle_close()
00121     except _reraised_exceptions:
00122         raise
00123     except:
00124         obj.handle_error()

Here is the caller graph for this function:

def asyncore.recv (   self,
  buffer_size 
)

Definition at line 382 of file asyncore.py.

00382 
00383     def recv(self, buffer_size):
00384         try:
00385             data = self.socket.recv(buffer_size)
00386             if not data:
00387                 # a closed connection is indicated by signaling
00388                 # a read condition, and having recv() return 0.
00389                 self.handle_close()
00390                 return b''
00391             else:
00392                 return data
00393         except socket.error as why:
00394             # winsock sometimes throws ENOTCONN
00395             if why.args[0] in _DISCONNECTED:
00396                 self.handle_close()
00397                 return b''
00398             else:
00399                 raise

Here is the caller graph for this function:

def asyncore.send (   self,
  data 
)

Definition at line 369 of file asyncore.py.

00369 
00370     def send(self, data):
00371         try:
00372             result = self.socket.send(data)
00373             return result
00374         except socket.error as why:
00375             if why.args[0] == EWOULDBLOCK:
00376                 return 0
00377             elif why.args[0] in _DISCONNECTED:
00378                 self.handle_close()
00379                 return 0
00380             else:
00381                 raise

Here is the caller graph for this function:

def asyncore.set_reuse_addr (   self)

self.__dict__['socket'] = sock

Definition at line 306 of file asyncore.py.

00306 
00307     def set_reuse_addr(self):
00308         # try to re-use a server port if possible
00309         try:
00310             self.socket.setsockopt(
00311                 socket.SOL_SOCKET, socket.SO_REUSEADDR,
00312                 self.socket.getsockopt(socket.SOL_SOCKET,
00313                                        socket.SO_REUSEADDR) | 1
00314                 )
00315         except socket.error:
00316             pass

def asyncore.writable (   self)

Definition at line 326 of file asyncore.py.

00326 
00327     def writable(self):
00328         return True

Here is the caller graph for this function:

def asyncore.write (   obj)

Definition at line 89 of file asyncore.py.

00089 
00090 def write(obj):
00091     try:
00092         obj.handle_write_event()
00093     except _reraised_exceptions:
00094         raise
00095     except:
00096         obj.handle_error()

Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE,
00002                            EBADF))

Definition at line 60 of file asyncore.py.

tuple asyncore._reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit)

Definition at line 79 of file asyncore.py.

Definition at line 334 of file asyncore.py.

Definition at line 340 of file asyncore.py.

Definition at line 344 of file asyncore.py.

Definition at line 203 of file asyncore.py.

Definition at line 66 of file asyncore.py.