Exemplo n.º 1
0
    def __init__(self, address, async_val=False, verbose=0):
        threading.Thread.__init__(self)

        # Store whether to handle requests asynchronously
        self._async = async_val

        # Verbosity
        self._verbose = verbose

        # Determine host and port (assume tcp)
        protocol, host, port = split_address(address)

        # Create socket. Apply SO_REUSEADDR when binding, so that a
        # improperly closed socket on the same port will not prevent
        # us connecting.
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        # Bind (can raise error is port is not available)
        s.bind((host, port))

        # Store socket instance
        self._bsd_socket = s

        # To stop serving
        self._stop_me = False

        # Make deamon
        self.setDaemon(True)
Exemplo n.º 2
0
 def __init__(self, address, async_val=False, verbose=0):
     threading.Thread.__init__(self)
     
     # Store whether to handle requests asynchronously
     self._async = async_val
     
     # Verbosity
     self._verbose = verbose
     
     # Determine host and port (assume tcp)
     protocol, host, port = split_address(address)
     
     # Create socket. Apply SO_REUSEADDR when binding, so that a
     # improperly closed socket on the same port will not prevent
     # us connecting.
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     
     # Bind (can raise error is port is not available)
     s.bind((host,port))
     
     # Store socket instance
     self._bsd_socket = s
     
     # To stop serving
     self._stop_me = False
     
     # Make deamon
     self.setDaemon(True)
Exemplo n.º 3
0
    def connect(self, address, timeout=1.0, name=''):
        """ connect(self, address, timeout=1.0, name='')
        
        Setup a connection with another context, by connection to a 
        hosting context. An error is raised when the connection could
        not be made.
        
        Returns a Connection instance that represents the
        connection to the other context. These connection objects 
        can also be obtained via the Context.connections property.
        
        Parameters
        ----------
        address : str
            Should be of the shape hostname:port. The port should be an
            integer number between 1024 and 2**16. If port does not 
            represent a number, a valid port number is created using a 
            hash function.
        max_tries : int
            The number of ports to try; starting from the given port, 
            subsequent ports are tried until a free port is available. 
            The final port can be obtained using the 'port' property of
            the returned Connection instance.
        name : string
            The name for the created Connection instance. It can
            be used as a key in the connections property.
        
        Notes on hostname
        -----------------
        The hostname can be:
          * The IP address, or the string hostname of this computer. 
          * 'localhost': the connection is only visible from this computer. 
            Also some low level networking layers are bypassed, which results
            in a faster connection. The other context should also host as
            'localhost'.
          * 'publichost': the connection is visible by other computers on the 
            same network. Optionally an integer index can be appended if
            the machine has multiple IP addresses (see socket.gethostbyname_ex).
        
        """

        # Trigger cleanup of closed connections
        self.connections

        # Split address in protocol, real hostname and port number
        protocol, hostname, port = split_address(address)

        # Based on protocol, instantiate connection class (currently only tcp)
        if False:  #protocol == 'itc':
            connection = ItcConnection(self, name)
        else:
            connection = TcpConnection(self, name)

        # Create new connection and connect it
        connection._connect(hostname, port, timeout)

        # Save connection instance
        self._connections_lock.acquire()
        try:
            # Push packages from startup queue
            while self._startupQueue:
                connection._inject_package(self._startupQueue.pop())
            # Add connection object to list of connections
            self._connections.append(connection)
        finally:
            self._connections_lock.release()

        # Send message in the network to signal a new connection
        bb = 'NEW_CONNECTION'.encode('utf-8')
        p = Package(bb, SLOT_CONTEXT, self._id, 0, 0, 0, 0)
        self._send_package(p)

        # Return Connection instance
        return connection
Exemplo n.º 4
0
    def bind(self, address, max_tries=1, name=''):
        """ bind(address, max_tries=1, name='')
        
        Setup a connection with another Context, by being the host.
        This method starts a thread that waits for incoming connections.
        Error messages are printed when an attemped connect fails. the
        thread keeps trying until a successful connection is made, or until
        the connection is closed.
        
        Returns a Connection instance that represents the
        connection to the other context. These connection objects 
        can also be obtained via the Context.connections property.
        
        Parameters
        ----------
        address : str
            Should be of the shape hostname:port. The port should be an
            integer number between 1024 and 2**16. If port does not 
            represent a number, a valid port number is created using a 
            hash function.
        max_tries : int
            The number of ports to try; starting from the given port, 
            subsequent ports are tried until a free port is available. 
            The final port can be obtained using the 'port' property of
            the returned Connection instance.
        name : string
            The name for the created Connection instance. It can
            be used as a key in the connections property.
        
        Notes on hostname
        -----------------
        The hostname can be:
          * The IP address, or the string hostname of this computer. 
          * 'localhost': the connections is only visible from this computer. 
            Also some low level networking layers are bypassed, which results
            in a faster connection. The other context should also connect to
            'localhost'.
          * 'publichost': the connection is visible by other computers on the 
            same network. Optionally an integer index can be appended if
            the machine has multiple IP addresses (see socket.gethostbyname_ex).
        
        """

        # Trigger cleanup of closed connections
        self.connections

        # Split address in protocol, real hostname and port number
        protocol, hostname, port = split_address(address)

        # Based on protocol, instantiate connection class (currently only tcp)
        if False:  #protocol == 'itc':
            connection = ItcConnection(self, name)
        else:
            connection = TcpConnection(self, name)

        # Bind connection
        connection._bind(hostname, port, max_tries)

        # Save connection instance
        self._connections_lock.acquire()
        try:
            # Push packages from startup queue
            while len(self._startupQueue):
                connection._inject_package(self._startupQueue.pop())
            # Add connection object to list of connections
            self._connections.append(connection)
        finally:
            self._connections_lock.release()

        # Return Connection instance
        return connection
Exemplo n.º 5
0
def do_request(address, request, timeout=-1):
    """ do_request(address, request, timeout=-1)
    
    Do a request at the server at the specified address. The server can
    be a yoton.RequestServer, or any other server listening on a socket
    and following a REQ/REP pattern, such as html or telnet. For example:
    ``html = do_request('www.google.com:80', 'GET http/1.1\\r\\n')``
    
    Parameters
    ----------
    address : str
        Should be of the shape hostname:port.
    request : string
        The request to make.
    timeout : float
        If larger than 0, will wait that many seconds for the respons, and
        return None if timed out.
    
    Notes on hostname
    -----------------
    The hostname can be:
      * The IP address, or the string hostname of this computer.
      * 'localhost': the connections is only visible from this computer.
        Also some low level networking layers are bypassed, which results
        in a faster connection. The other context should also connect to
        'localhost'.
      * 'publichost': the connection is visible by other computers on the
        same network.
    
    """
    
    # Determine host (assume tcp)
    protocol, host, port = split_address(address)
    
    # Check request
    if not isinstance(request, basestring):
        raise ValueError('request should be a string.')
    
    # Check timeout
    if timeout is None:
        timeout = -1
    
    # Create socket and connect
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((host,port))
    except socket.error:
        raise RuntimeError('No server is listening at the given port.')
    
    # Send request
    send_all(s, request, True)
    
    # Receive reply
    reply = recv_all(s, timeout)
    
    # Close socket
    try:
        s.close()
    except socket.error:
        pass
    
    # Done
    return reply
Exemplo n.º 6
0
   * 'publichost': the connection is visible by other computers on the
     same network.
 
 """
 
 def __init__(self, address, async=False, verbose=0):
     threading.Thread.__init__(self)
     
     # Store whether to handle requests asynchronously
     self._async = async
     
     # Verbosity
     self._verbose = verbose
     
     # Determine host and port (assume tcp)
     protocol, host, port = split_address(address)
     
     # Create socket. Apply SO_REUSEADDR when binding, so that a
     # improperly closed socket on the same port will not prevent
     # us connecting.
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     
     # Bind (can raise error is port is not available)
     s.bind((host,port))
     
     # Store socket instance
     self._bsd_socket = s
     
     # To stop serving
     self._stop_me = False
Exemplo n.º 7
0
 def connect(self, address, timeout=1.0, name=''):
     """ connect(self, address, timeout=1.0, name='')
     
     Setup a connection with another context, by connection to a 
     hosting context. An error is raised when the connection could
     not be made.
     
     Returns a Connection instance that represents the
     connection to the other context. These connection objects 
     can also be obtained via the Context.connections property.
     
     Parameters
     ----------
     address : str
         Should be of the shape hostname:port. The port should be an
         integer number between 1024 and 2**16. If port does not 
         represent a number, a valid port number is created using a 
         hash function.
     max_tries : int
         The number of ports to try; starting from the given port, 
         subsequent ports are tried until a free port is available. 
         The final port can be obtained using the 'port' property of
         the returned Connection instance.
     name : string
         The name for the created Connection instance. It can
         be used as a key in the connections property.
     
     Notes on hostname
     -----------------
     The hostname can be:
       * The IP address, or the string hostname of this computer. 
       * 'localhost': the connection is only visible from this computer. 
         Also some low level networking layers are bypassed, which results
         in a faster connection. The other context should also host as
         'localhost'.
       * 'publichost': the connection is visible by other computers on the 
         same network. Optionally an integer index can be appended if
         the machine has multiple IP addresses (see socket.gethostbyname_ex).
     
     """
     
     # Trigger cleanup of closed connections
     self.connections
     
     # Split address in protocol, real hostname and port number
     protocol, hostname, port = split_address(address)
     
     # Based on protocol, instantiate connection class (currently only tcp)
     if False:#protocol == 'itc':
         connection = ItcConnection(self, name)
     else:
         connection = TcpConnection(self, name)
     
     # Create new connection and connect it
     connection._connect(hostname, port, timeout)
     
     # Save connection instance
     self._connections_lock.acquire()
     try:
         # Push packages from startup queue
         while self._startupQueue:
             connection._inject_package(self._startupQueue.pop())
         # Add connection object to list of connections
         self._connections.append(connection)
     finally:
         self._connections_lock.release()
     
     # Send message in the network to signal a new connection
     bb = 'NEW_CONNECTION'.encode('utf-8')
     p = Package(bb, SLOT_CONTEXT, self._id, 0,0,0,0)
     self._send_package(p)
     
     # Return Connection instance
     return connection
Exemplo n.º 8
0
 def bind(self, address, max_tries=1, name=''):
     """ bind(address, max_tries=1, name='')
     
     Setup a connection with another Context, by being the host.
     This method starts a thread that waits for incoming connections.
     Error messages are printed when an attemped connect fails. the
     thread keeps trying until a successful connection is made, or until
     the connection is closed.
     
     Returns a Connection instance that represents the
     connection to the other context. These connection objects 
     can also be obtained via the Context.connections property.
     
     Parameters
     ----------
     address : str
         Should be of the shape hostname:port. The port should be an
         integer number between 1024 and 2**16. If port does not 
         represent a number, a valid port number is created using a 
         hash function.
     max_tries : int
         The number of ports to try; starting from the given port, 
         subsequent ports are tried until a free port is available. 
         The final port can be obtained using the 'port' property of
         the returned Connection instance.
     name : string
         The name for the created Connection instance. It can
         be used as a key in the connections property.
     
     Notes on hostname
     -----------------
     The hostname can be:
       * The IP address, or the string hostname of this computer. 
       * 'localhost': the connections is only visible from this computer. 
         Also some low level networking layers are bypassed, which results
         in a faster connection. The other context should also connect to
         'localhost'.
       * 'publichost': the connection is visible by other computers on the 
         same network. Optionally an integer index can be appended if
         the machine has multiple IP addresses (see socket.gethostbyname_ex).
     
     """ 
     
     # Trigger cleanup of closed connections
     self.connections
     
     # Split address in protocol, real hostname and port number
     protocol, hostname, port = split_address(address)
     
     # Based on protocol, instantiate connection class (currently only tcp)
     if False:#protocol == 'itc':
         connection = ItcConnection(self, name)
     else:
         connection = TcpConnection(self, name)
     
     # Bind connection
     connection._bind(hostname, port, max_tries)
     
     # Save connection instance
     self._connections_lock.acquire()
     try:
         # Push packages from startup queue
         while len(self._startupQueue):
             connection._inject_package(self._startupQueue.pop())
         # Add connection object to list of connections
         self._connections.append(connection)
     finally:
         self._connections_lock.release()
     
     # Return Connection instance
     return connection
Exemplo n.º 9
0
def do_request(address, request, timeout=-1):
    """ do_request(address, request, timeout=-1)
    
    Do a request at the server at the specified address. The server can
    be a yoton.RequestServer, or any other server listening on a socket
    and following a REQ/REP pattern, such as html or telnet. For example:
    ``html = do_request('www.google.com:80', 'GET http/1.1\\r\\n')``
    
    Parameters
    ----------
    address : str
        Should be of the shape hostname:port.
    request : string
        The request to make.
    timeout : float
        If larger than 0, will wait that many seconds for the respons, and
        return None if timed out.
    
    Notes on hostname
    -----------------
    The hostname can be:
      * The IP address, or the string hostname of this computer.
      * 'localhost': the connections is only visible from this computer.
        Also some low level networking layers are bypassed, which results
        in a faster connection. The other context should also connect to
        'localhost'.
      * 'publichost': the connection is visible by other computers on the
        same network.
    
    """

    # Determine host (assume tcp)
    protocol, host, port = split_address(address)

    # Check request
    if not isinstance(request, basestring):
        raise ValueError("request should be a string.")

    # Check timeout
    if timeout is None:
        timeout = -1

    # Create socket and connect
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((host, port))
    except socket.error:
        raise RuntimeError("No server is listening at the given port.")

    # Send request
    send_all(s, request, True)

    # Receive reply
    reply = recv_all(s, timeout)

    # Close socket
    try:
        s.close()
    except socket.error:
        pass

    # Done
    return reply
Exemplo n.º 10
0
   * 'publichost': the connection is visible by other computers on the
     same network.
 
 """
 
 def __init__(self, address, async=False, verbose=0):
     threading.Thread.__init__(self)
     
     # Store whether to handle requests asynchronously
     self._async = async
     
     # Verbosity
     self._verbose = verbose
     
     # Determine host and port (assume tcp)
     protocol, host, port = split_address(address)
     
     # Create socket. Apply SO_REUSEADDR when binding, so that a
     # improperly closed socket on the same port will not prevent
     # us connecting.
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     
     # Bind (can raise error is port is not available)
     s.bind((host,port))
     
     # Store socket instance
     self._bsd_socket = s
     
     # To stop serving
     self._stop_me = False