SocketScience.py :  » Database » PyDO » skunkweb-3.4.4 » pylibs » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyDO 
PyDO » skunkweb 3.4.4 » pylibs » SocketScience.py
#  
#  Copyright (C) 2001 Andrew T. Csillag <drew_csillag@geocities.com>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
#   
"""
"Takes some of the rocket science out of socket science."

Normally, calling the recv method on a socket 
object doesn't guarantee that you'll get as many bytes 
as you ask for.  Because of this fact (which is because UNIX 
works this way), this module makes it easy to say, "I want X
number of bytes in N seconds or bust."
"""

import signal 
import socket
import errno

from SkunkExcept import *

# Allow catching of timeout
class SocketTimeoutError ( SkunkRuntimeError ):
    pass

_got_alrm = 0

def _sigalrm_handler ( sig, frame ):
    """Reset the signal handler -- called if the read times out."""

    # XXX for some reason, this doesn't propagate to main thread - don't ask me
    global _got_arlm
    _got_alrm = 1

    signal.signal ( signal.SIGALRM, signal.SIG_DFL )

def read_this_many ( sock, length, timeout = 0 ):
    """
    Promises to read length bytes from sock, or bust
    (we timed out), where bust means 'raise SkunkTimeoutError', or,
    if we just get 0 bytes, we raise SkunkCriticalError instead since this
    usually indicates that the connection is toast.
    """
    global _got_alrm

    dat=''
    if timeout > 0:
        signal.signal ( signal.SIGALRM, _sigalrm_handler )
        signal.alarm ( timeout )

    while len(dat) < length:
        try:
            sock.setblocking(1)
            rd=sock.recv(length - len(dat))
        except socket.error, (err, errstr):
            if timeout > 0 and err == errno.EINTR:
                raise SocketTimeoutError, 'timed out reading from socket'
            
            # re-raise
            raise

        if len(rd) == 0:
            raise SkunkCriticalError, \
                      "socket: got 0 bytes this read, %s total, wanted %s" % \
                      (len(dat), length)

        dat=dat+rd

    # Clear alarm if was set
    if timeout > 0:
        signal.alarm ( 0 )
        signal.signal ( signal.SIGALRM, signal.SIG_DFL )

    return dat

def send_it_all(sock, s):
    """Promises to write all of s to the sock, or bust.  If we for some reason
    write 0 bytes at any time, we raise SkunkRuntimeError because this usually
    indicates that the connection has died."""
    sentlen=0
    len_s=len(s)
    while sentlen < len_s:
        thislen=sock.send(s[sentlen:])
        if thislen == 0:
            raise SkunkRuntimeError, \
                  "wrote 0 bytes this write, %s total, wanted %s" % \
                  (sentlen, len_s)
        sentlen=sentlen+thislen
    return sentlen

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.