提问人:abc 提问时间:11/12/2023 最后编辑:abc 更新时间:11/12/2023 访问量:24
在 GCP 上使用 flask-socketio 连接到 HTML 时出现 400 错误请求错误
400 bad request error when connecting to html with flask-socketio on gcp
问:
下面是错误示例
GET https://eco-crow-403708.de.r.appspot.com/socket.io/?EIO=3&transport=polling&t=Ol3IX-n 400(错误请求)
此错误每秒出现在控制台上
它在本地运行良好,但是当我将其上传到 gcp 时出现错误
我的 server.py
from flask import Flask, render_template, request, jsonify
from google.cloud import storage
import mysql.connector
from datetime import datetime
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="\*")
storage_client = storage.Client()
bucket_name = "-"
bucket = storage_client.bucket(bucket_name)
connection_name = "-"
conn = mysql.connector.connect(
unix_socket=f'/cloudsql/{connection_name}',
user='root',
password='-',
database='-',
)
cursor = conn.cursor()
@app.route('/', methods=\['POST', 'GET'\])
def index():
if request.method == 'POST':
try:
data = request.get_json()
if data and 'num_head' in data:
num_head = data\['num_head'\]
num_helmet = data\['num_helmet'\]
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
return jsonify(data), 200
else:
return jsonify({"message": "error"}), 400
except Exception as e:
return jsonify({"message": "error"}), 400
return render_template('index.html', detection_data=detection_data)
@app.route('/update_notification', methods=\['POST'\])
def update_notification():
message = 'post.'
print('Sending notification: ' + message)
socketio.emit('notification', {'message': message})
return jsonify({"message": "Notification received."}), 200
if __name__ == '__main__':
socketio.run(app)
我尝试更改 CORS 设置并将端口设置为适合,但都失败了
答: 暂无答案
评论