1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: UTF-8 -*-
  2. from flask import Flask
  3. from config import SysConfig
  4. from flask import request
  5. import pymysql
  6. import json
  7. # 物件初始化
  8. app = Flask(__name__)
  9. app.config.from_object(SysConfig)
  10. connection = pymysql.connect(
  11. host=app.config['DB_HOST'],
  12. port=app.config['DB_PORT'],
  13. user=app.config['DB_USER'],
  14. password=app.config['DB_PASS'],
  15. database=app.config['DB_NAME'],
  16. charset='utf8'
  17. )
  18. # 路由(API 實作)
  19. @app.route('/test', methods=['GET'])
  20. def test():
  21. # 接收參數
  22. request.args.get('user', 'admin')
  23. # 參數驗證 (自己刻或者使用公版的 reqparse)
  24. # 資料庫
  25. cursor = connection.cursor()
  26. sql = "SELECT mycase FROM post limit 2;"
  27. cursor.execute(sql)
  28. result_set = cursor.fetchall()
  29. cursor.close()
  30. # 數據處理
  31. # 返回
  32. return json.dumps(result_set, ensure_ascii=False)
  33. # 入口
  34. if __name__ == '__main__':
  35. app.run()