Ejemplo n.º 1
0
    def __init__(self, server_name='', host=None, port=None, adapter=None):
        """ WebServer class constructor.

        @param server_name: server name
        @param host: host to listen on
        @param port: port to listen on
        @param adapter: optional, can receive an adapter retrieved with
                        get_available_adapters()

        @type server_name: string
        @type host: string
        @type port: int
        @type adapter: AdapterInterface

        @note: the only parameter that should be always passed is the server
               name. In most case scenarios, the others can be guessed/assigned
               automatically by the webserver.
        """
        Resource.__init__(self, '/')
        self.server_name = server_name
        self.host = None
        self.port = None
        self.adapter = None
        self.running = False

        if adapter and not issubclass(adapter, AdapterInterface):
            # Invalid adapter
            raise TypeError('Adapter must implement AdapterInterface')
        elif adapter:
            # Adapter OK
            self.adapter = adapter()
        else:
            pref = get_preferred_adapter()

            if not pref:
                # First available adapter
                available = get_available_adapters()

                if len(available) > 0:
                    self.adapter = available[0]()
            else:
                # Preferred adapter
                self.adapter = pref()

        if not self.adapter:
            log.critical('Could not select a adapter. Check the supported '\
                         'adapters and install one of them on your system.')
            raise SystemExit

        if not self.host and not self.port:
            self._generate_random_address()
        else:
            self.set_bind_address(host, port)

        self.adapter.setup(self.server_name, self.host, self.port,
                           self.application)
Ejemplo n.º 2
0
    def __init__(self, server_name='', host=None, port=None, adapter=None):
        """ WebServer class constructor.

        @param server_name: server name
        @param host: host to listen on
        @param port: port to listen on
        @param adapter: optional, can receive an adapter retrieved with
                        get_available_adapters()

        @type server_name: string
        @type host: string
        @type port: int
        @type adapter: AdapterInterface

        @note: the only parameter that should be always passed is the server
               name. In most case scenarios, the others can be guessed/assigned
               automatically by the webserver.
        """
        Resource.__init__(self, '/')
        self.server_name = server_name
        self.host = None
        self.port = None
        self.adapter = None
        self.running = False

        if adapter and not issubclass(adapter, AdapterInterface):
            # Invalid adapter
            raise TypeError('Adapter must implement AdapterInterface')
        elif adapter:
            # Adapter OK
            self.adapter = adapter()
        else:
            pref = get_preferred_adapter()

            if not pref:
                # First available adapter
                available = get_available_adapters()

                if len(available) > 0:
                    self.adapter = available[0]()
            else:
                # Preferred adapter
                self.adapter = pref()

        if not self.adapter:
            log.critical('Could not select a adapter. Check the supported '\
                         'adapters and install one of them on your system.')
            raise SystemExit

        if not self.host and not self.port:
            self._generate_random_address()
        else:
            self.set_bind_address(host, port)

        self.adapter.setup(self.server_name, self.host, self.port,
                           self.application)
Ejemplo n.º 3
0
    def _create_socket(self, shared):
        """ Creates the socket if a shared socket has not been passed to the
        constructor.

        @param shared: socket to be reused
        @type shared: socket.socket
        """
        if shared:
            self.socket = shared
        else:
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                                         socket.IPPROTO_UDP)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        try:
            self.socket.bind((self.interface, self.port))
            self.mreq = pack('4sl', socket.inet_aton(self.addr),
                             socket.INADDR_ANY)
            self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
                                    self.mreq)
        except Exception, e:
            log.critical('Cannot create socket: %s' % str(e))
            raise CannotListenError(self.interface, self.port, self.addr,
                                    "Couldn't bind address")
Ejemplo n.º 4
0
    def _create_socket(self, shared):
        """ Creates the socket if a shared socket has not been passed to the
        constructor.

        @param shared: socket to be reused
        @type shared: socket.socket
        """
        if shared:
            self.socket = shared
        else:
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                                        socket.IPPROTO_UDP)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        try:
            self.socket.bind((self.interface, self.port))
            self.mreq = pack('4sl', socket.inet_aton(self.addr),
                             socket.INADDR_ANY)
            self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
                                   self.mreq)
        except Exception, e:
            log.critical('Cannot create socket: %s' % str(e))
            raise CannotListenError(self.interface, self.port, self.addr,
                                    "Couldn't bind address")