當我在本地運行這個應用程式時,它作業正常。在云中,我收到以下錯誤。我認為這是我的 yaml 檔案。作為旁注,如果日志中有任何我不應該分享的內容,我將不勝感激。謝謝!
內部服務器錯誤 服務器遇到內部錯誤,無法完成您的請求。服務器過載或應用程式出錯。
我的main.py檔案:
from flask import Flask
import requests
app = Flask(__name__)
@app.route("/")
def data():
response = requests.request("GET", "https://jsonplaceholder.typicode.com/posts")
data = response.json()
return(data)
returned_data = data()
#for i, item in enumerate(returned_data):
# userID = item.get("userID", None)
# id = item.get("id", None)
# title = item.get("id", None)
# print(userID,id,title)
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8080, debug=True)
我的app.yaml檔案:
runtime: python39
entrypoint: gunicorn -b :$PORT -w 2 main:app
我的app.yaml檔案:
runtime: python39
entrypoint: gunicorn -b :$PORT -w 2 main:app
和我的 requirements.txt
requests
Flask==2.1.0
gunicorn
以下是日志:
{
insertId: "63604ebd000c8d09af3f2e13"
labels: {
clone_id: "00c61b114F0e8fb053d8ebee071db45f9aa9ec947906c0029f3b30b22f414ae90868c30c989456619d667de0e08ca37e2598be2ed37ad5d3b"
}
logName: "projects/XXXX/logs//var/log/google_init.log"
receiveTimestamp: "2022-10-31T22:39:58.016186551Z"
resource: {
labels: {
module_id: "default"
project_id: "XXXX"
version_id: "20221031t161907"
zone: "us6"
}
type: "gae_app"
}
textPayload: "[start] 2022/10/31 22:39:57.821966 Start program failed: termination triggered by nginx exit"
timestamp: "2022-10-31T22:39:57.822537Z"
}
uj5u.com熱心網友回復:
您的代碼正在生成一個串列并嘗試在燒瓶中回傳一個串列將導致錯誤 -
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list.
要修復錯誤,請修改您的代碼以使用jsonify
from flask import Flask, jsonify
import requests
app = Flask(__name__)
@app.route("/")
def data():
response = requests.request("GET", "https://jsonplaceholder.typicode.com/posts")
data = response.json()
# return(data)
return jsonify(data) # New; jsonify the list before it's returned
#returned_data = data() This line also causes an error
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524749.html
