import flask
import json
#1、mock接口
#2、给别人提供数据
#flask web开发框架
server = flask.Flask(__name__)
import pymysql
def op_mysql(sql,many_tag=False):
conn = pymysql.connect(host='118.24.3.40',user='jxz',password='123456',
db='jxz',port=3306,charset='utf8',
autocommit=True)
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute(sql)
if many_tag:
result = cur.fetchall() # [{"id":1,"name":"xxx"},{"id":2,"name":"xxx"}]
else:
result = cur.fetchone()
cur.close()
conn.close()
return result
@server.route('/index')
def login():
d = {"code":0,"msg":"登录成功 niuhanyangq111!"}
return json.dumps(d,ensure_ascii=False)
@server.route('/get_data')
def table_data():
table_list = ['app_myuser','app_product','app_student']
table_name = flask.request.args.get('table_name')
limit = flask.request.args.get('limit',10)
if table_name:
if table_name in table_list:
sql='select * from %s limit %s;'%(table_name,limit)
print(sql)
data = op_mysql(sql,True)
else:
data = {"code": -2, 'msg': "你没有权限查看这个表里面的数据!"}
else:
data = {"code":-1,'msg':"必填参数未填,请查看接口文档"}
return json.dumps(data,ensure_ascii=False)
@server.route('/post_data',methods=['post'])
def post():
username = flask.request.args.get('username')#参数在url里面的话,用它
username2 = flask.request.values.get('username2')#参数在body里面,用它
file = flask.request.files.get('f')#上传文件
cookies = flask.request.cookies.get('f')#cookie
headers = flask.request.headers.get('f')#headers
# headers = flask.request.json.get('username')#json格式的
file.save(file.filename)
return 'abc'
server.run(host='0.0.0.0',port=8989,debug=True)
#app_user,app_product,app_student