Python restful framework interface development and implementation

understanding

Install flask restful

  1. cmd input: pip install flask, install flask

  2. cmd input: pip install flask-restful, install flask-restful

The following error will appear during the installation process:

You are using pip version 9.0.1, however version 19.2.3 is available. You should consider upgrading via the ‘python -m pip install –upgrade pip’ comm and.

Solution

Upgrade pip python -m pip install --upgrade pip

Note: In some Flask versions, if the from flask.ext.restful import Api error occurs when importing the module, you can use from flask_restful import Api

Official website tutorial

illustration

restful.py content:

#! /usr/bin/python3
# encoding:utf-8from flask import Flask,request
from flask_restful import reqparse, abort, Api, Resource
# Initialize app, api
app =Flask(__name__)
api =Api(app)
LISTS =[{'parameter':'Home'},{'parameter':'log in'},{'parameter':'Backstage'}]
# /LISTS/<list_id (url parameter), it is judged that the LISTS subscript of the input parameter value list is out of range, and if it exceeds the range, exit
def abort_if_list_doesnt_exist(list_id):try:
LISTS[list_id]
except IndexError:abort(404, message="The entered value is not within the range")'''
add_argument('per_page', type=int, location='args') str
add_In argument, the parameter object is obtained by specifying the parameter name, parameter type, and parameter acquisition method and supports legality verification
The first parameter is the name of the parameter that needs to be obtained
Parameter type:The type of the parameter, if the parameter may contain Chinese, you need to use six.text_type.Or directly don&#39;t specify type
Parameter location:The way to get parameters, optional args(Get in url)、json(json type)、form(Form submission)
Parameter required:Whether it is necessary or not, required by default=True(have to)
Parameter help:For the necessary parameters, if the request is not provided, the corresponding information in the help will be returned
'''
parser = reqparse.RequestParser()
# Input parameter, location='json'Represented as input parameters in json format
parser.add_argument('parameter',location='json')
# Routing class, functions get, post, put, delete, etc. to implement http request methods
# URL does not include parameters/LISTS
classc_dictList(Resource):
# Type get, process according to the list LISTS, and return a new list r_lists
def get(self):
r_lists =[]for listV in LISTS:if listV:
new_list ={}
# The LISTS list stores a dictionary, and when traversing, it is a dictionary listV['parameter']To get the dictionary value
new_list['parameter']= listV['parameter']
# LISTS is a list, index can find out the corresponding subscript value
new_list['url']='url/'+str(LISTS.index(listV))
# Add a dictionary to the LISTS list
r_lists.append(new_list)return r_lists
# Type post, add a value after the list LISTS, and return the list value
def post(self):
args = parser.parse_args()
list_id =len(LISTS)
# args['parameter'], Join
LISTS.append({'parameter': args['parameter']})return LISTS,201
# Routing class, functions get, post, put, delete, etc. to implement http request methods
# url into the parameters/LISTS/<list_id 
classc_dict(Resource):
# According to the input url input parameter value as the subscript of LISTS, return the value
def get(self, list_id):
url_int =int(list_id)abort_if_list_doesnt_exist(url_int)return LISTS[url_int]
# According to the input url input parameter value as the subscript of LISTS, modify the value, and return the list value
def put(self, list_id):
url_int =int(list_id)
args = parser.parse_args()
# args['parameter'], Join
parameter ={'parameter': args['parameter']}
LISTS[url_int]= parameter
return LISTS,201
# According to the input url input parameter value as the subscript of LISTS, delete the value
def delete(self, list_id):
url_int =int(list_id)abort_if_list_doesnt_exist(url_int)
del LISTS[url_int]return'',204
# Set up resource routing api.add_resource (class name, url path)
# url, without parameters, such as: http://127.0.0.1:8891/LISTS
api.add_resource(c_dictList,'/LISTS')
# url, bring in the parameters,<list_id is the variable value, such as: http://127.0.0.1:8891/LISTS/1
api.add_resource(c_dict,'/LISTS/<list_id ')if __name__ =='__main__':
# Do not set ip, port, default: http://127.0.0.1:5000/
# app.run(debug=True)
# Set ip, port
app.run(host="127.0.0.1", port=8891,debug=True)

Console running results:

Serving Flask app “123” (lazy loading) * Environment: production
WARNING: This is a development server. Do not use it in a productiondeployment. Use a production WSGI server instead. * Debug mode: onRestarting with stat * Debugger is active! * Debugger PIN: 279-443-943 * Running on http://127.0.0.1:8891/ (Press CTRL+C toquit)

Postman call result

url without parameters

get

post, there is a request to enter the parameters, the format is json, the input parameter value is appended to the back of the list

url with parameter get, enter the parameter value according to the url as shown in the figure = 1, as the subscript of LISTS, get the list value

put, enter the parameter value according to the URL as shown in the figure = 1, as the subscript of LISTS, modify the list value to request input parameter value, and log in to order

put, enter the parameter value according to the url as shown in the figure below, as the subscript of LISTS, delete the value, and return to status 204 successfully

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python restful framework interface development and implementation
Detailed explanation of the implementation steps of Python interface development
Python3 interface development commonly used.md
FM algorithm analysis and Python implementation
Python implementation of intersection and IOU tutorial
Naive Bayes algorithm and its Python implementation
Python and Go
Least squares method and its python implementation details
Python introspection and reflection
python development [first article]
[python] python2 and python3 under ubuntu
Python GUI interface programming
Python deconstruction and packaging
Python3 configuration and entry.md