资源标识符:在 restful 风格中,每个资源都有一个唯一的标识符,通常是一个 url(uniform resource locator)。url 用于标识资源的位置,使得客户端可以使用 http 协议进行访问。例如,一个简单的 url 可以是:http://example.com/products/123,其中“products”表示资源类型,“123”表示资源标识符。
表示层:资源可以以不同的格式表示,例如 json,xml,html 等。客户端可以根据需要选择适当的表示形式进行交互。例如,一个 restful api 可以返回 json 格式的数据,以便客户端可以更容易地解析和处理数据。
自描述消息:每个消息都应该包含足够的信息,以描述如何处理该消息。例如,http 响应应该包含状态码、响应头和响应正文等信息,以便客户端可以理解响应的含义。
无状态通信:restful 风格的设计强调无状态通信,这意味着每个请求都应该包含所有必要的信息以处理该请求,而不依赖于先前的请求。这可以使得 web 应用程序更加简单和可扩展,因为服务器不需要保留任何状态信息。
统一接口:所有资源应该通过相同的接口来访问。这意味着客户端可以使用相同的 http 方法(如 get、post、put、delete 等)来操作不同类型的资源。这使得 api 更加简单和一致,并且更容易被其他开发者理解和使用。
总之,restful 风格的设计使得 web 应用程序更加灵活、可扩展和易于维护,是一种现代化的 web 应用程序设计方式。
二、python 中的 restfulpython 可以用于实现 restful 风格的 web 应用程序,通常使用一些 web 框架来简化开发过程。下面是一些常见的 python web 框架:
flask:flask 是一个简单、轻量级的 web 框架,可以用来构建 restful 风格的 web 应用程序。它使用 python 的装饰器语法来定义 http 路由,使得编写 web 应用程序变得简单和直观。flask 还提供了扩展机制,使得开发人员可以轻松地添加新的功能,例如数据库访问、表单验证等。
django:django 是一个功能强大、全面的 web 框架,可以用于构建复杂的 web 应用程序。它提供了许多内置功能,例如 orm(对象关系映射)、表单验证、身份验证等,可以使开发人员更快地构建 web 应用程序。django 也支持 restful 风格的 web 应用程序开发,可以使用第三方库 django rest framework 来实现。
bottle:bottle 是一个轻量级的 web 框架,它使用 python 的装饰器语法来定义 http 路由,可以快速构建 restful 风格的 web 应用程序。bottle 还包含了一些有用的功能,例如模板引擎、数据库访问、静态文件处理等。
pyramid:pyramid 是一个灵活、高度可定制的 web 框架,可以用于构建各种类型的 web 应用程序,包括 restful 风格的 web 应用程序。pyramid 提供了许多扩展机制,使得开发人员可以轻松地添加新的功能,例如数据库访问、表单验证、身份验证等。
以上框架都支持 restful 风格的 web 应用程序开发,并且都具有各自的优缺点,开发人员可以根据自己的需求选择合适的框架。
三、flask restful api 示例讲解1)flask-restful 库讲解flask-restful 是一个基于 flask 的扩展库,它提供了一些方便的工具来构建 restful api。下面是 flask-restful 的一些主要特点和功能:
资源类:flask-restful 提供了一个 resource 基类,可以用来创建资源。resource 类包含了 http 方法(get、post、put、delete 等)的处理逻辑,并提供了一些方便的方法来处理请求和响应。
请求参数解析:flask-restful 提供了一个 requestparser 类,用于解析请求参数。requestparser 可以自动将查询参数、表单参数、json 参数等解析成 python 类型,并提供了一些选项来指定参数的类型、默认值、必须存在等限制条件。
响应格式化:flask-restful 提供了一个 marshal_with() 装饰器,用于格式化响应数据。marshal_with() 装饰器可以将 python 对象转换成指定的输出格式(如 json、xml 等),并支持指定输出字段、字段类型、嵌套字段等功能。
路由定义: flask-restful 提供了一个 api 类,用于定义路由和资源的映射关系。api 类包含了 add_resource()方法,用于将资源类和 url 路由绑定起来。
异常处理:flask-restful 提供了一些异常类,用于处理 http 请求和响应中的错误。flask-restful 的异常类包括 abort、httpexception 等,可以方便地处理 http 状态码、错误信息等。
综上所述,flask-restful 提供了一些方便的工具来简化 restful api 的开发。使用 flask-restful 可以快速地定义资源、解析请求参数、格式化响应数据、定义路由和处理异常等,从而提高开发效率并降低出错的风险。
2)flask-restful 库安装要安装 flask-restful 库,可以使用 pip 命令进行安装。在终端中执行以下命令:
pip3 install flask-restful
这将会从 pypi 上下载 flask-restful 库,并安装到本地的 python 环境中。安装完成后,就可以在代码中导入 flask_restful 模块,使用 flask-restful 提供的功能来构建 restful api。
3)restful 示例讲解下面是一个简单的 flask restful api 示例,它实现了一个简单的 to-do list 应用程序:
from flask import flask, requestfrom flask_restful import api, resource, reqparse, fields, marshal_withapp = flask(__name__)api = api(app)todos = {}todo_fields = { 'id': fields.integer, 'task': fields.string, 'status': fields.boolean}class todolist(resource): @marshal_with(todo_fields) def get(self): return todos @marshal_with(todo_fields) def post(self): parser = reqparse.requestparser() parser.add_argument('task', type=str, help='task is required', required=true) args = parser.parse_args() todo_id = len(todos) + 1 todo = {'task': args['task'], 'status': false} todos[todo_id] = todo return todo, 201class todoitem(resource): @marshal_with(todo_fields) def get(self, todo_id): return todos[todo_id] def put(self, todo_id): parser = reqparse.requestparser() parser.add_argument('task', type=str) parser.add_argument('status', type=bool) args = parser.parse_args() todo = todos[todo_id] if args['task']: todo['task'] = args['task'] if args['status']: todo['status'] = args['status'] return todo def delete(self, todo_id): del todos[todo_id] return '', 204api.add_resource(todolist, '/todos')api.add_resource(todoitem, '/todos/<int:todo_id>')if __name__ == '__main__': app.run(debug=true)
启动
# 配置环境变量export flask_app=restful-test.py# 启动服务,公开访问需要加上--host=0.0.0.0python -m flask run --host=0.0.0.0
该示例使用 flask 和 flask-restful 库来实现 to-do list 应用程序的 restful api。下面是一些重要的代码片段的解释:
定义资源:在示例中,有两个资源:todolist 和 todoitem。todolist 用于处理所有的 to-do 任务列表,todoitem 用于处理单个任务。
定义请求参数:在示例中,我们使用 flask-restful 库的 requestparser 来解析请求参数。我们定义了 'task' 和 'status' 参数,并使用 add_argument() 方法来指定它们的类型和其他限制条件。
定义响应格式:在示例中,我们使用 flask-restful 库的 marshal_with() 装饰器来定义响应的格式。我们定义了一个名为 todo_fields 的字典,其中包含了 to-do 任务的 id、task 和 status 字段。
定义请求方法:在示例中,我们使用 flask-restful 库的 resource 类来定义请求方法。我们实现了 get、post、put 和 delete 方法,用于获取任务列表、添加任务、更新任务和删除任务。
添加路由:在示例中,我们使用 flask-restful 库的 api 类来添加路由。我们使用 add_resource() 方法来将 todolist 和 todoitem 类与相应的 url 路由绑定起来。
在运行该示例后,可以通过访问 url 来使用 to-do list 应用程序的 restful api。例如,要获取所有任务列表,可以使用以下 url:
# get http://localhost:5000/todoscurl http://localhost:5000/todos
要添加一个新任务,可以使用以下 url:
# post http://localhost:5000/todoscurl -xpost http://localhost:5000/todos -d 'task=123'curl -xpost http://localhost:5000/todos -d '{"task":"456"}' --header "content-type: application/json"
要获取单个任务,可以使用以下 url:
# get http://localhost:5000/todos/1curl http://localhost:5000/todos/1
要更新任务,可以使用以下 url:
# put http://localhost:5000/todos/1curl -xput http://localhost:5000/todos/1 -d '{"task":"test"}' --header "content-type: application/json"# 查看curl http://localhost:5000/todos/1
要删除任务,可以使用以下 url:
# delete http://localhost:5000/todos/1curl -xdelete http://localhost:5000/todos/1
以上就是python flask restful怎么使用的详细内容。
