示例#1
0
    def __init__(self, application, scriptName='', environ=None,
                 bindAddress=('localhost', 4000), allowedServers=None,
                 loggingLevel=logging.INFO, **kw):
        """
        scriptName is the initial portion of the URL path that "belongs"
        to your application. It is used to determine PATH_INFO (which doesn't
        seem to be passed in). An empty scriptName means your application
        is mounted at the root of your virtual host.

        environ, which must be a dictionary, can contain any additional
        environment variables you want to pass to your application.

        bindAddress is the address to bind to, which must be a tuple of
        length 2. The first element is a string, which is the host name
        or IPv4 address of a local interface. The 2nd element is the port
        number.

        allowedServers must be None or a list of strings representing the
        IPv4 addresses of servers allowed to connect. None means accept
        connections from anywhere.

        loggingLevel sets the logging level of the module-level logger.
        """
        BaseSCGIServer.__init__(self, application,
                                scriptName=scriptName,
                                environ=environ,
                                multithreaded=False,
                                multiprocess=True,
                                bindAddress=bindAddress,
                                allowedServers=allowedServers,
                                loggingLevel=loggingLevel)
        for key in ('multithreaded', 'multiprocess', 'jobClass', 'jobArgs'):
            if kw.has_key(key):
                del kw[key]
        PreforkServer.__init__(self, jobClass=Connection, jobArgs=(self,), **kw)
示例#2
0
    def __init__(self, application, environ=None,
                 bindAddress=None, multiplexed=False, **kw):
        """
        environ, if present, must be a dictionary-like object. Its
        contents will be copied into application's environ. Useful
        for passing application-specific variables.

        bindAddress, if present, must either be a string or a 2-tuple. If
        present, run() will open its own listening socket. You would use
        this if you wanted to run your application as an 'external' FastCGI
        app. (i.e. the webserver would no longer be responsible for starting
        your app) If a string, it will be interpreted as a filename and a UNIX
        socket will be opened. If a tuple, the first element, a string,
        is the interface name/IP to bind to, and the second element (an int)
        is the port number.
        """
        BaseFCGIServer.__init__(self, application,
                                environ=environ,
                                multithreaded=False,
                                multiprocess=True,
                                bindAddress=bindAddress,
                                multiplexed=multiplexed)
        for key in ('multithreaded', 'multiprocess', 'jobClass', 'jobArgs'):
            if kw.has_key(key):
                del kw[key]
        PreforkServer.__init__(self, jobClass=self._connectionClass,
                               jobArgs=(self,), **kw)

        try:
            import resource
            # Attempt to glean the maximum number of connections
            # from the OS.
            maxProcs = resource.getrlimit(resource.RLIMIT_NPROC)[0]
            maxConns = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
            maxConns = min(maxConns, maxProcs)
        except ImportError:
            maxConns = 100 # Just some made up number.
        maxReqs = maxConns
        self.capability = {
            FCGI_MAX_CONNS: maxConns,
            FCGI_MAX_REQS: maxReqs,
            FCGI_MPXS_CONNS: 0
            }
示例#3
0
    def run(self):
        """
        The main loop. Exits on SIGHUP, SIGINT, SIGTERM. Returns True if
        SIGHUP was received, False otherwise.
        """
        self._web_server_addrs = os.environ.get('FCGI_WEB_SERVER_ADDRS')
        if self._web_server_addrs is not None:
            self._web_server_addrs = map(lambda x: x.strip(),
                                         self._web_server_addrs.split(','))

        sock = self._setupSocket()

        ret = PreforkServer.run(self, sock)

        self._cleanupSocket(sock)

        return ret
示例#4
0
    parser.add_argument('-r',
                        type=str,
                        help='Root directory with static files')
    parser.add_argument('-c', type=int, help='CPU count')
    parser.add_argument('-H', type=int, help='Server\'s host')
    parser.add_argument('-p', type=str, help='Server\'s port')
    parser.add_argument('-l',
                        type=str,
                        help='Number of socket connections before refusing')
    parser.add_argument(
        '-b',
        type=str,
        help='Size of buffer being used for reading data from socket')

    args = vars(parser.parse_args())
    configurator = Configurator(config.config_path, args)

    server = PreforkServer(cpu_count=configurator.cpu_count,
                           host=configurator.host,
                           port=configurator.port,
                           listeners=configurator.listeners,
                           buffer_size=configurator.buffer_size,
                           handler=ResponseHandler(configurator.root_dir))
    try:
        server.start()
    except Exception as err:
        print('|ERROR|', err)
        traceback.print_exc()
    finally:
        server.stop()