| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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/<id>')
- def show(id):
- try:
- with open('html/show.html', 'r') as f:
- return f.read()
- except:
- return ""
-
- @app.route('/show_new/<id>')
- def show_new(id):
- try:
- with open('html/show.new.html', 'r') as f:
- return f.read()
- except:
- return ""
- @app.route('/show/all/<id>')
- 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/<id>')
- 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/<id>')
- 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/<id>')
- 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/<id>')
- 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)
|