Exemple #1
0
def main():
    main_pid = os.getpid()
    printf("Main pid: %s" % main_pid)
    printf("ScrapydWeb version: %s" % __version__)
    printf("Use the 'scrapydweb -h' command to get help")
    printf("Loading default settings from %s" %
           os.path.join(CWD, 'default_settings.py'))

    app = create_app()
    load_custom_config(app.config)

    args = parse_args(app.config)
    # "scrapydweb -h" ends up here
    update_app_config(app.config, args)
    # from pprint import pprint
    # pprint(app.config)
    try:
        check_app_config(app.config)
    except AssertionError as err:
        sys.exit("\n!!! %s\nCheck and update your settings in: %s" %
                 (err, scrapydweb_settings_py_path))

    if app.config.get('ENABLE_CACHE', True):
        caching_pid = init_caching(app.config, main_pid)
    else:
        caching_pid = None

    # https://stackoverflow.com/questions/34164464/flask-decorate-every-route-at-once
    @app.before_request
    def require_login():
        if app.config.get('ENABLE_AUTH', False):
            auth = request.authorization
            USERNAME = str(app.config.get('USERNAME',
                                          ''))  # May be 0 from config file
            PASSWORD = str(app.config.get('PASSWORD', ''))
            if not auth or not (auth.username == USERNAME
                                and auth.password == PASSWORD):
                return authenticate()

    # Should be commented out for released version
    # https://stackoverflow.com/questions/34066804/disabling-caching-in-flask
    # @app.after_request
    # def add_header(r):
    # r.headers['Pragma'] = 'no-cache'
    # r.headers['Expires'] = '0'
    # r.headers['Cache-Control'] = 'public, max-age=0'
    # return r

    @app.context_processor
    def inject_variable():
        return dict(
            main_pid=main_pid,
            caching_pid=caching_pid,
            CHECK_LATEST_VERSION_FREQ=100,
            scrapydweb_settings_py_path=scrapydweb_settings_py_path,
        )

    printf(
        "Visit ScrapydWeb at http://127.0.0.1:{port} or http://{bind}:{port}".
        format(bind='IP-OF-CURRENT-HOST', port=app.config['SCRAPYDWEB_PORT']))

    # site-packages/flask/app.py
    # def run(self, host=None, port=None, debug=None, load_dotenv=True, **options):
    # Threaded mode is enabled by default.
    app.run(host=app.config['SCRAPYDWEB_BIND'],
            port=app.config['SCRAPYDWEB_PORT'])  # , debug=True)
Exemple #2
0
def test_init_caching(app):
    init_caching(app.config, os.getpid())
Exemple #3
0
def main():
    main_pid = os.getpid()
    printf("Main pid: %s" % main_pid)
    printf("scrapydweb version: %s" % __version__)
    printf("Run 'scrapydweb -h' to get help")
    printf("Loading default settings from %s" % os.path.join(CWD, 'default_settings.py'))

    app = create_app()
    load_custom_config(app.config)

    args = parse_args(app.config)
    # "scrapydweb -h" would end up here
    update_app_config(app.config, args)
    # from pprint import pprint
    # pprint(app.config)
    try:
        check_app_config(app.config)
    except AssertionError as err:
        sys.exit("\n!!! %s\nCheck out your settings in: %s" % (err, scrapydweb_settings_py_path))

    if not app.config.get('DISABLE_CACHE', False):
        caching_pid = init_caching(app.config, main_pid)
    else:
        caching_pid = None

    REQUIRE_LOGIN = False if app.config.get('DISABLE_AUTH', True) else True
    USERNAME = str(app.config.get('USERNAME', ''))  # May be 0 from config file
    PASSWORD = str(app.config.get('PASSWORD', ''))

    # https://stackoverflow.com/questions/34164464/flask-decorate-every-route-at-once
    @app.before_request
    def require_login():
        if REQUIRE_LOGIN:
            auth = request.authorization
            if not auth or not (auth.username == USERNAME and auth.password == PASSWORD):
                return authenticate()

    @app.context_processor
    def inject_variable():
        return dict(
            SCRAPYD_SERVERS=app.config['SCRAPYD_SERVERS'],
            SCRAPYD_SERVERS_AMOUNT=len(app.config['SCRAPYD_SERVERS']),
            SCRAPYD_SERVERS_GROUPS=app.config['SCRAPYD_SERVERS_GROUPS'],
            SCRAPYD_SERVERS_AUTHS=app.config['SCRAPYD_SERVERS_AUTHS'],
            PYTHON_VERSION='.'.join([str(n) for n in sys.version_info[:3]]),
            SCRAPYDWEB_VERSION=__version__,
            CHECK_LATEST_VERSION_FREQ=30,
            DEFAULT_LATEST_VERSION=DEFAULT_LATEST_VERSION,
            GITHUB_URL=__url__,
            SHOW_SCRAPYD_ITEMS=app.config.get('SHOW_SCRAPYD_ITEMS', True),
            DAEMONSTATUS_REFRESH_INTERVAL=int(app.config.get('DAEMONSTATUS_REFRESH_INTERVAL', 10)),
            REQUIRE_LOGIN=REQUIRE_LOGIN,
            scrapydweb_settings_py_path=scrapydweb_settings_py_path,
            main_pid=main_pid,
            caching_pid=caching_pid,
        )

    printf("Visit ScrapydWeb at http://{bind}:{port} or http://127.0.0.1:{port}".format(
        bind='IP-OF-CURRENT-HOST', port=app.config['SCRAPYDWEB_PORT']))

    # /site-packages/flask/app.py
    # def run(self, host=None, port=None, debug=None, load_dotenv=True, **options):
    # Threaded mode is enabled by default.
    app.run(host=app.config['SCRAPYDWEB_BIND'], port=app.config['SCRAPYDWEB_PORT'])  # , debug=True)