1、创建helloworld项目
<shell-command>
   paster create --template=pylons helloworld
</shell-command>
2、项目目录结构
   -helloworld

       - helloworld
              o components
              o config
              o controllers
              o docs
              o i18n
              o lib
              o public
              o tests
              o templates
              o __init__.py
              o websetup.py
       - helloworld.egg-info
       - development.ini
       - setup.cfg
       - setup.py

3、测试
<shell-command>
   cd helloworld
   paster serve --reload development.ini
</shell-command>
这时,可以通过来访问helloworld
   paster serve development.ini 

在public目录下创建test.html文件如下:
<html>
   <body>
      Hello World!
   </body>
</html>

4、debug 可以通过配置development.ini文件修改debug=false

5、创建一个controller

   首先,创建一个最基本的hello controller
<shell-command>
   paster controller hello
</shell-command>
   该命令将在controller目录下创建hello.py,同时在tests目录下创建functional/test_hello.py,如下所示:
<shell-command>
 paster controller hello
Creating /home/guotie/py/pylons/helloworld/helloworld/controllers/hello.py
Creating /home/guotie/py/pylons/helloworld/helloworld/tests/functional/test_hello.py
</shell-command>

helloworld/controllers/hello.py文件代码如下:
from helloworld.lib.base import *

class HelloController(BaseController):
   def index(self):
      m.write('hello world')

   pylons使用强大的,灵活的route系统来配置url的route
例如,如果把映射到上,可以修改config/route.py文件,如下:
    map.connect(':controller/:action/:id')
   <strong>
    map.connect('', controller='hello', action='index')
   </strong>
    map.connect('*url', controller='template', action='view')
注意:必须把上面重点强调的语句放在最后一句之前。

6、使用模板

创建文件helloworld/templates/serverinfo.myt,如下所示:
<p>Hi, here's the server environment: <br />
   <% str(request.environ) %>
</p>

<p>
   and here's the URL you called: <% h.url_for() %>
</p>

为了是这个模板能够工作,我们需要在前面章节创建的controller hello中,添加对应的方法: def serverinfo(self):

访问http://localhost:5000/hello/serverinfo/

注意: a.上面的函数serverinfo是class HelloController中的方法,因此,必须保持缩进格式;否则,访问出错,报错为: Error(NotImplementedError): Action serverinfos is not implemented b.如果serverinfo.myt不存在,则访问页面错误为HTTP 404。

7、controllers变量,template 全局变量,pylons全局变量 从lib.base中导出的全局变量:

8、向Templates传递参数

每个request对应于一个pylons controllers,这意味着你可能需要在不同的controller中传递参数。然而,keep track所有的变量和self的方法是很不方便的,尤其是当你想把它们传递给template时。

为了建立可以在template中使用的数据,看看如何做:

hello controller的hello.py:
def serverinfo(self):
    c.value = 2
    m.subexec('/serverinfo.myt')

serverinfo.myt如下:
<p>The value of <tt>c.value</tt> is:
<% c.value %>

如果请求一个c上不存在的属性,将会返回空值而不是throw exception。 如果请求不存在的变量,则会出错。 注意:变量不能使用以下划线“_”开头的变量;

9、Application global与永久对象

有些场合,你可能需要一些信息对所有controller有效,而不是每次请求都reset。例如,数据库连接是在application load时进行,对所有的请求都有效。

g变量是里lib/app-gloables.py文件中Globals类的一个实例。任何在init()中设置的类属性都会在你的pylons应用中作为g的属性有效。在任一request中对g的属性的修改将影响其他request。你必须非常小心设置request中的全局变量。

例如,在lib/app_globals.py文件中Globals类中,增加变量message,如下:
def __init__(self, defaults, app, **extra):
    self.message = 'Hello'

在hellworld/controllers/hello.py中的HelloController增加app_globals_test方法如下:
    def app_globals_test(self):

        if g.message == 'Hello':
            m.write(g.message)
            g.message = 'Hello World!'
        else:
            m.write(g.message)
            g.message =   'Hello'

================================================================================

1、应用部署

所有pylons应用的发布都是使用.egg格式完成。egg文件是一个可执行的python包。
进入项目的根目录,执行下面的命令:
<shell-command>
python setup.py bdist_egg
</shell-command>

如果一切顺利的话,将会在dist目录下创建egg文件。 网管想安装pylons应用时,只需要下子egg并安装它即可。

2、非root用户安装 使用“vitual pylons”的方式安装。

3、理解安装过程 例如,你写了一个名为helloworld的pylons应用,现在通过一下步骤来安装: a 首先,生成config文件;

进入项目的根目录,执行下面的命令:
<shell-command>
python make-config helloword helloworld_production.ini
</shell-command>

b 修改生成的ini配置文件
c set-up命令
<shell-command>
python set-up helloworld_production.ini
</shell-command>

d 部署,这里使用paster serve,也可以使用apache cgi,fastcgi,scgi等
<shell-command>
paster serve helloworld_production.ini
</shell-command>

4、Make Config paster make-config 命令寻找 “paste_deploy_config.ini_tmpl”文件,并使用该文件作为生成 .ini文件的基础。 paste_deploy_config.ini_tmpl文件位于项目根目录下project.egg-info/目录下。文件内容类似如下:

[DEFAULT] debug = true

email_to = [email protected]

smtp_server = localhost

error_email_from = [email protected]

[server:main] use = egg:Paste

#http host = 0.0.0.0 port = 5000

[app:main] use = egg:helloworld

cache_dir = %(here)s/cache

app_instance_uuid = ${app_instance_uuid}

session_key = helloworld session_secret = ${app_instance_secret}

================================================================================ 配置文件

1、可以通过两种途径配置应用:

================================================================================ web-server配置

1、使用Fast-CGI Fast-CGI是连接CGI应用与web服务器之间的网关。pylons能够运行在Fast-CGI上,不管是线程模式还是进程模式。推荐使用线程模式。

配置文件非常简单,修改如下:
将配置文件
[server:main]
use = egg:Paste#http
改为:

    [server:main]
use = egg:PasteScript#flup_fcgi_thread
host = 0.0.0.0
port = 6500

安装flup软件包。flup是遵守wsgi(python web server gateway interface)的软件包。可以通过easy_install:
<shell-command>
[sudo] easy_install -U flup
</shelll-command>

config 文件中的选项传递给flup。运行FastCGI的两种方法是:使用socket监听requests,监听port/host。
socket方式的配置如下:
[server:main]
use = egg:PasteScript#flup_fcgi_thread
socket = /location/to/app.socket

port/host的方式的例子如第一个例子。

2、Apache配置
假设你使用Apache2。
首先,Apache需要安装mod_fastcgi;如果apache已经安装fastcgi模块,在apache的配置文件中应该有类似于如下的配置:
<IfModule mod_fastcgi.c>
FastCgiIpcDir /tmp
FastCgiExternalServer /some/path/to/app/myapp.fcgi -host some.host.com:6200
</IfModule>

在apache的配置文件中,还需要有以下指令:
AddHandler fastcgi-script .fcgi

然后,配置虚拟host:

</VirtualHost>

还有一些其他有用的指令,例如ErrorLog

myapp.fcgi如下:

# Load the WSGI application from the config file from paste.deploy import loadapp

# Deploy it using FastCGI from flup.server.fcgi import WSGIServer

必须将myapp.fcgi的权限设置为755。

当myapp.fcgi不存在时,apache出错如下: [Thu Jun 22 16:20:09 2006] [error] [client 10.10.136.96] (2)No such file or directory: FastCGI: stat() of "/home/guotie/py/pylons/helloworld/myapp.fcgi" failed

在myapp.fcgi的权限没有设置为755时,apache出错如下: [Thu Jun 22 17:26:47 2006] [error] [client 10.10.136.96] FastCGI: invalid (dynamic) server "/home/guotie/py/pylons/helloworld/myapp.fcgi": access for server (uid 33, gid 33) not allowed: execute not allowed

出错如下: [Thu Jun 22 17:31:05 2006] [error] [client 10.10.136.96] FastCGI: incomplete headers (0 bytes) received from server "/home/guotie/py/pylons/helloworld/myapp.fcgi" [Thu Jun 22 17:32:49 2006] [warn] FastCGI: (dynamic) server "/home/guotie/py/pylons/helloworld/myapp.fcgi" has failed to remain running for 30 seconds given 3 attempts, its restart interval has been backed off to 600 seconds 原因查找中。

PyLonsInstall (last edited 2009-12-25 07:12:31 by localhost)