博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python和golang_Python,Ruby和Golang:Web服务应用程序比较
阅读量:2519 次
发布时间:2019-05-11

本文共 11652 字,大约阅读时间需要 38 分钟。

python和golang

python, ruby, and golang images

After a of Python, Ruby, and Golang for a command-line application I decided to use the same pattern to compare building a simple web service. I have selected (Python), (Ruby), and (Golang) for this comparison. Yes, there are many other options for web application libraries in each language but I felt these three lend well to comparison.

在对命令行应用程序的Python,Ruby和Golang进行之后,我决定使用相同的模式来比较构建简单的Web服务。 为了进行比较,我选择了 (Python), (Ruby)和 (Golang)。 是的,每种语言的Web应用程序库还有许多其他选择,但是我觉得这三个可以很好地进行比较。

This is a guest blog post by , a software engineer from Boulder.

这是一个客人博客文章由 ,从博尔德一名软件工程师。

图书馆概况 (Library Overviews)

Here is a high-level comparison of the libraries by .

这是对库的高层比较。

烧瓶(Python) (Flask (Python))

Flask is a micro-framework for Python based on Werkzeug, Jinja2 and good intentions.

Flask是基于Werkzeug,Jinja2和良好意图的Python微型框架。

For very simple applications, such as the one shown in this demo, Flask is a great choice. The basic Flask application is only 7 lines of code (LOC) in a single Python source file. The draw of Flask over other Python web libraries (such as or ) is that you can start small and build up to a more complex application as needed.

对于非常简单的应用程序,例如本演示中显示的应用程序,Flask是一个不错的选择。 基本的Flask应用程序在一个Python源文件中只有7行代码(LOC)。 Flask与其他Python Web库(例如或 )相比,其优势在于您可以从小规模开始,并根据需要构建更复杂的应用程序。

112233445566778899

锡纳特拉(Ruby) (Sinatra (Ruby))

Sinatra is a for quickly creating web applications in Ruby with minimal effort.

Sinatra是一种用于以最少的工作量在Ruby中快速创建Web应用程序。

Just like Flask, Sinatra is great for simple applications. The basic Sinatra application is only 4 LOC in a single Ruby source file. Sinatra is used instead of libraries such as for the same reason as Flask – you can start small and expand the application as needed.

就像Flask一样,Sinatra非常适合简单的应用程序。 基本的Sinatra应用程序在单个Ruby源文件中只有4个LOC。 出于与Flask相同的原因,使用Sinatra代替了诸如之类的库–您可以从小处着手,并根据需要扩展应用程序。

1122334455

马提尼(Golang) (Martini (Golang))

Martini is a powerful package for quickly writing modular web applications/services in Golang.

Martini是一个功能强大的软件包,用于在Golang中快速编写模块化的Web应用程序/服务。

Martini comes with a few more batteries included than both Sinatra and Flask but is still very lightweight to start with – only 9 LOC for the basic application. Martini has come under some by the Golang community but still has one of the highest rated Github projects of any Golang web framework. The author of Martini responded directly to the criticism . Some other frameworks include , , and even the built-in library.

马提尼酒比西纳特拉酒瓶和Flask酒瓶附带的电池要多一些,但一开始仍然非常轻巧-基本应用只有9 LOC。 Martini受到Golang社区的 ,但仍然是所有Golang Web框架中评分最高的Github项目之一。 马提尼酒的作者在直接回应了批评。 其他一些框架包括 , 甚至内置的库。

11223344556677889910101111

With the basics out of the way, let’s build an app!

借助基础知识,让我们构建一个应用程序!

服务说明 (Service Description)

The service created provides a very basic blog application. The following routes are constructed:

创建的服务提供了一个非常基本的博客应用程序。 构建了以下路线:

  • GET /: Return the blog (using a template to render).
  • GET /json: Return the blog content in JSON format.
  • POST /new: Add a new post (title, summary, content) to the blog.
  • GET / :返回博客(使用模板进行渲染)。
  • GET /json :以JSON格式返回博客内容。
  • POST /new :向博客添加新帖子(标题,摘要,内容)。

The external interface to the blog service is exactly the same for each language. For simplicity MongoDB will be used as the data store for this example as it is the simplest to set up and we don’t need to worry about schemas at all. In a normal “blog-like” application a relational database would likely be necessary.

每种语言的博客服务外部接口完全相同。 为简单起见,MongoDB将用作此示例的数据存储,因为它是最简单的设置,我们完全不需要担心架构。 在正常的“类博客”应用程序中,可能需要关系数据库。

添加帖子 (Add A Post)

POST /new

POST /new

11223344

查看HTML (View The HTML)

GET /

GET /

python, ruby, and golang images

查看JSON (View The JSON)

GET /json

GET /json

1122334455667788991010

应用结构 (Application Structure)

Each application can be broken down into the following components:

每个应用程序可以细分为以下组件:

应用程式设定 (Application Setup)

  • Initialize an application
  • Run the application
  • 初始化应用程序
  • 运行应用程序

请求 (Request)

  • Define routes on which a user can request data (GET)
  • Define routes on which a user can submit data (POST)
  • 定义用户可以请求数据(GET)的路由
  • 定义用户可以提交数据(POST)的路由

响应 (Response)

  • Render JSON (GET /json)
  • Render a template (GET /)
  • 渲染JSON( GET /json
  • 渲染模板( GET /

数据库 (Database)

  • Initialize a connection
  • Insert data
  • Retrieve data
  • 初始化连接
  • 插入资料
  • 检索数据

应用部署 (Application Deployment)

  • Docker!
  • 码头工人!


The rest of this article will compare each of these components for each library. The purpose is not to suggest that one of these libraries is better than the other – it is to provide a specific comparison between the three tools:

本文的其余部分将比较每个库的每个组件。 目的并不是要建议这些库中的一个优于另一个,而是要提供这三个工具之间的特定比较:

  • (Python)
  • (Ruby)
  • (Golang)
  • (Python)
  • (Ruby)
  • (Golang)

项目设置 (Project Setup)

All of the are bootstrapped using and . Before diving into how each application is bootstrapped under the hood we can just use docker to get each up and running in exactly the same way – docker-compose up

使用和引导所有 。 在深入探讨如何在后台启动每个应用程序之前,我们可以使用docker以完全相同的方式启动和运行每个应用程序– docker-compose up

Seriously, that’s it! Now for each application there is a Dockerfile and a docker-compose.yml file that specify what happens when you run the above command.

说真的,就是这样! 现在,对于每个应用程序,都有一个DockerfileDockerfile docker-compose.yml文件,它们指定运行上述命令时发生的情况。

Python(烧瓶)– Dockerfile (Python (flask) – Dockerfile)

112233445566

This Dockerfile says that we are starting from a base image with Python 3.4 installed, adding our application to the /app directory and using to install our application requirements specified in requirements.txt.

Dockerfile表示,我们从安装了Python 3.4的基础映像开始,将我们的应用程序添加到/app目录中,并使用来安装requirements.txt中指定的应用程序requirements.txt

Ruby(sinatra) (Ruby (sinatra))

112233445566

This Dockerfile says that we are starting from a base image with Ruby 2.2 installed, adding our application to the /app directory and using to install our application requirements specified in the Gemfile.

这个Dockerfile表示,我们从安装了Ruby 2.2的基本映像开始,将我们的应用程序添加到/app目录,并使用安装我们在Gemfile指定的应用程序需求。

Golang(马提尼酒) (Golang (martini))

112233445566778899

This Dockerfile says that we are starting from a base image with Golang 1.3 installed, adding our application to the /go/src/github.com/kpurdon/go-blog directory and getting all of our necessary dependencies using the go get command.

这个Dockerfile表示我们从安装Golang 1.3的基础映像开始,将我们的应用程序添加到/go/src/github.com/kpurdon/go-blog目录中,并使用go get命令获取所有必要的依赖项。

初始化/运行应用程序 (Initialize/Run An Application)

Python(烧瓶)– app.py (Python (Flask) – app.py)

11223344556677
11

Ruby(Sinatra)– app.rb (Ruby (Sinatra) – app.rb)

1122
11

Golang(马提尼酒)– app.go (Golang (Martini) – app.go)

11223344556677889910101111121213131414
11

定义路线(GET / POST) (Define A Route (GET/POST))

Python(烧瓶) (Python (Flask))

112233445566778899

露比(Sinatra) (Ruby (Sinatra))

112233445566778899

Golang(马提尼酒) (Golang (Martini))

11223344556677889910101111121213131414151516161717

呈现JSON响应 (Render A JSON Response)

Python(烧瓶) (Python (Flask))

Flask provides a method but since the service is using MongoDB the mongodb bson utility is used.

Flask提供了方法,但是由于该服务使用的是MongoDB,因此使用了mongodb bson实用程序。

1122

露比(Sinatra) (Ruby (Sinatra))

112233

Golang(马提尼酒) (Golang (Martini))

11

渲染HTML响应(模板化) (Render An HTML Response (Templating))

Python(烧瓶) (Python (Flask))

11
11223344556677889910101111121213131414

露比(Sinatra) (Ruby (Sinatra))

11
11223344556677889910101111121213131414

Golang(马提尼酒) (Golang (Martini))

11
11223344556677889910101111121213131414

数据库连接 (Database Connection)

All of the applications are using the mongodb driver specific to the language. The environment variable DB_PORT_27017_TCP_ADDR is the IP of a linked docker container (the database ip).

所有应用程序都使用特定于该语言的mongodb驱动程序。 环境变量DB_PORT_27017_TCP_ADDR是链接的DB_PORT_27017_TCP_ADDR容器的IP(数据库ip)。

Python(烧瓶) (Python (Flask))

112233

露比(Sinatra) (Ruby (Sinatra))

112233

Golang(马提尼酒) (Golang (Martini))

11223344

从POST插入数据 (Insert Data From a POST)

Python(烧瓶) (Python (Flask))

11223344556677

露比(Sinatra) (Ruby (Sinatra))

11

Golang(马提尼酒) (Golang (Martini))

11

检索数据 (Retrieve Data)

Python(烧瓶) (Python (Flask))

11

露比(Sinatra) (Ruby (Sinatra))

11

Golang(马提尼酒) (Golang (Martini))

1122

应用程序部署(泊坞窗!) (Application Deployment (docker!))

A great solution to deploying all of these applications is to use and .

部署所有这些应用程序的绝佳解决方案是使用和 。

Python(烧瓶) (Python (Flask))

Dockerfile

Docker文件

112233445566

docker-compose.yml

docker-compose.yml

112233445566778899101011111212

露比(Sinatra) (Ruby (Sinatra))

Dockerfile

Docker文件

112233445566

docker-compose.yml

docker-compose.yml

112233445566778899101011111212

Golang(马提尼酒) (Golang (Martini))

Dockerfile

Docker文件

112233445566

docker-compose.yml

docker-compose.yml

112233445566778899101011111212

结论 (Conclusion)

To conclude lets take a look at what I believe are a few categories where the presented libraries separate themselves from each other.

最后,让我们看一下我认为所提供的库彼此分开的几个类别。

简单 (Simplicity)

While Flask is very lightweight and reads clearly, the Sinatra app is the simplest of the three at 23 LOC (compared to 46 for Flask and 42 for Martini). For these reasons Sinatra is the winner in this category. It should be noted however that Sinatra’s simplicity is due to more default “magic” – e.g., implicit work that happens behind the scenes. For new users this can often lead to confusion.

虽然Flask非常轻巧,而且读起来很清晰,但Sinatra应用是这三个应用中最简单的,它的LOC为23(Flask为46,马丁尼为42)。 由于这些原因,Sinatra是该类别的赢家。 但是,应该注意的是Sinatra的简单性是由于更多默认的“魔术”所致–例如,幕后发生的隐性工作。 对于新用户,这通常会导致混乱。

Here is a specific example of “magic” in Sinatra:

这是Sinatra中“魔术”的一个具体示例:

11

And the equivalent Flask code:

和等效的Flask代码:

112233445566

For beginners to programming Flask and Sinatra are certainly simpler, but for an experienced programmer with time spent in other statically typed languages Martini does provide a fairly simplistic interface.

对于初学者来说,Flask和Sinatra肯定更简单,但是对于有经验的程序员,他们花时间在其他静态类型的语言上,Martini确实提供了一个相当简单的界面。

文献资料 (Documentation)

The Flask documentation was the simplest to search and most approachable. While Sinatra and Martini are both well documented, the documentation itself was not as approachable. For this reason Flask is the winner in this category.

Flask文档是最容易搜索且最易于访问的文档。 尽管Sinatra和Martini都有很好的文档,但是文档本身并不那么容易获得。 因此,Flask是该类别的赢家。

社区 (Community)

Flask is the winner hands down in this category. The Ruby community is more often than not dogmatic about Rails being the only good choice if you need anything more than a basic service (even though offers this on top of Sinatra). The Golang community is still nowhere near a consensus on one (or even a few) web frameworks, which is to be expected as the language itself is so young. Python however has embraced a number of approaches to web development including Django for out-of-the-box full-featured web applications and Flask, Bottle, CheryPy, and Tornado for a micro-framework approach.

Flask是此类别中的获胜者。 如果您除了基本服务之外还需要其他任何东西(即使在Sinatra之上提供了此功能),Ruby社区通常会坚信Rails是唯一的好选择。 Golang社区在一个(或什至几个)Web框架上仍未达成共识,这是因为它本身还很年轻。 但是,Python接受了许多Web开发方法,其中包括Django用于开箱即用的全功能Web应用程序,以及Flask,Bottle,CheryPy和Tornado用于微框架方法。

最终决定 (Final Determination)

Note that the point of this article was not to promote a single tool, rather to provide an unbiased comparison of Flask, Sinatra, and Martini. With that said, I would select Flask (Python) or Sinatra (Ruby). If you are coming from a language like C or Java perhaps the statically-typed nature of Golang may appeal to you. If you are a beginner, Flask might be the best choice as it is very easy to get up and running and there is very little default “magic”. My recommendation is that you be flexible in your decisions when selecting a library for your project.

请注意,本文的目的不是要推广单个工具,而是要对Flask,Sinatra和Martini进行公正的比较。 话虽如此,我会选择Flask(Python)或Sinatra(Ruby)。 如果您来自C或Java之类的语言,那么Golang的静态类型本质可能会吸引您。 如果您是初学者,则Flask可能是最好的选择,因为它很容易启动和运行,并且几乎没有默认的“魔术”。 我的建议是,在为项目选择库时,您可以灵活地做出决定。



翻译自:

python和golang

转载地址:http://nwhwd.baihongyu.com/

你可能感兴趣的文章
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
数据挖掘算法比赛 - 简单经验总结
查看>>
【RFID防碰撞协议/算法】二进制搜索防碰撞算法
查看>>
win7(64位)php5.5-Apache2.4-mysql5.6环境安装
查看>>
同一行内不同大小的字体垂直居中
查看>>
【翻译】(13)Prebuilts
查看>>
C++中的单例模式(转)
查看>>
使用ptrace向已运行进程中注入.so并执行相关函数(转)
查看>>
Android 9 patch 图片 (.9.png 格式图片) 的特点和制作(转)
查看>>
CentOS7系统安装 Maria Db(MYSQL)教程
查看>>
三十四.MySQL主从同步 、主从同步模式
查看>>
zk 的配额
查看>>