In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use python to write api interface". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use python to write api interface".
1. Operation steps
Import: import flask,json
Instantiate: api = flask.Flask (name)
Define the access path and access method of the interface: @ api.route ('/ index',methods= ['get/post/PUT/DELETE'])
Define the function, note that it should be consistent with the name of the path, set the return type and support Chinese: def index (): return json.dumps (ren,ensure_ascii=False)
There are three kinds of input parameter access interfaces:
5.1 url format input parameter: flask.request.args.get ('id')
5.2 input parameter for form-data format: pwd = flask.request.values.get ('pwd')
5.3 input parameter for josn format: pwd = flask.request.json.get ('pwd')
Start the service: api.run (port=8888,debug=True,host='127.0.0.1'). After enabling the service, you can access the interface via ip+ port + path + input parameter.
2. Source code example #! / usr/bin/python3# encoding:utf-8import flask,json# instantiates api and treats the current python file as a service. _ _ name__ represents the current python file api = flask.Flask (_ _ name__) # 'index' is the interface path, but methods does not write it. The default get request @ api.route ('/ index',methods= ['get']) # get accesses def index (): ren = {' msg':' successfully accessed the home page', 'msg_code':200} # the ascii encoding used by default for Chinese when serializing json.dumps. To export Chinese, you need to specify ensure_ascii=False return json.dumps (ren,ensure_ascii=False) # post input parameter access method 1: url format parameter @ api.route ('/ article',methods= ['post']) def article (): # url format parameter? 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':' cannot find the article', 'msg_code':400} else: ren = {' msg':' Please enter the article id parameter', 'msg_code':-1} return json.dumps (ren,ensure_ascii=False) # post input parameter 2: from-data (KMTV) format parameter @ api.route (' / login') Methods= ['post']) def login (): # from-data format parameter usrname = flask.request.values.get (' usrname') pwd = flask.request.values.get ('pwd') if usrname and pwd: if usrname =' test' and pwd = = '123456login: ren = {' msg':' login successful', 'msg_code':200} else: ren = {' msg':' username or password error' 'msg_code':-1} else: ren = {' msg':' username or password is empty', 'msg_code':1001} return json.dumps (ren,ensure_ascii=False) # post input parameter 2: josn format parameter @ api.route (' / loginjosn' Methods= ['post']) def loginjosn (): # from-data format parameter usrname = flask.request.json.get (' usrname') pwd = flask.request.json.get ('pwd') if usrname and pwd: if usrname =' test' and pwd = = '123456 login: ren = {' msg':' login successful', 'msg_code':200} else: ren = {' msg':' username or password error' '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') # startup service # debug=True, after changing the code No need to restart, it will automatically restart the # 'host='127.0.0.1' other IP access address
Running result:
* Serving Flask app 'monitor' (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: 991,833,116
* Running on http://127.0.0.1:8888/ (Press CTRL+C to quit)
127.0.0.1-[16/Jan/2022 14:05:53] "POST / login?usrname=test&pwd=123456 HTTP/1.1" 200-
127.0.0.1-[16/Jan/2022 14:08:34] "GET / index HTTP/1.1" 200-
Request method:
Use postman to test whether the interface is feasible
Such as:
Url:127.0.0.1:8888/login
Parameter: usrname=test;pwd=123456
Several ways to get request parameters:
Flask.request.form.get ("key", type=str, default=None) gets the form data, flask.request.args.get ("key") gets the get request parameters, and flask.request.values.get ("key") gets all the parameters. Thank you for your reading, the above is the content of "how to use python to write api interface". After the study of this article, I believe you have a deeper understanding of how to use python to write api interface, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.