简体   繁体   English

Flask 应用程序中的 ERR_TOO_MANY_REDIRECTS。 在本地工作,但不在服务器中

[英]ERR_TOO_MANY_REDIRECTS in a Flask application. Works in local but not in server

In local my Flask application works fine, and when I use /editing_buddy it redirects me correctly.本地,我的 Flask 应用程序工作正常,当我使用/editing_buddy时,它会正确重定向我。 The thing is, when I upload it to the server , it always gives me 404 Error .问题是,当我将它上传到服务器时,它总是给我404 Error

If I try to use @app.route decorator, I get a TOO MANY REDIRECTS error .如果我尝试使用@app.route装饰器,我会收到TOO MANY REDIRECTS error My server is hosted by Wikimedia in funpedia.toolforge.org if that helps.如果有帮助,我的服务器由 funpedia.toolforge.org 中的 Wikimedia 托管。 It's a webservice with kubernetes backend.这是一个带有 kubernetes 后端的网络服务。

My code is like this:我的代码是这样的:

app = flask.Flask(__name__)

if __name__ == '__main__':
    app.run_server(host='0.0.0.0', threaded=True, debug=True)


# Load configuration from YAML file
__dir__ = os.path.dirname(__file__)
app.config.update(
    yaml.safe_load(open(os.path.join(__dir__, 'config.yaml'))))

@app.route('/')
def index():

    return flask.redirect(flask.url_for('index'))

@app.route('/editing_buddy')
def buddy():

    return flask.redirect(flask.url_for('buddy'))

@app.errorhandler(404)
def handling_page_not_found(e):
    return "<h1>404</h1><p>The resource could not be found.</p>", 404

The apps for the "/" and "/editing_buddy" are Dash applications with the same url_base_pathname: “/”和“/editing_buddy”的应用程序是具有相同 url_base_pathname 的 Dash 应用程序:

Buddy伙伴

buddy_app = Dash(__name__, server=application, url_base_pathname="/editing_buddy/",
                             external_stylesheets=external_stylesheets)
buddy_app.config['suppress_callback_exceptions'] = True

Home

 home_app = Dash(__name__, server=application, url_base_pathname="/", 
                           external_stylesheets=external_stylesheets)
 home_app.config['suppress_callback_exceptions'] = True



from view.home import *
from view.editing_buddy_app import *

Locally, I start the server by running wsgi.py .在本地,我通过运行wsgi.py来启动服务器。 Which contains the code:其中包含代码:

from app import app as application

if __name__ == "__main__":
    application.run()

In the server I simply run webservice restart , but I have a.ini file called app.ini with content:在服务器中,我只是运行webservice restart ,但我有一个名为app.ini的.ini 文件,其中包含以下内容:

[uwsgi]
module = wsgi
callable = application

And my folder structure is我的文件夹结构是文件夹结构

PS : I've also tried hardcoding the redirects like flask.redirect("/editing_buddy/") . PS :我也尝试过硬编码像flask.redirect("/editing_buddy/")这样的重定向。 Even i've tried to hardcode the URL like flask.redirect("funpedia.toolforge.org/editing_buddy/") and either doesn't find it or Too many redirects.即使我尝试对 URL 像flask.redirect("funpedia.toolforge.org/editing_buddy/")进行硬编码,但要么找不到它,要么重定向太多。

You can see my repo HERE你可以在这里看到我的回购

You are literally redirecting in your methods to the exact same method you're doing the redirect from.您实际上是在您的方法中重定向到您正在执行重定向的完全相同的方法。 By doing so you are creating an endless loop, and TBH this should even throw errors locally.通过这样做,您正在创建一个无限循环,并且 TBH 这甚至应该在本地引发错误。

I advise you to give this medium.com article a good read to make sure you set up both Dash and Flask correctly, using the Application Factory Pattern.我建议您阅读此medium.com 文章,以确保您使用应用程序工厂模式正确设置 Dash 和 Flask。

=================================================== ==================================================== =

EDIT:编辑:

This is a copy/paste from the article I linked, you can see that when the Flask app gets instantiated, it also creates the Dash apps, using the Flask app as the server...这是我链接的文章的复制/粘贴,您可以看到当 Flask 应用程序被实例化时,它还会创建 Dash 应用程序,使用 Flask 应用程序作为服务器...

dashboard.py仪表板.py

def create_app():
    server = Flask(__name__)
    server.config.from_object(BaseConfig)

    register_dashapps(server)
# snipped some here

    return server


def register_dashapps(app):
    from app.dashapp1.layout import layout
    from app.dashapp1.callbacks import register_callbacks

    # Meta tags for viewport responsiveness
    meta_viewport = {"name": "viewport", "content": "width=device-width, initial-scale=1, shrink-to-fit=no"}

    dashapp1 = dash.Dash(__name__,
                         server=app,
                         url_base_pathname='/dashboard/',
                         assets_folder=get_root_path(__name__) + '/dashboard/assets/',
                         meta_tags=[meta_viewport])

    with app.app_context():
        dashapp1.title = 'Dashapp 1'
        dashapp1.layout = layout
        register_callbacks(dashapp1)

    _protect_dashviews(dashapp1)

=================================================== ==================================================== =

EDIT2:编辑2:

Thanks for adding the repo, this helped in figuring out what is going on, and I think I have a grasp of what you want to do based on the rest of the code.感谢您添加回购,这有助于弄清楚发生了什么,我想我根据代码的 rest 了解了您想要做什么。 I'm guessing you want to put authentication logic in your app so not everyone can just access your handlers for the respective Dash apps.我猜你想在你的应用程序中加入身份验证逻辑,这样不是每个人都可以访问相应 Dash 应用程序的处理程序。

Thing is, you are running into one of the exact same issues described in the first article I linked, which is the authentication.问题是,您遇到了与我链接的第一篇文章中描述的完全相同的问题,即身份验证。

You will need to refactor your code if you want to use authentication, similar to what is discussed in the aforementioned article.如果要使用身份验证,需要重构代码,类似于上述文章中讨论的内容。

While researching this question I did come across an article which might interest you, as it discusses how to implement authentication.在研究这个问题时,我确实遇到了一篇您可能感兴趣的文章,因为它讨论了如何实现身份验证。 It's also a simpler article, and it basically allows you to apply Flask stuff to Dash apps.这也是一篇更简单的文章,它基本上允许您将 Flask 的东西应用于 Dash 应用程序。 Find it here ... 在这里找到它...

Now it works, why?现在可以了,为什么? I don't have any clue.我没有任何线索。 I did the next:我做了下一个:

  1. I eliminated the app.ini from the directory of the app.py and put it on the root directory.我从app.ini的目录中去掉了app.py ,放到根目录下。
  2. In home.py , I renamed from app import app as application to just from app import app .home.py中,我from app import app as application重命名为 just from app import app
  3. Eliminated the unnecessary @app.route for the dash apps, they are not needed because you already have the url_base_pathname .消除了破折号应用程序不必要的@app.route ,因为您已经有了url_base_pathname ,所以不需要它们。

After doing some testing, I found that the solution is the step 2. By using from app import app in the home.py script.经过一些测试,我发现解决方案是第2步。通过在home.py脚本中使用from app import app

HOWEVER , in the editing_buddy_app.py , I have it with from app import app as application AND IT WORKS但是,在editor_buddy_app.py中,我将它from app import app as application editing_buddy_app.py使用,并且它可以工作

Definitely, I'm missing something here, but I'm unable to guess what.当然,我在这里遗漏了一些东西,但我无法猜测是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 ERR_TOO_MANY_REDIRECTS? - How to fix ERR_TOO_MANY_REDIRECTS? 无法将 jupyterhub 连接到 keycloak 并获得 ERR_TOO_MANY_REDIRECTS - Cant connect jupyterhub to keycloak and getting ERR_TOO_MANY_REDIRECTS ERR_TOO_MANY_REDIRECTS 与 Django 中的中间件重定向 - ERR_TOO_MANY_REDIRECTS in redirect with middleware in Django Django 需要登录错误 ERR_TOO_MANY_REDIRECTS - Django login required error ERR_TOO_MANY_REDIRECTS 在 Django 中使用自定义中间件时出现“ERR_TOO_MANY_REDIRECTS”错误 - Getting 'ERR_TOO_MANY_REDIRECTS' error when using custom Middleware in Django 尝试通过使用 Selenium 和 Python 的框架和 Javascript 的网页登录时出现 ERR_TOO_MANY_REDIRECTS 错误 - ERR_TOO_MANY_REDIRECTS error while trying to login through a webpage that uses frames and Javascript using Selenium and Python python gdata“来自服务器的重定向太多:302” - python gdata “too many redirects from server: 302” flask 应用程序中的 Deepface。 出现错误:'_thread._local' object 没有属性'值' - Deepface in flask application. Getting error: '_thread._local' object has no attribute 'value' 如何使用 heroku 让 flask-python 应用程序重定向到 https 而没有太多重定向错误? - How to get the flask-python app to redirect to https with heroku without too many redirects error? 使用Python请求重定向错误太多 - Too many redirects error using Python requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM