Here's the issue.I was trying to build up a database server using flask deployed on IIS and it is available now.But the issue is after long-time running(maybe 4 or 5 days),the flask server got to return no response without no error!
Is it a bug of Flask or something else? Here's my code. Thank everyone!
from flask import Flask,abort
from flask import jsonify
from flask import request
from flask import make_response
from flask_sqlalchemy import SQLAlchemy
import MySQLdb
import json
from flask_cors import CORS, cross_origin
import time
from datetime import datetime
envdata = []
hard = {
'ch0':'',
'ch1':'',
'ch2':'',
'ch3':''
}
app = Flask(__name__, static_url_path='')
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:coma@localhost/smart'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class enviData(db.Model):
__tablename__ = 'smartData'
id = db.Column(db.Integer, primary_key=True, nullable = False, autoincrement = True)
date = db.Column(db.Integer, nullable = False)
hour = db.Column(db.Integer, nullable = False)
minute = db.Column(db.Integer, nullable = False)
temp = db.Column(db.Integer, nullable = False)
PM2_5 = db.Column(db.Integer, nullable =False)
def __init__(self, id, date, hour, minute, temp, PM2_5):
self.id = id
self.date = date
self.hour = hour
self.minute = minute
self.temp = temp
self.PM2_5 = PM2_5
def __repr__(self):
return '<ID %r>' % self.id
db.create_all()
@app.route('/')
def welcome():
return app.send_static_file("index.html")
@app.route('/envidata/', methods = ['POST'])
def add_data():
if ((not request.json) or (not 'id' in request.json) or (not 'temp' in request.json) or (not 'date' in request.json) or (not 'hour' in request.json) or (not 'minute' in request.json) or (not 'PM2_5' in request.json)):
abort(400)
envData = {
'id': request.json['id'],
'temp':request.json['temp'],
'date':request.json['date'],
'hour':request.json['hour'],
'minute':request.json['minute'],
'PM2_5':request.json['PM2_5']
}
newestData = {
'id': request.json['id'],
'temp':request.json['temp'],
'date':request.json['date'],
'hour':request.json['hour'],
'minute':request.json['minute'],
'PM2_5':request.json['PM2_5']
}
dataE = enviData(int(envData['id']), int(envData['date']), int(envData['hour']), int(envData['minute']), int(envData['temp']), int(envData['PM2_5']))
db.session.add(dataE)
db.session.commit()
return 'Successfully Added!'
@app.route('/hardware/control/', methods = ['POST'])
def hardCtrl():
if(not request.json):
abort(400)
hard.ch0 = request.json['ch0']
hard.ch1 = request.json['ch1']
hard.ch2 = request.json['ch2']
hard.ch3 = request.json['ch3']
return 'Got your command!'
@app.route('/envidata/date/', methods = ['GET'] )
def get_data_by_date():
if (not request.args['date']):
abort(400)
get_date = request.args['date']
dates = enviData.query.filter_by(date = get_date)
i = 0
buf1 = {}
data = {}
for row1 in dates:
buf1['id'] = row1.id
buf1['date'] = row1.date
buf1['hour'] = row1.hour
buf1['minute'] = row1.minute
buf1['temp'] = row1.temp
buf1['PM2_5'] = row1.PM2_5
data.setdefault(i, buf1)
i += 1
buf1 = {}
row1 = ''
jsonString = json.dumps(data)
if len(data) == 0:
abort(404)
return jsonString
@app.route('/envidata/hour/', methods = ['GET'])
def get_data_by_hour():
if (not request.args['hour'] or not request.args['date']):
abort(400)
get_hour = request.args['hour']
get_date = request.args['date']
hours = enviData.query.filter_by(date = get_date, hour = get_hour).all()
j = 0
buf2 = {}
data = {}
for row2 in hours:
buf2['id'] = row2.id
buf2['date'] = row2.date
buf2['hour'] = row2.hour
buf2['minute'] = row2.minute
buf2['temp'] = row2.temp
buf2['PM2_5'] = row2.PM2_5
data.setdefault(j, buf2)
j += 1
buf2 = {}
row2 = ''
jsonString = json.dumps(data)
if len(data) == 0:
abort(404)
return jsonString
@app.route('/envidata/newest/', methods = ['GET'])
def get_newest_data():
today = int(time.strftime('%Y%m%d', time.localtime(time.time())))
get_hour = int(time.strftime('%H', time.localtime(time.time())))
get_minute = int(time.strftime('%M', time.localtime(time.time())))
news = enviData.query.filter_by(date = today, hour = get_hour, minute = get_minute).all()
data = {}
q = 0
buf3 = {}
for row3 in news:
buf3['id'] = row3.id
buf3['date'] = row3.date
buf3['hour'] = row3.hour
buf3['minute'] = row3.minute
buf3['temp'] = row3.temp
buf3['PM2_5'] = row3.PM2_5
data.setdefault(q, buf3)
q += 1
buf3 = {}
row3 = ''
jsonString = json.dumps(data)
if len(data) == 0:
abort(404)
return jsonString
@app.route('/hardware/command/', methods = ['GET'])
def get_command():
command = '00' + str(hard.ch0) + '01' + str(hard.ch1) + '10' + str(hard.ch2) + '11' + str(hard.ch3)
return command
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
return response
app.run("120.24.70.93", port=1234)
Aucun commentaire:
Enregistrer un commentaire