server.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from flask import Flask,render_template,Response,request
  2. import time
  3. import mysqlhelper
  4. import flask
  5. import json
  6. import datetime
  7. # json时间问题和bytes问题解决
  8. class JsonEncoder(json.JSONEncoder):
  9. def default(self, obj):
  10. if isinstance(obj,datetime.datetime):
  11. return obj.strftime("%Y-%m-%d %H:%M:%S")
  12. elif isinstance(obj,bytes):
  13. return str(obj, 'utf-8')
  14. else:
  15. return json.JSONEncoder.default(self,obj)
  16. app=Flask(__name__, static_folder='static',template_folder='static')
  17. app.jinja_env.auto_reload=True
  18. # Response('{"success":false}', mimetype='application/json')
  19. @app.route('/testpage')
  20. def testpage():
  21. try:
  22. with open('html/testpage.html', 'r') as f:
  23. return f.read()
  24. except:
  25. return ""
  26. # Response('{"success":false}', mimetype='application/json')
  27. @app.route('/show/<id>')
  28. def show(id):
  29. try:
  30. with open('html/show.html', 'r') as f:
  31. return f.read()
  32. except:
  33. return ""
  34. @app.route('/show_new/<id>')
  35. def show_new(id):
  36. try:
  37. with open('html/show.new.html', 'r') as f:
  38. return f.read()
  39. except:
  40. return ""
  41. @app.route('/show/all/<id>')
  42. def show_all(id):
  43. try:
  44. with open('html/show.all.html', 'r') as f:
  45. return f.read()
  46. except:
  47. return ""
  48. @app.route('/')
  49. def index():
  50. try:
  51. with open('html/index.html', 'r') as f:
  52. return f.read()
  53. except:
  54. return ""
  55. @app.route('/course/<id>')
  56. def index_course(id):
  57. try:
  58. with open('html/index.course.html', 'r') as f:
  59. return f.read()
  60. except:
  61. return ""
  62. @app.route('/get/course')
  63. def get_course():
  64. data = db.selectone("select time, data from course")
  65. if isinstance(data, int):
  66. # 失败
  67. return Response('{"success":0}', mimetype='application/json')
  68. d = {
  69. "success":1,
  70. "time": data[0],
  71. "data": json.loads(data[1])
  72. }
  73. return Response(json.dumps(d, cls=JsonEncoder, ensure_ascii=False), mimetype='application/json')
  74. @app.route('/get/question/list/<id>')
  75. def get_question_list(id):
  76. data = db.selectone("select data from question_list where course_id = %s", id)
  77. if isinstance(data, int) or not data:
  78. # 失败
  79. return Response('{"success":0}', mimetype='application/json')
  80. d = {
  81. "success":1,
  82. "data": json.loads(data[0])
  83. }
  84. return Response(json.dumps(d, cls=JsonEncoder, ensure_ascii=False), mimetype='application/json')
  85. @app.route('/get/question/content/<id>')
  86. def get_question_content(id):
  87. data = db.selectone("select content from question_content where question_id = %s", id)
  88. if isinstance(data, int) or not data:
  89. # 失败
  90. return Response('{"success":0}', mimetype='application/json')
  91. d = {
  92. "success":1,
  93. "data": str(data[0], 'utf-8')
  94. }
  95. return Response(json.dumps(d, cls=JsonEncoder, ensure_ascii=False), mimetype='application/json')
  96. @app.route('/get/question/content_object/<id>')
  97. def get_question_content_object(id):
  98. data = db.selectone("select content_object from question_content_object where question_id = %s", id)
  99. if isinstance(data, int) or not data:
  100. # 失败
  101. return Response('{"success":0}', mimetype='application/json')
  102. d = {
  103. "success":1,
  104. "data": json.loads(data[0])
  105. }
  106. return Response(json.dumps(d, cls=JsonEncoder, ensure_ascii=False), mimetype='application/json')
  107. if __name__ == '__main__':
  108. db = mysqlhelper.MySqLHelper()
  109. app.run(host='127.0.0.1',port=8381)