Karrigell ---python web 框架介绍

来源:互联网 发布:淘宝购物车营销在哪 编辑:程序博客网 时间:2024/06/07 10:40

背景: 

Python 提供了五花八门的web框架,有功能强大的Django,也有特别方便小巧的Bottle, Karrigell, wxpython,

最近在看《可爱的python》这本书,对于有编程基础的人,这本书入门python非常好,里面介绍了Karrigell

这个web框架,它是一个支持python开发web程序的框架,简单来说,就是可以解释python脚本的web服务器。

当然,语法不是单纯的python脚本,而是python脚本与HTML的结合体。这样如果按照了Karrigell就可以去

开一个python脚本写的界面。

Karrigell的web服务器的功能

      1. 提供了4种不同的方法支持python开发web界面(html里嵌入python,python里嵌入html等,语法不一样)

      2. 提供多种web服务,支持session管理

      3. 自身做为web服务器解释页面,完成请求

      4. 可以和其他web服务器(eg:Apache)结合工作,你选择了Karrigell,仍可使用之前一直在用的web服务器

      5. 提供了一个小型的数据库来和python页面交互, 当然也支持其他数据库(eg:Mysql)

 下面我们来看一下Karrigell如果操作,以下的步骤都是基于最新的版本,Karrigell-3.1.1。

  一、Karrigell 的安装和运行

      1.到SourceForge或者官网下载最新的Karrigell: http://sourceforge.net/projects/karrigell/

      2. 如果在linux下面,是Karrigell-3.1.1_core.tar.gz, 使用tar -xvf  Karrigell-3.1.1_core.tar.gz 解压,然后

          进入Karrigell-3.1.1_core(可以重命名为简单的名字Karrigell),执行python Karrigell.py即可运行.

          如果在windows下面,安装后,直接在Karrigell-3.1.1_core下面双击Karrigell.exe文件即可

          运行成功的话,窗口会显示(如下),然后,你就可以运行python网页了!

                              Karrigell 3.1.1 running on port 80 (ipv4)

                              Press Ctrl+C to stop

 二、 Karrigell的端口配置和使用

      1. 在Karrigell目录下,有server_config.py(关于服务器的配置信息), 可以看到服务器默认端口是port = 80。

          为了防止和其他web服务冲突,我们一般为Karrigell要更改服务器端口,我们就把 server_config.py里面的

          port = 8081(可以任意)。这样再重新运行python Karrigell.py 就出现:

                            Karrigell 3.1.1 running on port 8081 (ipv4)

                            Press Ctrl+C to stop

      2. Karrigell web服务的主目录:Karrigell/www (默认)

          在浏览器上输入http://localhost:8081,就可以打开Karrigell的主界面,这个界面就是目录里面index.py的内容

          我们可以试着编写了一个最简单的python脚本hello.py   (内容是:print "hello world"), 然后在浏览器输入http://localhost:8081/hello.py或者http://localhost:8081/hello

          页面上就显示了 hello world

 三、python脚本和html的结合

         我们先看一个《可爱的python》上面列举的一个小例子hh.py:

def _htmhead(title):    htm = <span style="color:#ff0000;"><strong>"""</strong></span><html> <HEAD><meta http-equi v="Content-Type" content = "text/html;charset=utf-8"/><title>%s</title></HEAD><body><strong><span style="color:#ff0000;">"""</span></strong>%title    return htmhtmfoot= <span style="color:#3366ff;">"""</span><h5>design by:<a href="abc">Zoom.Quiet</a>    powered by: <a href=<a>http://</a>python.com>Python</a> +<a href=<a target=_blank href="http://karrigell.com">http://karrigell.com</a>>KARRIGELL2.3.5</a></h5></body></html><span style="color:#3366ff;">"""</span>#if __name__=='__main__':print _htmhead("PyCDC WEB")print htmfoot

    在浏览器里面输入http://localhost:8081/hh.py之后,页面显示:

                             design by:Zoom.Quiet    powered by:Python +KARRIGELL2.3.5

    可以看到python和html结合的方法之一是:就是把html页面的内容用print输出

     1. html中的内容可以使用python中的变量或者函数参数来传递值

     2. python脚本中的变量可以用html语句形成的字符串表示

     3.  然后python脚本中的print将字符串内容打印出来,显示到页面上  

    看个小例子,再感受一下:  

<pre class="python" name="code">print "<center>"print "<a href=http://Sourseforge/project/karrigel>"print "here is the Karrigell download.</a>"print "<B>"print "you can try by yourself."print "</B>"print "</center>"


 

 

 

 

 

 

 

 

对于web开发的具体过程,我会在后续总结出来,这里列出参考文章:

http://www.devshed.com/c/a/Python/Karrigell-for-Python/

http://bbs.blueidea.com/thread-2802613-1-1.html (中文翻译版)

http://blog.csdn.net/xbl1986/article/details/6242634 (Karrigell目录介绍)

 

 

 

0 0
原创粉丝点击