写在前言,仅供参考 ,实际没什么用
import pymysql
from flask import Flask
import flask_restful
import json
from flask import request
db = pymysql.connect(host='', user='', password='', database='', charset='utf8')
def mysql_data(db):
x = db.cursor()
try:
x.execute("select `url`,`title`,`date` from table where date > '20150101'")
except Exception as e:
db.rollback()
db.commit()
db.close()
return x.fetchall(),x.description #如果x.fetchone是空呢?怎么处理
'''
mysql_data()取数据库数据
'''
a = mysql_data(db)
ziduan_list=[]
for i in range(len(a[1])):
ziduan_list.append(a[1][i][0])
jsonData=[]
for row in a[0]:
result = {}
result[ziduan_list[0]]=row[0]
result[ziduan_list[1]]=row[1]
result[ziduan_list[2]]=row[2]
jsonData.append(result)
results = json.dumps(jsonData,ensure_ascii=False)
results = json.loads(results)
'''
处理数据
'''
app = Flask(__name__)
api = flask_restful.Api(app)
# class gov(flask_restful.Resource):
# def get(self):
# return results
# api.add_resource(gov, '/')
'''
写接口
'''
@app.route('/login', methods=['get', 'post'])
def login():
# 获取通过url请求传参的数据
name = request.values.get('name')
# 获取url请求传的密码,明文
pwd = request.values.get('pwd')
# 判断用户名、密码都不为空,如果不传用户名、密码则name和pwd为None
if name and pwd:
if name=='123' and pwd=='456':
#登陆成功后可以将信息写在cookie里
resu = {'code': 200, 'message': 'success','data':results}
return json.dumps(resu, ensure_ascii=False) # 将字典转换为json串, json是字符串
else:
resu = {'code': -1, 'message': 'failure'}
return json.dumps(resu, ensure_ascii=False)
else:
resu = {'code': 10001, 'message': 'param is none'}
return json.dumps(resu, ensure_ascii=False)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug = True)
代码执行结果:
Postman执行结果
参考别人写的但是没什么用啊,与前端交互不起来 ,前端结果如下
结果未显示,到现在我还是没有解决
|