from flask import Flask,render_template,Response,request import time import mysqlhelper import flask import json import datetime # json时间问题和bytes问题解决 class JsonEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj,datetime.datetime): return obj.strftime("%Y-%m-%d %H:%M:%S") elif isinstance(obj,bytes): return str(obj, 'utf-8') else: return json.JSONEncoder.default(self,obj) app=Flask(__name__, static_folder='static',template_folder='static') app.jinja_env.auto_reload=True # Response('{"success":false}', mimetype='application/json') @app.route('/testpage') def testpage(): try: with open('html/testpage.html', 'r') as f: return f.read() except: return "" # Response('{"success":false}', mimetype='application/json') @app.route('/show/') def show(id): try: with open('html/show.html', 'r') as f: return f.read() except: return "" @app.route('/show_new/') def show_new(id): try: with open('html/show.new.html', 'r') as f: return f.read() except: return "" @app.route('/show/all/') def show_all(id): try: with open('html/show.all.html', 'r') as f: return f.read() except: return "" @app.route('/') def index(): try: with open('html/index.html', 'r') as f: return f.read() except: return "" @app.route('/course/') def index_course(id): try: with open('html/index.course.html', 'r') as f: return f.read() except: return "" @app.route('/get/course') def get_course(): data = db.selectone("select time, data from course") if isinstance(data, int): # 失败 return Response('{"success":0}', mimetype='application/json') d = { "success":1, "time": data[0], "data": json.loads(data[1]) } return Response(json.dumps(d, cls=JsonEncoder, ensure_ascii=False), mimetype='application/json') @app.route('/get/question/list/') def get_question_list(id): data = db.selectone("select data from question_list where course_id = %s", id) if isinstance(data, int) or not data: # 失败 return Response('{"success":0}', mimetype='application/json') d = { "success":1, "data": json.loads(data[0]) } return Response(json.dumps(d, cls=JsonEncoder, ensure_ascii=False), mimetype='application/json') @app.route('/get/question/content/') def get_question_content(id): data = db.selectone("select content from question_content where question_id = %s", id) if isinstance(data, int) or not data: # 失败 return Response('{"success":0}', mimetype='application/json') d = { "success":1, "data": str(data[0], 'utf-8') } return Response(json.dumps(d, cls=JsonEncoder, ensure_ascii=False), mimetype='application/json') @app.route('/get/question/content_object/') def get_question_content_object(id): data = db.selectone("select content_object from question_content_object where question_id = %s", id) if isinstance(data, int) or not data: # 失败 return Response('{"success":0}', mimetype='application/json') d = { "success":1, "data": json.loads(data[0]) } return Response(json.dumps(d, cls=JsonEncoder, ensure_ascii=False), mimetype='application/json') if __name__ == '__main__': db = mysqlhelper.MySqLHelper() app.run(host='127.0.0.1',port=8381)