Example #1
0
 def _really_handle_connection(self, s):
     """ _really_handle_connection(s)
     Really handle an incoming connection.
     """
     # Get request
     request = recv_all(s, True)
     if request is None:
         return
     
     # Get reply
     reply = self.handle_request(request)
     
     # Test
     if not isinstance(reply, basestring):
         raise ValueError('handle_request() should return a string.')
     
     # Send reply
     send_all(s, reply, True)
     
     # Close the socket
     try:
         s.close()
     except socket.error:
         pass
Example #2
0
    def _really_handle_connection(self, s):
        """ _really_handle_connection(s)
        Really handle an incoming connection.
        """
        # Get request
        request = recv_all(s, True)
        if request is None:
            return

        # Get reply
        reply = self.handle_request(request)

        # Test
        if not isinstance(reply, basestring):
            raise ValueError("handle_request() should return a string.")

        # Send reply
        send_all(s, reply, True)

        # Close the socket
        try:
            s.close()
        except socket.error:
            pass
Example #3
0
 def _send_during_handshaking(self, text, shutdown=False):
     return send_all(self._bsd_socket, text + "\r\n", shutdown)
Example #4
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
Example #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
Example #6
0
 def _send_during_handshaking(self, text, shutdown=False):
     return send_all(self._bsd_socket, text+'\r\n', shutdown)