使用 python 在 Google Appengine 上成功部署应用程序后 Web url 出错
Error on web url after successful app deployment on Google Appengine with python
我正在尝试设置 google appengine 以读取来自 pubsub 的通知。成功部署应用程序后,它在 https://.appspot.com :
上给我错误
An internal server error occured:
name 'request' is not defined
Main.py 包含以下代码:
import logging
from flask import Flask
import os
import requests
app = Flask(__name__)
@app.route('/pubsub/push', methods=['POST'])
def pubsub_push():
if (request.args.get('token', '') !=
current_app.config['PUBSUB_VERIFICATION_TOKEN']):
return 'Invalid request', 400
envelope = json.loads(request.data.decode('utf-8'))
payload = base64.b64decode(envelope['message']['data'])
MESSAGES.append(payload)
# Returning any 2xx status indicates successful receipt of the message.
return 'OK', 200
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html', messages=MESSAGES)
data = request.form.get('payload', 'Example payload').encode('utf-8')
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(
current_app.config['PROJECT'],
current_app.config['PUBSUB_TOPIC'])
publisher.publish(topic_path, data=data)
return 'OK', 200
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__' :
app.run(host='127.0.0.1',port=8080,debug=True)
非常感谢对此问题的任何帮助。
For web applications it’s crucial to react to the data a client sends
to the server. In Flask this information is provided by the global
request object.
您缺少 request
导入,您需要添加它:
from flask import request
我正在尝试设置 google appengine 以读取来自 pubsub 的通知。成功部署应用程序后,它在 https://.appspot.com :
上给我错误An internal server error occured:
name 'request' is not defined
Main.py 包含以下代码:
import logging
from flask import Flask
import os
import requests
app = Flask(__name__)
@app.route('/pubsub/push', methods=['POST'])
def pubsub_push():
if (request.args.get('token', '') !=
current_app.config['PUBSUB_VERIFICATION_TOKEN']):
return 'Invalid request', 400
envelope = json.loads(request.data.decode('utf-8'))
payload = base64.b64decode(envelope['message']['data'])
MESSAGES.append(payload)
# Returning any 2xx status indicates successful receipt of the message.
return 'OK', 200
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html', messages=MESSAGES)
data = request.form.get('payload', 'Example payload').encode('utf-8')
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(
current_app.config['PROJECT'],
current_app.config['PUBSUB_TOPIC'])
publisher.publish(topic_path, data=data)
return 'OK', 200
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__' :
app.run(host='127.0.0.1',port=8080,debug=True)
非常感谢对此问题的任何帮助。
For web applications it’s crucial to react to the data a client sends to the server. In Flask this information is provided by the global request object.
您缺少 request
导入,您需要添加它:
from flask import request