Werkzeug 到底是什么?

What exactly is Werkzeug?

来自official documentation

Werkzeug is a WSGI utility library for Python.

但是,当我 运行 我的 Flask 网络应用程序时,我注意到来自服务器的响应 header 包含:

HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 13
Server: Werkzeug/0.11.9 Python/2.7.10
Date: Tue, 03 May 2016 12:50:08 GMT

服务器在第四行提到了 Werkzeug,但 Werkzeug 到底是什么,它是像 Apache 这样的网络服务器吗?

不,它不是像 Apache 这样的 Web 服务器。这是一个 CGI 库。由于 Apache(或您的 Flask 应用程序)可能正在使用该库来处理某些 HTTP 请求,因此它可能会将 header 添加到响应中。

因为不是。

在您的设置中,您最有可能使用 "development server"(run_simple 函数)进行测试。 所以在这个用例中就像一个(非常)穷人的Apache,但只是在某种意义上它能够正确回答 HTTP 请求。

如果你查看文档 http://werkzeug.pocoo.org/docs/serving/ ,您会看到以下注释:

The development server is not intended to be used on production systems. It was designed especially for development purposes and performs poorly under high load. For deployment setups have a look at the Application Deployment pages.

不,不是

Werkzeug(WSGI library) is like a communicator between your python code and http nginx/apache server

这是 Werkzeug WSGI 的完整用例:

WSGI has two sides: the "server" or "gateway" side (often a web server such as Apache or Nginx), and the "application" or "framework" side (the Python script itself). To process a WSGI request, the server side executes the application and provides environment information and a callback function to the application side. The application processes the request, returning the response to the server side using the callback function it was provided.

Between the server and the application, there may be a WSGI middleware, which implements both sides of the API. The server receives a request from a client and forwards it to the middleware. After processing, it sends a request to the application. The application's response is forwarded by the middleware to the server and ultimately to the client. There may be multiple middlewares forming a stack of WSGI-compliant applications.

希望对您有所帮助

Werkzeug 主要是一个库,而不是 Web 服务器,尽管它确实提供了一个用于开发目的的简单 Web 服务器。该开发服务器提供 Server: header.

更详细:

首先,让我们谈谈WSGI。有很多 Web 服务器,比如 Apache、Nginx、Lighttpd 等。还有很多用 Python 编写的 Web 框架,例如Django、Flask、Tornado、Pyramid 等。如果这些都可以互操作,那将非常方便。这就是 WSGI 的用武之地。这个想法是这样的:

  • 响应客户端的 HTTP 请求涉及两方面:网络服务器网络应用程序 .服务器处理复杂的网络连接、接收请求和发送响应。应用程序获取请求数据,对其进行操作,并制作响应以供服务器发回。

  • 如果您想编写一个 Python 网络应用程序,请确保它有一个可调用的 object(例如一个函数),它接受 HTTP [=73] 的某些参数=]s,输入表单数据,环境变量等

  • 如果您想编写一个服务于 Python 应用程序的 Web 服务器,请让它在每次收到 HTTP 请求时从应用程序调用可调用 object。

  • WSGI 规范(在 PEP 3333 中)明确指定了可调用的参数必须是什么以及 return 值应该是什么,因此每个服务器都知道如何交谈每个应用程序,反之亦然。

因此,我们知道每个 Web 应用程序都需要提供此可调用对象并能够处理它接收到的特定参数。 每个应用程序都需要这样做... 这听起来是一个使用库的好机会。 Werkzeug 就是这个库。

Werkzeug 提供了一组用于开发 WSGI-compliant 应用程序的实用程序。这些实用程序执行的操作包括解析 header、发送和接收 cookie、提供对表单数据的访问、生成重定向、在出现异常时生成错误页面,甚至在浏览器中提供 运行 的交互式调试器.它真的很全面。 Flask 然后在此基础上构建(以及 Jinja、Click 等)以提供完整的 Web 框架。

那么,如果 Werkzeug 是 应用程序 的库,为什么它会出现在服务器 header 中?

Werkzeug 也有一个服务器角色模块。这纯粹是为了方便。

安装和配置像 Apache 或 Nginx 这样的 full-fledged 网络服务器需要付出很多努力,而且几乎可以肯定,仅仅为了在您自己的开发箱上测试您的应用程序就有点过分了。出于这个原因,Werkzeug 提供了一个开发服务器:一个简单的 Web 服务器,您可以 运行 使用单个命令并且几乎不需要配置。当您执行 flask run(或 werkzeug.serving.run_simple())时,您将获得此开发服务器。开发服务器的 Server: header - 你猜对了 - Werkzeug/<version> Python/<version>.

此服务器不适用于生产用途。至少,根据文档,它不能很好地扩展。但如果还有其他问题,例如安全性,我也不会感到惊讶。

Flask python 使用 werkzeug 作为 Web 服务器进行测试