【问题标题】:Cannot access flask web application deployed on the Google cloud platform app engine无法访问部署在谷歌云平台应用引擎上的flask web应用
【发布时间】:2018-12-19 10:42:18
【问题描述】:

我成功地部署了应用程序而没有任何错误,但是当我访问应用程序 URL 时,我得到一个错误:

错误:未找到 在此服务器上找不到请求的 URL /。

当我访问云控制台上的错误日志时,我看到了错误:

ValueError:virtualenv:无法访问flask:没有这样的virtualenv或站点>目录

我的代码如下:

应用程序.py

from flask import Flask, render_template, request,url_for,redirect,flash
import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

app = Flask(__name__)
app.secret_key = 'some_secret'

engine = create_engine("postgresql://postgres:sona@localhost:1234/users")
db = scoped_session(sessionmaker(bind=engine))

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

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

@app.route("/hello", methods=["GET","POST"])
def hello():
    if request.method=="GET":
        return redirect(url_for('index'))

    else:
        fname = request.form.get("fname")
        fname = fname.capitalize()
        lname = request.form.get("lname")
        lname = lname.capitalize()
        username = request.form.get("username")
        password = request.form.get("password")
        email = request.form.get("email")
        mobile = request.form.get("mobile")
        age = request.form.get("age")
        location = request.form.get("location")

        if fname == '' and age == '':
            flash('Invalid Credentials !!')
            return render_template("register.html",message='True')
        else:
            db.execute("INSERT INTO users(fname, lname, username, password, 
                email, mobile, age, location) VALUES (:fname, :lname, 
                :username, :password, 
                :email, :mobile, :age, :location)",
                    {"fname": fname, "lname": lname,"username": username, 
                    "password": password, "email": email, "mobile": mobile, 
                    "age": age, "location": location})
        db.commit()

        users = db.execute("SELECT * FROM users").fetchall()

        return render_template("hello.html",fname=fname,lname=lname,
            username=username,password=password,
            email=email,mobile=mobile,age=age,
            location=location,users=users)

if __name__ == '__main__':
    app.debug = True
    app.run()

appengine_config.py

from google.appengine.ext import vendor

    # Add any libraries installed in the "lib" folder.
vendor.add('flask')

app.yaml

  runtime: python27
    api_version: 1
    threadsafe: true

    handlers:
    - url: /static
      static_dir: static
    - url: /.*
      script: main.app

requirements.txt

 Flask==0.12.2
    Werkzeug<0.13.0,>=0.12.0

剩下的所有 HTML 文件都很好,我可以在 localhost 服务器上成功运行应用程序。 还需要指导将应用程序连接到云上的 postgresql 实例。 此外,该应用程序在云 sdk shell 上的 ocalhost 上进行测试时也不会出现错误。

【问题讨论】:

  • 我对 GCP 不熟悉,但你确定要使用你的 requirements.txt 远程安装你的 virtualenv 吗?此外,您在应用程序中使用 sqlalchemy,但它不在您的 requirements.txt 文件中,因此不会安装依赖项

标签: python google-app-engine flask web-applications google-cloud-platform


【解决方案1】:

application.py 重命名为main.py(或者,非标准,将app.yaml 文件中的main.app 更改为application.app,但不能同时使用两者)。包含app 变量的模块名称必须匹配。从Handlers element table from the app.yaml reference中的脚本行:

script: 指令必须是 python 导入路径,例如, package.module.app 指向一个 WSGI 应用程序。最后 使用 Python 模块 路径的 script: 指令的组件是 模块中全局变量的名称:该变量必须是 WSGI 应用程序,通常按约定称为app

您的 vendor.add('flask') 建议您为 3rd 方库使用非标准目录名称 - flask 而不是 lib - 请务必相应地调整文档信息。但我建议恢复到标准命名约定,除非你故意找麻烦。

删除 if __name__ == '__main__': 部分 - 这不是 Flask 在 GAE 上的工作方式。

我强烈建议仔细阅读Getting Started with Flask on App Engine Standard Environment(包括代码示例)。

【讨论】:

    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 2021-02-04
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2017-05-14
    • 1970-01-01
    相关资源
    最近更新 更多