pyyyy

import boto3
from flask import Flask , request , jsonify
import psycopg2

app = Flask(name)

region = "us-east-1"
dynamodb = boto3.resource('dynamodb', region_name=region)
table = dynamodb.Table('dynamodb-table')

RDS_CONFIG = {
"database":"postgres",
"user":"fbase",
"password":"123456",
"host":"192.168.198.152",
"port":"8432"
}

@app.route('/save_data', methods=["POST"])
def save_data():
data = request.get_json()
data1 = data["records"]
data2 = data1[:2]
data3 = data2[0]
id = data3["code"][-2:]
value1= int(data3["value"])
if value1 > 100 :
table.put_item(
Item={
"id": id,
"value": value1
}
)
else:
coon = psycopg2.connect(RDS_CONFIG)
cur = coon.cursor()
cur.execute("INSERT INTO measurements (id , value) VALUES (%s,%s)" , (id , value1))
coon.commit()
cur.close()
coon.close()
return jsonify(
{
"Codestatus":200 ,"mesgg":"chenggon"
}
)
@app.route('/get_value', methods=["GET"])
def get_value():
id = request.args.get("id")
value_Item = table.get_item(
Key={
"id":id
}
)
dynamodb_value = int(value_Item["Item"].get("value",""))
coon = psycopg2.connect(
RDS_CONFIG)
cur = coon.cursor()
cur.execute("select value from measurements where id = %s" , (id,))
RDS_value = int(cur.fetchone()[0])
cur.close()
coon.close()
return jsonify(
{
"Codestatus":200 ,"mesgg": dynamodb_value+RDS_value
}
)

if name == "main":
app.run(host="0.0.0.0",port=80)

posted @ 2026-11-09 21:15  tabtab--007  阅读(6)  评论(0)    收藏  举报