Skip to content

docs: translate some topic of cookbook to Chinese #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions cookbook/cgi-apache.zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
---
layout: default
title: CGI deployment on Apache
title: 在 Apache 上部署 CGI
---

# CGI deployment on Apache
# 在 Apache 上部署 CGI

Here are the simple steps needed to create and run an web.py application.
以下是创建并运行 web.py 应用程序的简单步骤:

* Install web.py and flups
* 确保已安装 web.py 和 flup。
* ​创建应用程序

按文档创建应用程序,主代码结构如下:

* Create the application as documented

if __name__ == "__main__":
web.run(urls, globals())

For our example, let it be named `app.py`, located in `/www/app` and we need it accessible as `http://server/app/app.py`.

* Configure Apache (version 2.2 in this example)
本示例中,我们将应用命名为 `app.py`,存放在 `/www/app` 目录下,并通过 `http://server/app/app.py` 访问。


* ​配置 Apache(以 Apache 2.2 为例)​

ScriptAlias /app "/www/app/"
<Directory "/www/app/">
Expand All @@ -25,9 +29,9 @@ For our example, let it be named `app.py`, located in `/www/app` and we need it
Allow from all
</Directory>

That's it. Your application is accessible via `http://server/app/app.py/`. Additional URLs handled by the application are added to the end of the URL, for examples `http://server/app/app.py/myurl`.
完成后,即可通过 `http://server/app/app.py/` 访问应用。应用处理的其他 URL 会附加在路径后,例如:`http://server/app/app.py/myurl`

* `.htaccess` configuration
* `.htaccess` 配置

Options +ExecCGI
AddHandler cgi-script .py
Expand All @@ -42,4 +46,7 @@ That's it. Your application is accessible via `http://server/app/app.py/`. Addit
RewriteRule ^(.*)$ index.py/$1 [PT]
</IfModule>

Here it is assumed that your application is called index.py. The above htaccess checks if some static file/directory exists failing which it routes the data to your index.py. Change the Rewrite Base to a sub-directory if needed.

此配置假设应用名为 `index.py`。当请求的静态文件或目录不存在时,请求会被路由到 `index.py`。若需调整路径,可修改 `RewriteBase` 为子目录。


79 changes: 54 additions & 25 deletions cookbook/fastcgi-apache.zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
---
layout: default
title: Web.py using FastCGI and Apache 2
title: 使用 FastCGI Apache 2 部署 Web.py
---

# Web.py using FastCGI and Apache 2
# 使用 FastCGI 和 Apache 2 部署 Web.py

# 环境要求

#Requirements
* Apache 2.x
* [mod_fcgid](http://fastcgi.coremail.cn/)
* [mod_rewrite](http://httpd.apache.org/docs/2.0/rewrite/)
* [Flup](http://trac.saddi.com/flup)

Note, on CentOS compiling mod_fcgid requires apache-devel be installed (available via yum).
#Apache Configuration
Replace '/var/www/myapp/' with the path to your apps directory

注意:在 CentOS 系统上编译 mod_fcgid 需要安装 apache-devel 包(可通过 yum 安装)。


# Apache 配置


将以下配置中的 '/var/www/myapp/' 替换为你的应用目录路径


LoadModule rewrite_module modules/mod_rewrite.so
LoadModule fcgid_module modules/mod_fcgid.so
Expand Down Expand Up @@ -48,9 +55,15 @@ Replace '/var/www/myapp/' with the path to your apps directory



#Hello World
Note the following line is required:
# Hello World 示例


注意,必须包含以下代码行:

```
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
```


#!/usr/bin/python

Expand All @@ -68,40 +81,56 @@ web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()


#Run
1. Start your server.
1. Open your application with your browser
1. To confirm your application is running try:
# 运行步骤

1. 启动你的服务端。
2. 通过浏览器访问应用。
3. 确认你的程序正在运行:


<code>
ps aux | grep code.py
</code>

#Troubleshooting
# 故障排查

<br>
###Check your apache error log for information!

首要检查 Apache 错误日志为主!

<br>
##Common problems
## 常见问题
<br>

###File permissions.
You might see error code 255 in your logs.
Ensure the directory is readable and that code. py is executable:
### 文件权限问题


日志中可能出现 255 错误码。
确保目录可读且 code.py 有执行权限:

<code>
chmod +x code.py
</code>

###404 Not Found.
Is your Alias path correct in your apache configuration?
### 404 Not Found 页面

检查 Apache 配置中的 Alias 路径是否正确?


### 其他问题

###Other problems
Web.py spawns http://0.0.0.0:8080, dies unexpectedly, or returns nothing.
Did you add this line?
<pre>

Web.py 意外退出、无响应或无法启动监听到 http://0.0.0.0:8080 。
检查是否添加了以下关键代码?


<code>
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
</pre>
#Misc
After updating your application you may need to restart your web server to see the changes.
</code>

# 补充说明

更新应用后可能需要重启 Web 服务器才能使修改生效。

10 changes: 5 additions & 5 deletions cookbook/index.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ title: Web.py Cookbook 简体中文版

## Testing 测试

* [Testing with Paste and Nose (未译)](testing_with_paste_and_nose.zh-cn)
* [RESTful doctesting using an application's request method (未译)](restful_doctesting_using_request.zh-cn)
* [使用Paste和Nose进行测试](testing_with_paste_and_nose.zh-cn)
* [使用 app.request 进行 RESTful 文档测试](restful_doctesting_using_request.zh-cn)

## User input 用户输入

Expand Down Expand Up @@ -101,11 +101,11 @@ title: Web.py Cookbook 简体中文版

* [通过Fastcgi和lighttpd部署](fastcgi-lighttpd.zh-cn)
* [通过Webpy和Nginx with FastCGI搭建Web.py](fastcgi-nginx.zh-cn)
* [CGI deployment through Apache (未译)](cgi-apache.zh-cn)
* [CGI deployment through Apache](cgi-apache.zh-cn)
* mod_python deployment through Apache (requested)
* [通过Apache和mod_wsgi部署](mod_wsgi-apache.zh-cn)
* [mod_wsgi deployment through Nginx (未译)](mod_wsgi-nginx.zh-cn)
* [Fastcgi deployment through Nginx (未译)](fastcgi-nginx.zh-cn)
* [mod_wsgi deployment through Nginx](mod_wsgi-nginx.zh-cn)
* [Fastcgi deployment through Nginx](fastcgi-nginx.zh-cn)

## Subdomains 子域名

Expand Down
10 changes: 8 additions & 2 deletions cookbook/restful_doctesting_using_request.zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
---
layout: default
title: RESTful doctesting using app.request
title: 使用 app.request 进行 RESTful 文档测试
---

# RESTful doctesting using app.request
# 使用 app.request 进行 RESTful 文档测试


`app.request` 是 ​web.py 框架的测试工具方法,用于在 **​不启动服务器** 的情况下模拟 HTTP 请求,主要用于 **​单元测试** 或 **​文档测试**。

`app.request` 可以​模拟请求直接向 web.py 应用发送 HTTP 请求(如 GET/POST)。可以返回包含状态码、响应头和响应内容的 web.Storage 对象。


#!/usr/bin/env python

Expand Down
57 changes: 38 additions & 19 deletions cookbook/testing_with_paste_and_nose.zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
layout: default
title: Testing with Paste and Nose
title: 使用Paste和Nose进行测试
---

# Testing with Paste and Nose
# 使用Paste和Nose进行测试

## Problem
## 问题描述

You want to test your web.py application.
你需要对 web.py 应用进行自动化测试。

## Solution
## 解决方案

from paste.fixture import TestApp
from nose.tools import *
Expand All @@ -23,47 +23,66 @@ You want to test your web.py application.
assert_equal(r.status, 200)
r.mustcontain('Hello, world!')

## Background
## 技术背景

This example makes use of the Paste and Nose libraries. [Paste](http://pythonpaste.org/) lets you throw test requests at your application, and adds some helpful [custom methods to the response objects](http://pythonpaste.org/webtest/#the-response-object), such as mustcontain(), seen above. [Nose](http://somethingaboutorange.com/mrl/projects/nose/) makes writing and running your tests dead simple. When run from the base of your tree, it automatically finds and runs anything which is named like a test, adding necessary modules to your PYTHONPATH. This gives you the flexibility to run your tests from other directories, as well. Another benefit of Nose is that you no longer need to have every test class inherit from unittest.TestCase. Many more details are outlined on the project page.
本方案使用两个关键库`Paste`和`Nose`协助你完成测试:

## Explanation
[Paste](http://pythonpaste.org/) :提供模拟请求功能,并为响应对象添加[实用方法](http://pythonpaste.org/webtest/#the-response-object)方便你去使用。(如示例中的 `mustcontain()`)

[Nose](http://somethingaboutorange.com/mrl/projects/nose/) :可以帮助你简化测试编写和运行代码,自动发现并执行测试文件,支持灵活目录结构运行
当你在你的项目目录下执行测试时,该工具可以自动识别并且运行所有符合测试命名规范的文件​(例如 `test_*.py`),同时也会动态的将必要模块添加到 `PYTHONPATH`。即使从其他子目录运行测试命令(如 subdir/nosetests),Nose 仍能正确解析路径并执行测试。另一个好处是,测试类无需强制继承 `unittest.TestCase`,减少冗余代码,提升代码可读性。


## 代码解析


该测试代码位于 test_code.py 文件中,项目目录结构如下:

This code resides in a file called test_code.py. The directory layout of the application looks like this:

./
code.py
./test
test_code.py

Most of the code example above should be fairly self-explanatory. From our main module, code, we import app, which is defined in the usual way:
大部分代码示例应该是容易理解的。从主模块 code 导入已定义的 web.py 应用对象:

app = web.application(urls, globals())

To set up the test, we pass its wsgifunc() to Paste's TestApp, as you have already seen in the example.
在设置测试用例的初始化环节,我们将 `wsgifunc()` 传递给 Paste 的 TestApp,如下面代码所示:

app = TestApp(app.wsgifunc(*middleware))

assert_equal() is one of the methods provided by nose's utils, and works just like unittest's assertEqual().

## Setting Up the Test Environment
`assert_equal()` 断言是 Nose 提供的工具方法,功能等同于 unittest 的 assertEqual():

In order to avoid kicking off web.py's webserver when we run our tests, a change is required to the line which calls run(). It normally looks something like this:
assert_equal(response.status, 200) # 验证状态码是否为 200

if __name__ == "__main__": app.run()
## 测试环境配置

为避免运行测试时自动启动 web.py 的 Web 服务器,需修改调用 app.run() 的代码。原始代码通常如下:

if __name__ == "__main__":
app.run()

我们可以通过读取环境变量的方式去区分测试还是实际部署运行,运行测试时,设置环境变量 `WEBPY_ENV=test` 标识测试环境。
然后,在程序启动阶段,添加环境检测函数 `is_test()`,整体代码将变成如下形式:

We can define an environment variable, such as WEBPY_ENV=test, when we run our tests. In that case, the above line becomes the following:

import os

def is_test():
if 'WEBPY_ENV' in os.environ:
return os.environ['WEBPY_ENV'] == 'test'

if (not is_test()) and __name__ == "__main__": app.run()
if (not is_test()) and __name__ == "__main__":
app.run()

Then, it's simply a matter of running nosetests like so:
如此一来,启动测试时附加环境变量即可:

WEBPY_ENV=test nosetests

The is_test() function comes in handy for other things, such as doing conditional database commits to avoid test database pollution.

`is_test()` 函数还可用于其他场景,例如,测试环境下跳过数据库提交,避免污染测试数据:

if not is_test():
db.commit()