【问题标题】:404 error running python script from html button | Google App Engine website hosted从 html 按钮运行 python 脚本时出现 404 错误托管 Google App Engine 网站
【发布时间】:2020-07-26 11:23:15
【问题描述】:

我有一个 html 页面 custom.html 和一个 python 脚本 test.py(都在下面截图)。 html 页面只有一个按钮,应该触发 python 脚本打印一行(在我的下一个 python 脚本中,我希望它做更多,但这是我的起点)。

在 Chrome 的开发人员工具下,一旦我单击按钮,我就会收到由 Jquery 发起的 GET 404 错误。非常感谢任何有关从我的 html 按钮成功激活 python 脚本的建议。

我的 test.py 脚本很简单

print("Successful line print")

这是我的 custom.html 文档

<!DOCTYPE html>
  <html lang="en" dir="ltr">

  <head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" type="text/css" href="../static/css/style2.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


  </head>
  <pre>

  </pre>

  <body>
    <div>
      <div>
        <button style="text-align: center; margin-bottom: 150px" class="search-btn" type="button" value=" Run Script " onclick="goPython()">Trigger Python
          <script>
            function goPython() {
              $.ajax({
                url: "../folder/test.py",
                context: document.body
              }).done(function() {
                alert('finished python script');;
              });
            }
          </script>
        </button>
      </div>
    </div>
  </body>

  </html>

编辑:我将代码添加到 Google App Engine 处理 URL 调用和导入 Flask 所需的 main.py 脚本中。

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html')

@app.route("/learn.html")
def learn():
    return render_template('learn.html')

@app.route("/custom.html")
def custom():
    return render_template('custom.html')

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

编辑 2,在尝试@Dustin Ingram 的回答后:

这是我的 custom.html 的新代码

<!DOCTYPE html>
  <html lang="en" dir="ltr">

  <head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" type="text/css" href="../static/css/style2.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


  </head>

  <body>
    <div>
      <div>
        <button style="text-align: center; margin-bottom: 150px" class="search-btn" type="button" value=" Run Script " onclick="goPython()">Click Here
          <script>
            function goPython() {
              $.ajax({
                url: "/trigger-python",
                context: document.body
              }).done(function() {
                alert('finished python script');;
              });
            }
          </script>
        </button>
      </div>
    </div>
  </body>

  </html>

我做了一个简单的test.py,只是为了测试html按钮点击激活python脚本的能力

from flask import Flask

app = Flask(__name__)

@app.route("/trigger-python")
def print_something():
    print("Successful line print")
    return 'OK'

print_something()

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)

修复在 AJAX 中调用的 URL 后,单击按钮时仍然出现相同的 404 错误。以下是更新后的 Chrome 开发者工具屏幕截图。

【问题讨论】:

    标签: python html ajax google-app-engine google-cloud-platform


    【解决方案1】:

    您不能通过这样的 AJAX 请求调用 Python 脚本。您需要调用对应于 Python Web 应用程序端点的 URL。

    所以,例如在前端:

    $.ajax({
      url: "/do-something",
      context: document.body
    })
    

    然后在后端有对应的路由:

    @app.route("/do-something")
    def do_something():
        print("Successful line print")
        return 'OK'
    

    有关在 App Engine 上开始使用 Python 网络应用程序的详细信息,请参阅 https://cloud.google.com/appengine/docs/standard/python3/quickstart。

    编辑:这正是我测试并确认的作品:

    app.yaml:

    runtime: python37
    

    requirements.txt:

    Flask==1.1.2
    

    main.py:

    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    
    @app.route('/custom.html')
    def custom():
        return render_template('custom.html')
    
    @app.route("/trigger-python")
    def print_something():
        print("Successful line print")
        return 'OK'
    
    if __name__ == '__main__':
        app.run(host='127.0.0.1', port=8080, debug=True)
    

    templates/custom.html

    <!DOCTYPE html>
      <html lang="en" dir="ltr">
    
      <head>
        <meta charset="utf-8">
        <title>Page Title</title>
        <link rel="stylesheet" type="text/css" href="../static/css/style2.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    
    
      </head>
    
      <body>
        <div>
          <div>
            <button style="text-align: center; margin-bottom: 150px" class="search-btn" type="button" value=" Run Script " onclick="goPython()">Click Here
              <script>
                function goPython() {
                  $.ajax({
                    url: "/trigger-python",
                    context: document.body
                  }).done(function() {
                    alert('finished python script');;
                  });
                }
              </script>
            </button>
          </div>
        </div>
      </body>
    
      </html>
    

    你可以在这里看到它的工作原理:https://stackoverflow-61195723.uc.r.appspot.com/custom.html

    【讨论】:

    • 感谢您的建议,我已将代码添加到 main.py,该脚本在我的 app.yaml 脚本、静态和模板子目录旁边用于调用网站各个页面的 URL .您是否建议单击按钮时需要一个单独的 URL,并且 python 脚本链接到该单独的 URL,就像“索引”、“学习”和“自定义”有自己的 URL 一样?
    • 这是我发现 AJAX 用于此目的的地方,但假设我通过 Google App Engine 运行它会更安全:stackoverflow.com/questions/48552343/…。所以这对我的情况不正确?
    • @Henrik 我已更新我的答案以添加更多详细信息。
    • 完美,感谢您的更新。早上我会先尝试一下,然后及时通知您。
    • 我整个早上都在执行你的答案,但仍然遇到同样的问题(我已经用新的代码和底部的错误更新了我的原始帖子)。我确定这是一个实施错误,我遗漏了一些小东西,但查找问题只是将我带到我已经访问过的相同站点。你有什么建议吗?
    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 2021-11-02
    • 2010-10-19
    • 1970-01-01
    • 2019-06-12
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多