1. Operation steps
- Import: import flask,json
- Instantiation: api = flask.Flask(name)
- Define the interface access path and access method: @api.route('/index',methods=['get/post/PUT/DELETE'])
- Define the function, pay attention to the same as the name of the path, set the return type and support Chinese: def index(): return json.dumps(ren,ensure_ascii=False)
- Three format input parameters access interface:
5.1 Enter parameters in url format: flask.request.args.get('id')
5.2 Input parameters in form-data format: pwd = flask.request.values.get('pwd')
5.3 josn format input parameters: pwd = flask.request.json.get('pwd')- Start the service: api.run(port=8888,debug=True,host='127.0.0.1'), after opening the service, you can access the interface through ip+port+path+input parameters
2. Source code example
#! /usr/bin/python3
# encoding:utf-8import flask,json
# Instantiate the api, treat the current python file as a service,__name__Represents the current python file
api = flask.Flask(__name__)
# ' index'Is the interface path, the methods are not written, the default get request
@ api.route('/index',methods=['get'])
# get access
def index():
ren ={'msg':'Successfully visited the homepage','msg_code':200}
# json.The ascii encoding used by default for Chinese during dumps serialization.You need to specify ensure if you want to output Chinese_ascii=False
return json.dumps(ren,ensure_ascii=False)
# Post entry access method 1: url format parameters
@ api.route('/article',methods=['post'])
def article():
# URL format parameters?id=12589&name='lishi'
id = flask.request.args.get('id')if id:if id =='12589':
ren ={'msg':'Successfully accessed the article','msg_code':200}else:
ren ={'msg':'No article found','msg_code':400}else:
ren ={'msg':'Please enter the article id parameter','msg_code':-1}return json.dumps(ren,ensure_ascii=False)
# Post entry access method 2: from-data(k-v) Format parameters
@ api.route('/login',methods=['post'])
def login():
# from-data format parameters
usrname = flask.request.values.get('usrname')
pwd = flask.request.values.get('pwd')if usrname and pwd:if usrname =='test' and pwd =='123456':
ren ={'msg':'login successful','msg_code':200}else:
ren ={'msg':'wrong user name or password','msg_code':-1}else:
ren ={'msg':'username or password is empty','msg_code':1001}return json.dumps(ren,ensure_ascii=False)
# Post entry access method 2: josn format parameters
@ api.route('/loginjosn',methods=['post'])
def loginjosn():
# from-data format parameters
usrname = flask.request.json.get('usrname')
pwd = flask.request.json.get('pwd')if usrname and pwd:if usrname =='test' and pwd =='123456':
ren ={'msg':'login successful','msg_code':200}else:
ren ={'msg':'wrong user name or password','msg_code':-1}else:
ren ={'msg':'username or password is empty','msg_code':1001}return json.dumps(ren,ensure_ascii=False)if __name__ =='__main__':
api.run(port=8888,debug=True,host='127.0.0.1') #Start service
# debug=True,After changing the code, there is no need to restart, it will restart automatically
# ' host='127.0.0.1'Different IP access address
operation result
- Serving Flask app “restful” (lazy loading)
- Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.- Debug mode: on
- Restarting with stat
- Debugger is active!
- Debugger PIN: 249-915-285
- Running on http://127.0.0.1:8888/ (Press CTRL+C to quit)
Three, postman access interface
get mode, access interface without parameters
Post mode, url format input parameter access interface
Post mode, form-data format input parameter access interface
Post mode, josn format input access interface
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts