亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python restful框架接口開發(fā)實(shí)現(xiàn)

 更新時(shí)間:2020年04月13日 10:12:21   作者:天天向上327  
這篇文章主要介紹了Python restful框架接口開發(fā)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

理解

  • 每一個(gè)URL代表一種資源
  • 客戶端和服務(wù)端之間,傳遞這種資源的某種表現(xiàn)層,客戶端通過四個(gè)HTTP動(dòng)詞
  • 對服務(wù)端資源進(jìn)行操作,實(shí)現(xiàn)“表現(xiàn)層狀態(tài)轉(zhuǎn)化”
  • 資源:網(wǎng)絡(luò)的具體信息,如圖片、文字等
  • 表現(xiàn)層:"資源"是一種信息實(shí)體,它可以有多種外在表現(xiàn)形式。我們把"資源"具體呈現(xiàn)出來的形式,如,文本可以用txt格式表現(xiàn),也可以用HTML格式、XML格式、JSON格式表現(xiàn)
  • 狀態(tài)轉(zhuǎn)化:訪問一個(gè)網(wǎng)站,就代表了客戶端和服務(wù)器的一個(gè)互動(dòng)過程。在這個(gè)過程中,勢必涉及到數(shù)據(jù)和狀態(tài)的變化。
  • 4個(gè)HTTP動(dòng)詞:GET用來獲取資源,POST用來新建資源(也可以用于更新資源),PUT用來更新資源,DELETE用來刪除資源。

安裝 flask restful

1.cmd輸入:pip install flask,安裝flask

2.cmd輸入:pip install flask-restful,安裝flask-restful

安裝過程中會(huì)出現(xiàn)如下報(bào)錯(cuò):

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.

解決方法

升級pip python -m pip install --upgrade pip

注意:某些Flask版本下,引入模塊時(shí)采用from flask.ext.restful import Api出錯(cuò),則可以使用from flask_restful import Api

官網(wǎng)教程

例證

restful.py 內(nèi)容:

#!/usr/bin/python3
# encoding:utf-8
from flask import Flask,request
from flask_restful import reqparse, abort, Api, Resource

#初始化app、api
app = Flask(__name__)
api = Api(app)

LISTS = [
  {'parameter': '首頁'},
  {'parameter': '登錄'},
  {'parameter': '后臺(tái)'}
]

# /LISTS/<list_id>(url參數(shù)),判斷輸入的參數(shù)值列表LISTS下標(biāo)越界,越界則退出
def abort_if_list_doesnt_exist(list_id):
  try:
    LISTS[list_id]
  except IndexError:
    abort(404, message="輸入的值,不在范圍內(nèi)")
'''
add_argument('per_page', type=int, location='args') str
add_argument中通過指定參數(shù)名、參數(shù)類型、參數(shù)獲取方式來獲取參數(shù)對象并支持做合法性校驗(yàn)
第一個(gè)參數(shù)是需要獲取的參數(shù)的名稱
參數(shù)type: 參數(shù)指的類型, 如果參數(shù)中可能包含中文需要使用six.text_type. 或直接不指定type
參數(shù)location: 獲取參數(shù)的方式,可選的有args(url中獲取)、json(json類型的)、form(表單方式提交)
參數(shù)required:是否必要,默認(rèn)非必要提供 required=True(必須)
參數(shù)help:針對必要的參數(shù),如果請求時(shí)沒有提供,則會(huì)返回help中相應(yīng)的信息
'''
parser = reqparse.RequestParser()
#入?yún)arameter,location='json'表示為入?yún)閖son格式
parser.add_argument('parameter',location='json')

# 路由類,函數(shù)get、post、put、delete等實(shí)現(xiàn)http請求方法
# url不帶入?yún)?/LISTS
class c_dictList(Resource):
  #類型get,根據(jù)列表LISTS,處理,返回一個(gè)新的列表r_lists
  def get(self):
    r_lists = []
    for listV in LISTS:
      if listV:
        new_list = {}
        #LISTS列表存的是字典,遍歷時(shí)為字典listV['parameter'],可獲取字典值
        new_list['parameter'] = listV['parameter']
        #LISTS為列表,index可以查出對應(yīng)下標(biāo)值
        new_list['url'] = 'url/'+ str(LISTS.index(listV))
        #LISTS列表中添加字典
        r_lists.append(new_list)
    return r_lists
    
  #類型post,在列表LISTS后添加一個(gè)值,并返回列表值
  def post(self):
    args = parser.parse_args()
    list_id = len(LISTS)
    #args['parameter'],入?yún)?
    LISTS.append({'parameter': args['parameter']}) 
    return LISTS, 201
  
# 路由類,函數(shù)get、post、put、delete等實(shí)現(xiàn)http請求方法
# url帶入?yún)?/LISTS/<list_id>
class c_dict(Resource):
  #根據(jù)輸入url入?yún)⒅底鳛長ISTS的下標(biāo),返回該值
  def get(self, list_id):
    url_int = int(list_id)
    abort_if_list_doesnt_exist(url_int)
    return LISTS[url_int]
  #根據(jù)輸入url入?yún)⒅底鳛長ISTS的下標(biāo),修改該值,并返回列表值
  def put(self, list_id):
    url_int = int(list_id)
    args = parser.parse_args()
    #args['parameter'],入?yún)?
    parameter = {'parameter': args['parameter']}
    LISTS[url_int] = parameter
    return LISTS, 201
  #根據(jù)輸入url入?yún)⒅底鳛長ISTS的下標(biāo),刪除該值
  def delete(self, list_id):
    url_int = int(list_id)
    abort_if_list_doesnt_exist(url_int)
    del LISTS[url_int]
    return '', 204
#設(shè)置資源路由api.add_resource(類名,url路徑)
#url,不帶入?yún)?,如:http://127.0.0.1:8891/LISTS
api.add_resource(c_dictList, '/LISTS')
#url,帶入?yún)ⅲ?lt;list_id>為變量值,如:http://127.0.0.1:8891/LISTS/1
api.add_resource(c_dict, '/LISTS/<list_id>')

if __name__ == '__main__':
  #不設(shè)置ip、端口,默認(rèn):http://127.0.0.1:5000/
  #app.run(debug=True)
  #設(shè)置ip、端口
  app.run(host="127.0.0.1", port=8891,debug=True)

控制臺(tái)運(yùn)行結(jié)果:

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調(diào)用結(jié)果

url不帶參數(shù)

get

post,有請求入?yún)?,格式為json,入?yún)⒅底芳拥搅斜砗竺?/p>

url帶參數(shù)get,根據(jù)url入?yún)⒅等缦聢D值=1,作為LISTS的下標(biāo),獲取列表值

put ,根據(jù)url入?yún)⒅等缦聢D值=1,作為LISTS的下標(biāo),修改該列表值為請求入?yún)⒅担卿浉臑橛唵?/p>

put ,根據(jù)url入?yún)⒅等缦聢D值=2,作為LISTS的下標(biāo),刪除該值,成功返回狀態(tài)204

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python+matplotlib繪制餅圖散點(diǎn)圖實(shí)例代碼

    python+matplotlib繪制餅圖散點(diǎn)圖實(shí)例代碼

    這篇文章主要介紹了python+matplotlib繪制餅圖散點(diǎn)圖實(shí)例代碼,本實(shí)例是官網(wǎng)的一個(gè)實(shí)例,不過也很簡單,大家可以參考下
    2018-01-01
  • Python中的time和datetime模塊使用方法詳解

    Python中的time和datetime模塊使用方法詳解

    Python 中的 time 和 datetime 模塊是處理時(shí)間和日期的重要工具,它們可以執(zhí)行各種操作,如獲取當(dāng)前時(shí)間、格式化日期、計(jì)算時(shí)間差等,本文將分享這兩個(gè)模塊的使用方法,包括安裝、基本功能、日期時(shí)間對象、時(shí)間戳、時(shí)間間隔、日期時(shí)間格式化和示例代碼
    2023-11-11
  • python實(shí)現(xiàn)n個(gè)數(shù)中選出m個(gè)數(shù)的方法

    python實(shí)現(xiàn)n個(gè)數(shù)中選出m個(gè)數(shù)的方法

    今天小編就為大家分享一篇python實(shí)現(xiàn)n個(gè)數(shù)中選出m個(gè)數(shù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • 最新評論