Detailed explanation of the implementation steps of Python interface development

1. Operation steps

  1. Import: import flask,json
  2. Instantiation: api = flask.Flask(name)
  3. Define the interface access path and access method: @api.route('/index',methods=['get/post/PUT/DELETE'])
  4. 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)
  5. 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')
  6. 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

Detailed explanation of the implementation steps of Python interface development
Detailed explanation of the principle of Python super() method
Detailed explanation of the usage of Python decimal module
Detailed explanation of the attribute access process of Python objects
Detailed explanation of the remaining problem based on python (%)
Detailed explanation of python backtracking template
Detailed implementation of Python plug-in mechanism
Detailed explanation of python sequence types
Python restful framework interface development and implementation
What is the prospect of python development
Detailed explanation of -u parameter of python command
Detailed explanation of Python guessing algorithm problems
Detailed explanation of the use of pip in Python | summary of third-party library installation
Python realizes the development of student management system
Detailed explanation of python standard library OS module
Detailed explanation of how python supports concurrent methods
Detailed explanation of data types based on Python
Python writes the game implementation of fishing master
Detailed explanation of common tools for Python process control
Detailed explanation of Python web page parser usage examples
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Python implementation of gomoku program
Consolidate the foundation of Python (3)
Python3 interface development commonly used.md
Use of Pandas in Python development
Implementation of reverse traversal of python list
Python implementation of IOU calculation case
Python embeds C/C++ for detailed development
Detailed explanation of ubuntu using gpg2
Python handles the 4 wheels of Chinese
Implementation of python selenium operation cookie
Python error handling assert detailed explanation
Use of numpy in Python development
Python simulation of the landlord deal
What is the use of Python
Detailed explanation of the installation and use of SSH in the Ubuntu environment
Detailed usage of dictionary in Python
Implementation of python3 registration global hotkey
Implementation of python student management system
The premise of Python string pooling
Secrets of the new features of Python 3.8
Implementation of python gradient descent algorithm
Learning path of python crawler development
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Basic analysis of Python turtle library implementation
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Implementation of JWT user authentication in python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Python implementation of intersection and IOU tutorial
What is the scope of python variables
Detailed analysis of Python garbage collection mechanism