接口_注冊接口


注冊接口:注冊時填寫的用戶名、密碼與數據庫的用戶名、密碼做比對

 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訪問了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM