【问题标题】:500 Internal Server Error - GCP App Engine500 内部服务器错误 - GCP App Engine
【发布时间】:2019-10-31 15:40:13
【问题描述】:

我在 GCP App Engine 中玩耍并尝试使用 python 连接到 MySQL 数据库,然后返回/打印特定表的第一行,以便在单击应用程序链接时它会在浏览器中显示类似这样的内容:


sub_event_id = 1234567

event_type = 1

date_time = 2019-09-22 00:00:00

ip = 未知

property_id = 1

contact_id = 1234567

address_id = 1234567


以下是我正在使用的代码以及我在部署应用程序后遇到的错误。 谁能告诉我我做错了什么?谢谢。

这是我的 main.py 文件中的代码:

from flask import Flask
import sqlalchemy as sa
app = Flask(__name__)
@app.route('/')
def dbresponse():
  engine = sa.create_engine('''mysql+pymysql://{username}:
                             {password}@{host}:{port}/{db_name}''')
      with engine.connect() as conn:
          # Execute the query and fetch all results
          response = conn.execute(
              "SELECT * FROM sub_events "
              "LIMIT 1"
          ).fetchall()
          # Print results
          for row in response:
              print("sub_event_id = ", row[0], )
              print("event_type   = ", row[1])
              print("date_time    = ", row[2])
              print("ip           = ", row[3])
              print("property_id  = ", row[4])
              print("contact_id   = ", row[5])
              print("address_id   = ", row[6], "\n")
      engine.dispose()


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

这是我的 app.yaml 文件中的代码:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  python_version: 3

这是我的 requirements.txt 文件中的代码:

Flask==1.1.1
gunicorn==19.9.0
SQLAlchemy==1.3.8
PyMySQL==0.9.3

运行gcloud app deploygcloud app browse 然后点击生成的链接...

我收到 500 内部服务器错误:

500 内部服务器错误 服务器遇到内部错误,无法完成您的请求。要么服务器过载,要么应用程序出错。

【问题讨论】:

  • 这个错误通常意味着你的程序崩溃了。在 Stackdriver 日志文件中查找错误消息。很可能是数据库连接引发了异常,您的代码未捕获异常并且 App Engine 终止了您的程序。首先添加 try/except 块来捕获错误。
  • 或者,您可以运行gcloud app logs tail,如果您的错误日志位于stderr,stdout,crash.log,nginx.request,request_log 中的某个位置(大多数情况下),这很有用。
  • 您是否关注official documentation建立连接?我会做一个测试应用程序来验证连接,然后尝试添加你的应用程序的逻辑。

标签: python mysql google-app-engine flask google-cloud-platform


【解决方案1】:

在这个 for 循环中有一个整数索引会带来 IndexError 的高风险。

在打印之前我会先检查行是否有这个大小。

有两种方法可以做到这一点(使用 try 或检查 len):

      for row in response:
          try:
              print("sub_event_id = ", row[0], )
              print("event_type   = ", row[1])
              print("date_time    = ", row[2])
              print("ip           = ", row[3])
              print("property_id  = ", row[4])
              print("contact_id   = ", row[5])
              print("address_id   = ", row[6], "\n")
          except IndexError:
              print("There is not complete data to print.")

        for row in response:
            if len(row) >= 7: 
                print("sub_event_id = ", row[0], )
                print("event_type   = ", row[1])
                print("date_time    = ", row[2])
                print("ip           = ", row[3])
                print("property_id  = ", row[4])
                print("contact_id   = ", row[5])
                print("address_id   = ", row[6], "\n")
            else:
                print("There is not complete data to print.")

还建议像@jkleinne 指出的那样配置日志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 2021-03-19
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2011-10-17
    相关资源
    最近更新 更多