python和golang_Python,Ruby和Golang:Web服务应用程序比较

python和golang

python, ruby, and golang images

After a recent comparison 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 Flask (Python), Sinatra (Ruby), and Martini (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服务。 为了进行比较,我选择了Flask (Python), Sinatra (Ruby)和Martini (Golang)。 是的,每种语言的Web应用程序库还有许多其他选择,但是我觉得这三个可以很好地进行比较。

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

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

图书馆概况 (Library Overviews)

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

这是Stackshare对库的高层比较。

烧瓶(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 Django or Pyramid) is that you can start small and build up to a more complex application as needed.

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

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

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

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

Sinatra是一种DSL,用于以最少的工作量在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 Ruby on Rails for the same reason as Flask – you can start small and expand the application as needed.

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

1
1
2
2
3
3
4
4
5
5

马提尼(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 criticism 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 here. Some other frameworks include Revel, Gin, and even the built-in net/http library.

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

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11

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

1
1
2
2
3
3
4
4

查看HTML (View The HTML)

GET /

GET /

python, ruby, and golang images

查看JSON (View The JSON)

GET /json

GET /json

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10

应用结构 (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:

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

项目设置 (Project Setup)

All of the projects are bootstrapped using docker and docker-compose. 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

使用dockerdocker-compose引导所有项目 。 在深入探讨如何在后台启动每个应用程序之前,我们可以使用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)

1
1
2
2
3
3
4
4
5
5
6
6

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 pip to install our application requirements specified in requirements.txt.

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

Ruby(sinatra) (Ruby (sinatra))

1
1
2
2
3
3
4
4
5
5
6
6

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 bundler to install our application requirements specified in the Gemfile.

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

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

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

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)

1
1
2
2
3
3
4
4
5
5
6
6
7
7
1
1

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

1
1
2
2
1
1

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

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
1
1

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

Python(烧瓶) (Python (Flask))

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

露比(Sinatra) (Ruby (Sinatra))

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

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

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17

呈现JSON响应 (Render A JSON Response)

Python(烧瓶) (Python (Flask))

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

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

1
1
2
2

露比(Sinatra) (Ruby (Sinatra))

1
1
2
2
3
3

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

1
1

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

Python(烧瓶) (Python (Flask))

1
1
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14

露比(Sinatra) (Ruby (Sinatra))

1
1
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14

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

1
1
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14

数据库连接 (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))

1
1
2
2
3
3

露比(Sinatra) (Ruby (Sinatra))

1
1
2
2
3
3

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

1
1
2
2
3
3
4
4

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

Python(烧瓶) (Python (Flask))

1
1
2
2
3
3
4
4
5
5
6
6
7
7

露比(Sinatra) (Ruby (Sinatra))

1
1

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

1
1

检索数据 (Retrieve Data)

Python(烧瓶) (Python (Flask))

1
1

露比(Sinatra) (Ruby (Sinatra))

1
1

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

1
1
2
2

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

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

部署所有这些应用程序的绝佳解决方案是使用dockerdocker-compose

Python(烧瓶) (Python (Flask))

Dockerfile

Docker文件

1
1
2
2
3
3
4
4
5
5
6
6

docker-compose.yml

docker-compose.yml

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12

露比(Sinatra) (Ruby (Sinatra))

Dockerfile

Docker文件

1
1
2
2
3
3
4
4
5
5
6
6

docker-compose.yml

docker-compose.yml

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12

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

Dockerfile

Docker文件

1
1
2
2
3
3
4
4
5
5
6
6

docker-compose.yml

docker-compose.yml

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12

结论 (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中“魔术”的一个具体示例:

1
1

And the equivalent Flask code:

和等效的Flask代码:

1
1
2
2
3
3
4
4
5
5
6
6

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 Padrino 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是此类别中的获胜者。 如果您除了基本服务之外还需要其他任何东西(即使Padrino在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可能是最好的选择,因为它很容易启动和运行,并且几乎没有默认的“魔术”。 我的建议是,在为项目选择库时,您可以灵活地做出决定。



翻译自: https://www.pybloggers.com/2015/07/python-ruby-and-golang-a-web-service-application-comparison/

python和golang

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值