Exemple #1
0
    def handle_request(self, c):
        funcname = result = request = None
        try:
            connection.deliver_challenge(c, self.authkey)
            connection.answer_challenge(c, self.authkey)
            request = c.recv()
            ignore, funcname, args, kwds = request
            assert funcname in self.public, '%r unrecognized' % funcname
            func = getattr(self, funcname)
        except Exception:
            msg = ('#TRACEBACK', format_exc())
        else:
            try:
                result = func(c, *args, **kwds)
            except Exception:
                msg = ('#TRACEBACK', format_exc())
            else:
                msg = ('#RETURN', result)

        try:
            c.send(msg)
        except Exception as e:
            try:
                c.send(('#TRACEBACK', format_exc()))
            except Exception:
                pass

            util.info('Failure to send message: %r', msg)
            util.info(' ... request was %r', request)
            util.info(' ... exception was %r', e)

        c.close()
Exemple #2
0
    def ClientWithTimeout(self, address, authkey, timeout):
        with socket.socket(socket.AF_INET) as s:
            s.setblocking(True)
            s.connect(address)

            # We'd like to call s.settimeout(timeout) here, but that won't work.

            # Instead, prepare a C "struct timeval" to specify timeout. Note that
            # these field sizes may differ by platform.
            seconds = int(timeout)
            microseconds = int((timeout - seconds) * 1e6)
            timeval = struct.pack("@LL", seconds, microseconds)

            # And then set the SO_RCVTIMEO (receive timeout) option with this.
            s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeval)

            # Now create the connection as normal.
            c = Connection(s.detach())

        # The following code will now fail if a socket timeout occurs.

        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)

        return c
    def handle_request(self, c):
        funcname = result = request = None
        try:
            connection.deliver_challenge(c, self.authkey)
            connection.answer_challenge(c, self.authkey)
            request = c.recv()
            ignore, funcname, args, kwds = request
            func = getattr(self, funcname)
        except Exception:
            msg = ('#TRACEBACK', format_exc())
        else:
            try:
                result = func(c, *args, **kwds)
            except Exception:
                msg = ('#TRACEBACK', format_exc())
            else:
                msg = ('#RETURN', result)

        try:
            c.send(msg)
        except Exception as e:
            try:
                c.send(('#TRACEBACK', format_exc()))
            except Exception:
                pass

            util.info('Failure to send message: %r', msg)
            util.info(' ... request was %r', request)
            util.info(' ... exception was %r', e)

        c.close()
        return
Exemple #4
0
def connect_node(node):
    try:

        #connect
        address = (node[0], int(node[1]))
        connect = Client(address, authkey="hi")
        connect.send('Hello from supervisor process')

        #check connection
        try:
            deliver_challenge(connect, authkey="hi")
            print "Node {} connected".format(node)
            node[2] = True
            return connect

        except EOFError:
            traceback.print_exc()
            print "Could not connect to node {} {}".format(node[0], node[1])
            node[2] = False
            con.close()

    except:
        traceback.print_exc()
        print "Could not connect to node {} {}".format(node[0], node[1])
        node[2] = False
 def handle_request(self, c):
     '''
     Handle a new connection
     '''
     funcname = result = request = None
     try:
         connection.deliver_challenge(c, self.authkey)
         connection.answer_challenge(c, self.authkey)
         request = c.recv()
         ignore, funcname, args, kwds = request
         assert funcname in self.public, '%r unrecognized' % funcname
         func = getattr(self, funcname)
     except Exception:
         msg = ('#TRACEBACK', format_exc())
     else:
         try:
             result = func(c, *args, **kwds)
         except Exception:
             msg = ('#TRACEBACK', format_exc())
         else:
             msg = ('#RETURN', result)
     try:
         c.send(msg)
     except Exception, e:
         try:
             c.send(('#TRACEBACK', format_exc()))
         except Exception:
             pass
         util.info('Failure to send message: %r', msg)
         util.info(' ... request was %r', request)
         util.info(' ... exception was %r', e)
Exemple #6
0
 def __init__(self, address, authkey=None):
     sock = socket.socket(socket.AF_UNIX)
     sock.setblocking(True)
     sock.connect(address)
     super(JsonClient, self).__init__(sock)
     if authkey is not None:
         connection.answer_challenge(self, authkey)
         connection.deliver_challenge(self, authkey)
Exemple #7
0
 def __init__(self, address, authkey=None):
     sock = socket.socket(socket.AF_UNIX)
     sock.setblocking(True)
     sock.connect(address)
     super(JsonClient, self).__init__(sock)
     if authkey is not None:
         connection.answer_challenge(self, authkey)
         connection.deliver_challenge(self, authkey)
Exemple #8
0
def _Client(address, authkey=None):
    """ Returns a connection to the address of a `Listener` """
    c = _SocketClient(address)
    if authkey is not None:
        if not isinstance(authkey, bytes):
            raise TypeError("Expected a byte string as an authentication key")
        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)
    return c
Exemple #9
0
def _Client(address, authkey=None):
    """ Returns a connection to the address of a `Listener` """
    c = _SocketClient(address)
    if authkey is not None:
        if not isinstance(authkey, bytes):
            raise TypeError("Expected a byte string as an authentication key")
        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)
    return c
Exemple #10
0
def check_nodes(nodes, inactive_nodes, connections):
    for con, node in zip(connections, nodes):
        try:
            deliver_challenge(con, authkey="hi")
            print "{} is connected".format(node)
        except EOFError:
            print "{} is not connected".format(node)
            node[2] = False
            con.close()
            connections.remove(con)
            nodes.remove(node)
            inactive_nodes.append(node)
Exemple #11
0
def Client(address, family=None, authkey=None, timeout=None):
    family = family or address_type(address)
    if family == 'AF_PIPE':
        c = PipeClient(address, timeout=timeout)
    else:
        c = SocketClient(address, timeout=timeout)

    if authkey is not None and not isinstance(authkey, bytes):
        raise TypeError, 'authkey should be a byte string'

    if authkey is not None:
        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)
    return c
Exemple #12
0
            except socket.error, e:
                if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
                    debug('failed to connect to address %s', address)
                    raise
                time.sleep(0.01)
            else:
                break
        else:
            raise

        fd = duplicate(s.fileno())
        conn = _multiprocessing.Connection(fd)
        s.close()
        ## authenticate
        answer_challenge(conn, authkey)
        deliver_challenge(conn, authkey)
        return conn
    ## end of copy

    def connectClientIPv6(address, authkey):
        try:
            client = SocketClientv6(address, authkey)
        except socket.error:
            return None
        return SocketConnection(client)

else:
    def connectClientIPv6(*args):
        return BrokenConnection()

if has_pipe: