Python Learning (24) Development interface

Source: Internet
Author: User

Simulate some interfaces that can be used to test the interface before it is successfully developed

Used to query data

1. First interface
ImportFlask,json#__name__, which represents the current Python fileServer=flask. Flask (__name__)#take this Python file as a service#Ip:8000/index?uge@server. Route ('/index', methods=['Get','Post'])#can only write one, then only support that onedefindex (): Res={'msg':'This is the first interface to develop','Msg_code': 0}returnJson.dumps (res,ensure_ascii=False) Server.run (Port=5555,debug=true)#Debug=true, after changing the code, do not restart, will automatically restart

After executing the program, open the local 127.0.0.1:5555/index, the interface will display: ' msg ': ' This is the first interface to develop ', ' Msg_code ': 0

2. Registration interface

Registered with the user name password, if the user already exists in the database, the prompt is returned, otherwise written to the database and returned to the successful registration

When executing the code, you must have a function of the database operation, which has been written before, as follows:

defmy_db (SQL):ImportPymysql Coon=Pymysql.connect (Host='xxxx', user='Jxz', passwd='123456', Port=3306, db='Jxz', charset='UTF8') cur= Coon.cursor ()#Creating CursorsCur.execute (SQL)#Execute SQL    ifSql.strip () [: 6].upper () = ='SELECT': Res=Cur.fetchall ()Else: Coon.commit () Res='OK'cur.close () coon.close ( )returnRes

Functions that call database operations in the following code

ImportFlask,jsonserver=flask. Flask (__name__) @server. Route ('/reg', methods=['Post'])defreg (): Username=flask.request.values.get ('username') PWD=flask.request.values.get ('passwd')    ifUsername andPwd:sql='SELECT * from My_user where username= "%s";'%usernameifmy_db (SQL): Res={'msg':'user already exists','Msg_code': 2001}        Else: Insert_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': 0}Else: Res={'msg':'required fields are not filled, please see the interface documentation','Msg_code': 1001}    returnJson.dumps (res,ensure_ascii=False) Server.run (Port=5555,debug=true,host='0.0.0.0') #加host='0.0.0.0', which means that other people use your IP to access the

Execute the program, open the Postman,url input 127.0.0.1:5555/reg, select the Post method, click Send directly, or the user name password is not fully written, will return: ' msg ': ' Required fields are not filled, please see the interface document ', ' Msg_code ' : 1001

if the user name and password values are written in the body, and the user does not exist in the database table, clicking Send will return:' msg ': ' Registered success ', ' Msg_code ': 0, and the database will be written

if the user name and password values are written in the body and the user exists in the database table, clicking Send will return:' msg ': ' User already exists ', ' Msg_code ': 2001

3.

Python Learning (24) Development interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.