Package CPSRSS :: Module timeoutsocket
[show private | hide private]
[frames | no frames]

Module CPSRSS.timeoutsocket

Timeout Socket

This module enables a timeout mechanism on all TCP connections.  It
does this by inserting a shim into the socket module.  After this module
has been imported, all socket creation goes through this shim.  As a
result, every TCP connection will support a timeout.

The beauty of this method is that it immediately and transparently
enables the entire python library to support timeouts on TCP sockets.
As an example, if you wanted to SMTP connections to have a 20 second
timeout:

    import timeoutsocket
    import smtplib
    timeoutsocket.setDefaultSocketTimeout(20)


The timeout applies to the socket functions that normally block on
execution:  read, write, connect, and accept.  If any of these 
operations exceeds the specified timeout, the exception Timeout
will be raised.

The default timeout value is set to None.  As a result, importing
this module does not change the default behavior of a socket.  The
timeout mechanism only activates when the timeout has been set to
a numeric value.  (This behavior mimics the behavior of the
select.select() function.)

This module implements two classes: TimeoutSocket and TimeoutFile.

The TimeoutSocket class defines a socket-like object that attempts to
avoid the condition where a socket may block indefinitely.  The
TimeoutSocket class raises a Timeout exception whenever the
current operation delays too long. 

The TimeoutFile class defines a file-like object that uses the TimeoutSocket
class.  When the makefile() method of TimeoutSocket is called, it returns
an instance of a TimeoutFile.

Each of these objects adds two methods to manage the timeout value:

    get_timeout()   -->  returns the timeout of the socket or file
    set_timeout()   -->  sets the timeout of the socket or file


As an example, one might use the timeout feature to create httplib
connections that will timeout after 30 seconds:

    import timeoutsocket
    import httplib
    H = httplib.HTTP("www.python.org")
    H.sock.set_timeout(30)

Note:  When used in this manner, the connect() routine may still
block because it happens before the timeout is set.  To avoid
this, use the 'timeoutsocket.setDefaultSocketTimeout()' function.

Good Luck!

Classes
TimeoutFile TimeoutFile object Implements a file-like object on top of TimeoutSocket.
TimeoutSocket TimeoutSocket object Implements a socket-like object that raises Timeout whenever an operation takes too long.

Exceptions
Timeout  

Function Summary
  getDefaultSocketTimeout()
  setDefaultSocketTimeout(timeout)
  socket(family, type, proto)
  timeoutsocket(family, type, proto)

Variable Summary
str __author__ = "Timothy O'Malley <timo@alum.mit.edu>"
str __version__ = '$Revision: 13190 $'

Variable Details

__author__

Type:
str
Value:
"Timothy O'Malley <timo@alum.mit.edu>"                                 

__version__

Type:
str
Value:
'$Revision: 13190 $'                                                   

Generated by Epydoc 2.1 on Mon Jun 27 12:48:04 2005 http://epydoc.sf.net