接口_注册接口
注册接口:注册时填写的用户名、密码与数据库的用户名、密码做比对
1 import flask 2 server = flask.Flask(__name__) #把咱们当前的这个python文件,当做一个服务 3 4 def my_db(sql): 5 import pymysql 6 coon = pymysql.connect( 7 host='xxx.xxx.xx.xxx', user='xxx', passwd='123456', 8 port=3306, db='xxx', charset='utf8') 9 cur = coon.cursor() #建立游标 10 cur.execute(sql)#执行sql 11 if sql.strip()[:6].upper()=='SELECT': 12 res = cur.fetchall() 13 else: 14 coon.commit() 15 res = 'ok' 16 cur.close() 17 coon.close() 18 return res 19 20 @server.route('/reg',methods=['post']) 21 def reg(): 22 username = flask.request.values.get('username') 23 pwd = flask.request.values.get('passwd') 24 if username and pwd: 25 sql = 'select * from my_user where username="%s";'%username 26 if my_db(sql): 27 res = {'msg':'用户已存在','msg_code':'2001'} 28 else: 29 insert_sql = 'insert into my_user (username,passwd,is_admin) values ("%s","%s",0);'%(username,pwd) 30 my_db(insert_sql) 31 res = {'msg':'注册成功!','msg_code':0} 32 else: 33 res={'msg':'必填字段未填,请查看接口文档!','msg_code':'1001'} 34 # 1001必填字段未填 35 return json.dumps(res,ensure_ascii=False) 36 37 server.run(port = 7777,debug=True,host='0.0.0.0') 38 #debug=True,表示改了代码后,不用重启,会自动帮你重启 39 #指定host='0.0.0.0'后别人就可以用你的ip访问了