欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Study Pylons One PylonsPythonOSCacheC# 

程序员文章站 2022-07-15 13:17:02
...

安装:
1. 下载安装工具ez_setup.py
2. 命令行运行:python ez_setup.py Pylons
    耐心等待,安装结束。
3. 设置环境变量
    系统变量->path->;C:\Python24\Scripts
4. 命令行运行:paster
    产生下面类似结果,则说明安装成功。
运行结果
Usage: C:\Python24\Scripts\paster-script.py COMMAND
usage: paster-script.py [paster_options] COMMAND [command_options]

options:
  --version         show program's version number and exit
  --plugin=PLUGINS  Add a plugin to the list of commands (plugins are Egg
                    specs; will also require() the Egg)
  -h, --help        Show this help message

Commands:
  create       Create the file layout for a Python distribution
  grep         Search project for symbol
  help         Display help
  make-config  Install a package and create a fresh config file/directory
  points       Show information about entry points
  serve        Serve the described application
  setup-app    Setup an application, given a config file

pylons:
  controller   Create a Controller and functional test for it

    The Controller command will create the standard controller template
    file and associated functional test to speed creation of controllers.

    Example usage::

        yourproj% paster controller comments
        Creating yourproj/yourproj/controllers/comments.py
        Creating yourproj/yourproj/tests/functional/test_comments.py

    If you'd like to have controllers underneath a directory, just include
    the path as the controller name and the necessary directories will be
    created for you::

        yourproj% paster controller admin/trackback
        Creating yourproj/controllers/admin
        Creating yourproj/yourproj/controllers/admin/trackback.py
        Creating yourproj/yourproj/tests/functional/test_admin_trackback.py

  shell        Open an interactive shell with the Pylons app loaded

    The optional CONFIG_FILE argument specifies the config file to use for
    the interactive shell. CONFIG_FILE defaults to 'development.ini'.

    This allows you to test your mapper, models, and simulate web requests
    using ``paste.fixture``.

    Example::

        $ paster shell my-development.ini
 开始动手:
1. 建立一个Pylons工程
    命令行运行:
    F:\python\lab\Pylons>paster create --template=pylons helloworld
    产生下面类似结果:
运行结果
Selected and implied templates:
  pylons#pylons  Pylons application template

Variables:
  egg:      helloworld
  package:  helloworld
  project:  helloworld
Creating template pylons
Creating directory .\helloworld
  Recursing into +egg+.egg-info
    Creating .\helloworld\helloworld.egg-info/
    Copying paste_deploy_config.ini_tmpl_tmpl to .\helloworld\helloworld.egg-inf
o\paste_deploy_config.ini_tmpl
  Recursing into +package+
    Creating .\helloworld\helloworld/
    Copying __init__.py_tmpl to .\helloworld\helloworld\__init__.py
    Recursing into config
      Creating .\helloworld\helloworld\config/
      Copying __init__.py_tmpl to .\helloworld\helloworld\config\__init__.py
      Copying environment.py_tmpl to .\helloworld\helloworld\config\environment.
py
      Copying middleware.py_tmpl to .\helloworld\helloworld\config\middleware.py

      Copying routing.py_tmpl to .\helloworld\helloworld\config\routing.py
    Recursing into controllers
      Creating .\helloworld\helloworld\controllers/
      Copying __init__.py_tmpl to .\helloworld\helloworld\controllers\__init__.p
y
      Copying error.py_tmpl to .\helloworld\helloworld\controllers\error.py
      Copying template.py_tmpl to .\helloworld\helloworld\controllers\template.p
y
    Recursing into docs
      Creating .\helloworld\helloworld\docs/
      Copying index.txt_tmpl to .\helloworld\helloworld\docs\index.txt
    Recursing into i18n
      Creating .\helloworld\helloworld\i18n/
      Copying __init__.py_tmpl to .\helloworld\helloworld\i18n\__init__.py
    Recursing into lib
      Creating .\helloworld\helloworld\lib/
      Copying __init__.py_tmpl to .\helloworld\helloworld\lib\__init__.py
      Copying app_globals.py_tmpl to .\helloworld\helloworld\lib\app_globals.py
      Copying base.py_tmpl to .\helloworld\helloworld\lib\base.py
      Copying helpers.py_tmpl to .\helloworld\helloworld\lib\helpers.py
    Recursing into models
      Creating .\helloworld\helloworld\models/
      Copying __init__.py_tmpl to .\helloworld\helloworld\models\__init__.py
    Recursing into public
      Creating .\helloworld\helloworld\public/
      Copying index.html_tmpl to .\helloworld\helloworld\public\index.html
    Recursing into templates
      Creating .\helloworld\helloworld\templates/
      Copying autohandler to .\helloworld\helloworld\templates\autohandler
    Recursing into tests
      Creating .\helloworld\helloworld\tests/
      Copying __init__.py_tmpl to .\helloworld\helloworld\tests\__init__.py
      Recursing into functional
        Creating .\helloworld\helloworld\tests\functional/
        Copying __init__.py_tmpl to .\helloworld\helloworld\tests\functional\__i
nit__.py
      Copying test_models.py_tmpl to .\helloworld\helloworld\tests\test_models.p
y
    Copying websetup.py_tmpl to .\helloworld\helloworld\websetup.py
  Copying README.txt_tmpl to .\helloworld\README.txt
  Copying development.ini_tmpl to .\helloworld\development.ini
  Copying setup.cfg_tmpl to .\helloworld\setup.cfg
  Copying setup.py_tmpl to .\helloworld\setup.py
Running C:\Python24\python.exe setup.py egg_info
Adding Pylons to paster_plugins.txt
Adding WebHelpers to paster_plugins.txt
2. 运行这个新建的工程
    1. 命令行运行:
        cd helloworld
        paster serve --reload development.ini
    2. 访问http://127.0.0.1:5000/ ,你将看到欢迎页面。
    3. 在helloworld/public目录下创建一个test.html的文件,内容如下:
<html>
    <body>
        Hello World!
    </body>
</html>
     4. 访问http://127.0.0.1:5000/test.html ,你将看到“HelloWorld!”。

3. 禁用调试功能
    将development.ini文件中的:
    #set debug = false
    改成:
    set debug = false

4. 创建一个控制器、修改Routes
    1. 命令行运行:F:\python\lab\Pylons\helloworld>paster controller hello
    2. 修改helloworld/controllers/hello.py,代码如下:

1from helloworld.lib.base import *
2
3class HelloController(BaseController):
4    def index(self):
5        return Response('hello world')

    3. 修改helloworld/config/routing.py,代码如下:

 """
 Setup your Routes options here
 """
 import sys, os
 from routes import Mapper
 
 def make_map(global_conf={}, app_conf={}):
     root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
    map = Mapper(directory=os.path.join(root_path, 'controllers'))
       # This route handles displaying the error page and graphics used in the 404/500
    # error pages. It should likely stay at the top to ensure that the error page is
    # displayed properly.
    map.connect('error/:action/:id', controller='error')
   
    # Define your routes. The more specific and detailed routes should be defined first,
    # so they may take precedent over the more generic routes. For more information, refer
    # to the routes manual @ http://routes.groovie.org/docs/
    map.connect('', controller='hello', action='index')
    map.connect(':controller/:action/:id')
   map.connect('*url', controller='template', action='view')

    return map
    4. 删除public/index.html。访问http://127.0.0.1:5000/hellohttp://127.0.0.1:5000/ ,你将看到“hello world”。

5.模版和请求周期
  1.创建模版文件: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>    2. 修改helloworld/controllers/hello.py,代码如下:
from helloworld.lib.base import *

class HelloController(BaseController):
    def index(self):
        return Response('hello world')
    def serverinfo(self):
        return render_response('/serverinfo.myt')
  3.访问http://127.0.0.1:5000/hello/serverinfo ,你将看到下面类似结果:
运行结果
Hi, here's the server environment:
{'HTTP_REFERER': 'http://pylonshq.com/docs/0.9.3/getting_started.html', 'paste.recursive.forward': , 'pylons.routes_dict': {'action': 'serverinfo', 'controller': 'hello', 'id': None}, 'paste.recursive.include': , 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/hello/serverinfo', 'SERVER_PROTOCOL': 'HTTP/1.1', 'QUERY_STRING': '', 'paste.throw_errors': True, 'CONTENT_LENGTH': '', 'HTTP_ACCEPT_CHARSET': 'gb2312,utf-8;q=0.7,*;q=0.7', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1) Gecko/20061010 Firefox/2.0', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'helloworld=ef423552a79452b4061d63335840e6690237c4375498846bc40a5e9218518af5', 'SERVER_NAME': '0.0.0.0', 'REMOTE_ADDR': '127.0.0.1', 'pylons.environ_config': {'cache': 'beaker.cache', 'session': 'beaker.session'}, 'paste.expected_exceptions': [], 'wsgi.url_scheme': 'http', 'beaker.cache': , 'paste.config': {'global_conf': {'error_email_from': 'paste@localhost' , 'email_to': 'you@yourdomain.com' , 'debug': 'false', '__file__': 'F:\\python\\lab\\Pylons\\helloworld\\development.ini', 'smtp_server': 'localhost', 'here': 'F:\\python\\lab\\Pylons\\helloworld'}, 'app_conf': {'session_key': 'helloworld', 'package': 'helloworld', 'session_secret': 'somesecret', 'cache_dir': 'F:\\python\\lab\\Pylons\\helloworld/data', 'session_data_dir': 'F:\\python\\lab\\Pylons\\helloworld/data\\sessions', 'cache_data_dir': 'F:\\python\\lab\\Pylons\\helloworld/data\\cache'}}, 'SERVER_PORT': '5000', 'paste.recursive.script_name': '', 'wsgi.input': , 'HTTP_HOST': '127.0.0.1:5000', 'beaker.session': {'_accessed_time': 1164775636.45, '_creation_time': 1164775537.0710001}, 'paste.recursive.include_app_iter': , 'wsgi.multithread': True, 'paste.httpexceptions': , 'HTTP_ACCEPT': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', 'wsgi.version': (1, 0), 'paste.registry': , 'wsgi.run_once': False, 'wsgi.errors': ', mode 'w' at 0x00ACE0B0>, 'wsgi.multiprocess': False, 'HTTP_ACCEPT_LANGUAGE': 'zh-cn,zh;q=0.5', 'CONTENT_TYPE': '', 'REMOTE_HOST': 'localhost', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate', 'HTTP_KEEP_ALIVE': '300'}

and here's the URL you called: /hello/serverinfo
6. 使用Sessions
    代码片段:
def serverinfo(self):
    session['name'] = 'George'
    session.save()
   return render_response('/serverinfo.myt')
. 控制器变量和模版全局变量
    控制器变量
    1. 修改helloworld/controllers/hello.py,代码如下:
from helloworld.lib.base import *

class HelloController(BaseController):
    def index(self):
        return Response('hello world')
    def serverinfo(self):
        c.value = 2
        return render_response('/serverinfo.myt')        2. 修改helloworld/templates/serverinfo.myt,代码如下:
<p>The value of <tt>c.value</tt> is:
<% c.value %>

    3. 访问http://127.0.0.1:5000/hello/serverinfo ,你将看到“The value of c.value is: 2”。
 
 模版全局变量
 .修改lib/app_globals.py,代码如下: 1class Globals(object):
 
     def __init__(self, defaults, app, **extra):
        self.message = 'Hello'   
        
    def __del__(self):
        """
         Put any cleanup code to be run when the application finally exits
        here.
       """
        pass
 2.修改helloworld/controllers/hello.py,代码如下:

from helloworld.lib.base import *

class HelloController(BaseController):
    def index(self):
        return Response('hello world')
    def serverinfo(self):
        c.value = 2
        return render_response('/serverinfo.myt')   
    def app_globals_test(self):
        resp = Response()
        if g.message == 'Hello':
            resp.write(g.message)
            g.message = 'Hello World!'
        else:
            resp.write(g.message)
        return resp
 3.访问http://127.0.0.1:5000/hello/app_globals_test/ ,你将看到“Hello World!”
参考资料:http://pylonshq.com/docs/0.9.3/getting_started.html