Registration interface: compare the username and password filled in during registration with the username and password of the database
1 import flask 2 server = flask.Flask( __name__ ) #Take our current python file as a service 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()#Create cursor 10 cur.execute(sql) #Execute 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 ' : ' Registration successful! ' , ' msg_code ' : 0} 32 else : 33 res={ ' msg ' : ' The required fields are not filled, please check the interface documentation! ' , ' msg_code ' :1001 ' } 34 # 1001 Required fields are not filled in 35 return json.dumps(res,ensure_ascii= False) 36 37 server.run(port = 7777,debug=True,host= ' 0.0.0.0 ' ) 38 # debug=True , means that after changing the code, you don't need to restart, it will automatically restart for you 39 #After specifying host='0.0.0.0', others can use your ip to access