1234567891011121314151617181920212223242526272829303132333435363738394041 |
- # -*- coding: UTF-8 -*-
- from flask import Flask
- from config import SysConfig
- from flask import request
- import pymysql
- import json
-
- # 物件初始化
- app = Flask(__name__)
- app.config.from_object(SysConfig)
- connection = pymysql.connect(
- host=app.config['DB_HOST'],
- port=app.config['DB_PORT'],
- user=app.config['DB_USER'],
- password=app.config['DB_PASS'],
- database=app.config['DB_NAME'],
- charset='utf8'
- )
-
-
- # 路由(API 實作)
- @app.route('/test', methods=['GET'])
- def test():
- # 接收參數
- request.args.get('user', 'admin')
- # 參數驗證 (自己刻或者使用公版的 reqparse)
- # 資料庫
- cursor = connection.cursor()
- sql = "SELECT mycase FROM post limit 2;"
- cursor.execute(sql)
- result_set = cursor.fetchall()
- cursor.close()
- # 數據處理
- # 返回
- return json.dumps(result_set, ensure_ascii=False)
-
-
- # 入口
- if __name__ == '__main__':
- app.run()
|