falcon框架

介绍 (Introduction)

RESTful APIs are a major component of any well-architected stack, and Python happens to have some brilliant frameworks for quickly composing APIs.

RESTful API是任何结构良好的堆栈的主要组成部分,而Python恰好具有一些出色的框架,可以快速组成API。

One of these frameworks is called Falcon - and it’s great! Essentially a microframework, it ships with a sizable number of advantages:

这些框架之一称为Falcon-太好了! 本质上是微框架,它具有许多优点:

  1. It’s fast. Really fast. Check out the benchmarks here.

    它很快。 真快。 在此处查看基准。

  2. HTTP Resources are defined as classes, with class methods being used for different REST operations on these resources. This helps maintaining a clean codebase.HTTP资源被定义为类,其中类方法用于这些资源上的不同REST操作。 这有助于维护干净的代码库。
  3. It’s quite extensible - check out this section on their wiki, to get a feel for it.

    它是相当可扩展的-在他们的Wiki上查看此部分 ,以了解它。

  4. It’s based on WSGI - the Pythonic standard for web apps - so it works with Python 2.6, 2.7, and 3.3+. And if you need more performance, run it using PyPy!它基于WSGI(Web应用程序的Pythonic标准),因此可与Python 2.6、2.7和3.3+一起使用。 如果您需要更高的性能,请使用PyPy来运行它!

入门 (Getting started)

First, let’s prepare our environment. Personally, it’s always great to work in virtual environments - you can use virtualenv, virtualenvwrapper or venv. Next, install Falcon using pip: pip install falcon.

首先,让我们准备环境。 就个人而言,在虚拟环境中工作总是很棒–您可以使用virtualenvvirtualenvwrappervenv 。 接下来,使用pip安装Falcon: pip install falcon

We’re going to develop a small sample API that does very basic time-zone manipulations for us. It will display the current time in UTC, as well as the corresponding epoch time. To that end, we’ll grab a nifty library called arrow: pip install arrow.

我们将开发一个小的示例API,为我们执行非常基本的时区操作。 它将以UTC显示当前时间以及相应的纪元时间。 为此,我们将获取一个名为arrow的漂亮库: pip install arrow

You can find the finished sample at https://github.com/rudimk/freecodecamp-guides-rest-api-falcon.

您可以在https://github.com/rudimk/freecodecamp-guides-rest-api-falcon中找到完成的示例。

资源资源 (Resources)

Think of a resource as an entity that your API needs to manipulate. In our case, the best resource would be a Timestamp. Our routing would typically be something like this:

将资源视为您的API需要操纵的实体。 在我们的例子中,最好的资源是Timestamp 。 我们的路由通常如下所示:

GET /timestamp

Here, GET is the HTTP verb used to call this endpoint, and /timestamp is the URL itself. Now that we’ve gotten this bit out of the way, let’s create a module!

此处, GET是用于调用此终结点的HTTP动词,而/timestamp是URL本身。 现在我们已经解决了这一点,让我们创建一个模块!

$ touch timestamp.py

$ touch timestamp.py

Time to import the Falcon library:

是时候导入Falcon库了:

import jsonimport falconimport arrow

Note we’ve also import the json package and the arrow library. Now, let’s define a class for our resource:

请注意,我们还导入了json包和arrow库。 现在,让我们为资源定义一个类:

class Timestamp(object):def on_get(self, req, resp):payload = {}payload['utc'] = arrow.utcnow().format('YYYY-MM-DD HH:mm:SS')payload['unix'] = arrow.utcnow().timestampresp.body = json.dumps(payload)resp.status = falcon.HTTP_200

Let’s go through this snippet. We’ve defined a Timestamp class, and defined a class method called on_get - this function tells Falcon that when a GET request is issued to an endpoint for this resource, run the on_get function and provide the request and response objects as parameters.

让我们看一下这段代码。 我们已经定义了一个Timestamp类,并定义了一个名为on_get的类方法-该函数告诉Falcon,当向该资源的端点发出GET请求时,运行on_get函数并将请求和响应对象作为参数提供。

After that, it’s smooth sailing - we create an empty dictionary, fill it up with the current UTC and UNIX timestamps, convert it to JSON and attach it to the response object.

之后,一切顺利-我们创建一个空字典,用当前的UTC和UNIX时间戳填充它,将其转换为JSON并将其附加到响应对象。

Pretty simple, right? But sadly, that’s not all. We now need to create a Falcon server and hook up the resource class we’ve just defined to an actual endpoint.

很简单,对吧? 但是可悲的是,这还不是全部。 现在,我们需要创建一个Falcon服务器,并将刚刚定义的资源类连接到实际的端点。

$ touch app.py

$ touch app.py

Now, add the code below:

现在,添加以下代码:

import falconfrom timestamp import Timestampapi = application = falcon.API()timestamp = Timestamp()api.add_route('/timestamp', timestamp)

Here, we’ve defined a Falcon API, and initialized an instance of the resource class we created earlier. Then, we’ve hooked up the /timestamp endpoint with the class instance - and now we’re good to go! To test this API install gunicorn(pip install gunicorn), and run gunicorn app. Use Postman or simple cURL to test this:

在这里,我们定义了Falcon API,并初始化了我们先前创建的资源类的实例。 然后,我们将/timestamp端点与类实例连接在一起-现在我们可以开始了! 要测试此API,请安装gunicorn ( pip install gunicorn ),然后运行gunicorn app 。 使用Postman或简单的cURL对此进行测试:

$ curl http://localhost:8000/timestamp
{"utc": "2017-10-20 06:03:14", "unix": 1508479437}

And that does it!

做到了!

继续 (Moving on)

Once you’ve got the hang of Falcon, composing powerful RESTful APIs that interact with databases or messaging queues is very easy. Do check out the Falcon docs, as well as PyPI for interesting Falcon modules that keep popping up.

一旦掌握了Falcon,就可以轻松构建与数据库或消息传递队列进行交互的强大RESTful API。 请检查Falcon文档以及PyPI,以获取不断弹出的有趣Falcon模块。

翻译自: https://www.freecodecamp.org/news/build-restful-apis-with-falcon/

falcon框架

falcon框架_如何使用Falcon构建RESTful API相关推荐

  1. asp编程工具_使用ASP.NET Core构建RESTful API的技术指南

    译者荐语:利用周末的时间,本人拜读了长沙.NET技术社区翻译的技术文章<微软RESTFul API指南>,打算按照步骤写一个完整的教程,后来无意中看到了这篇文章,与我要写的主题有不少相似之 ...

  2. 用 Flask 来写个轻博客 (36) — 使用 Flask-RESTful 来构建 RESTful API 之五

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 PUT 请求 DELETE 请求 测试 对一条已经存在的 posts ...

  3. 用 Flask 来写个轻博客 (35) — 使用 Flask-RESTful 来构建 RESTful API 之四

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 POST 请求 身份认证 测试 前文列表 用 Flask 来写个轻博客 ...

  4. 用 Flask 来写个轻博客 (34) — 使用 Flask-RESTful 来构建 RESTful API 之三

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 应用请求中的参数实现 API 分页 测试 前文列表 用 Flask 来 ...

  5. 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 构建 RESTful Flask API 定义资源路由 格式 ...

  6. dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3(十)之Spring MVC中使用 Swagger2 构建Restful API...

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010046908/article/details/55047193 1.Swagger2是什么? ...

  7. 使用ASP.NET Core 3.x 构建 RESTful API - 2. 什么是RESTful API

    1. 使用ASP.NET Core 3.x 构建 RESTful API - 1.准备工作 什么是REST REST一词最早是在2000年,由Roy Fielding在他的博士论文<Archit ...

  8. 如何在5分钟内通过身份验证构建RESTful API —全部从命令行(第1部分)

    by Niharika Singh 由Niharika Singh 如何在5分钟内通过身份验证构建RESTful API -全部从命令行(第1部分) (How to Build a RESTful A ...

  9. pycharm构建虚拟环境_10分钟,带你用Python构建RESTful API 服务

    1. 前言 上一篇文章,介绍了使用 Java + Spring Boot + MyBatis 构建 RESTful API 的详细步骤:很多小伙伴表示,更愿意用 Python 编写 RESTful A ...

最新文章

  1. Kafka 可视化客户端工具(Kafka Tool)的基本使用
  2. android 无法运行 项目红叉_Android Studio 项目报红叉没法运行的几种解决办法总结...
  3. 編程之美資格賽傳話遊戲
  4. 先进的NumPy数据科学
  5. 北京大学c语言试题及答案,北大网络教育(计算机专业)C程序设计作业答案
  6. tf.nn.conv2d理解(带通道的卷积图片输出案例)
  7. 与资源库同步时,我的svn报错 Previous operation has not finished; run 'cleanup' if it was interrupted...
  8. Tcpdump的用法及使用案例
  9. Android软件图标分辨率,android app icon 图标大小尺寸
  10. 中医药天池大数据竞赛——中医文献问题生成挑战(二)
  11. Windows10新版本设置卓越性能
  12. php测速,speedtest-x :一款PHP网页测速工具
  13. 私有云部署和本地化部署有什么区别?
  14. 互联网教育:玩法与困境
  15. 两个龙的linux,Linux办公一条龙之组件间的调用
  16. thinkadmin打印sql语句调试sql
  17. 使用vite安装vue3.0(不是脚手架)
  18. QT常用控件——QComboBox控件
  19. 政府民航行业双获嘉奖,永洪科技做好行业数字化转型伙伴
  20. centos7.9-kvm-ESXi相关操作

热门文章

  1. 【C++ STL学习之二】容器vector大总结
  2. mysql 修改密码演练
  3. 如何制作歌单 0202 winform
  4. 数字三角形的显示 java
  5. mysql进阶-01-视图
  6. python-对向-查看全部属性-查看全部方法
  7. MySQL中查看某数据库大小及表大小
  8. vue 项目上传到码云 解决push失败
  9. C++头文件,预处理详解
  10. Android中scrollview的scrollto方法不起作用的办法