Karrigell 入门_PHP教程

WBOY
Libérer: 2016-07-13 17:29:36
original
853 Les gens l'ont consulté

因为python并不是特别为web开发而生,所以现在很多的python使用者都在开发能够让python运行于web的框架.Karrigell就是现在为数众多的python/web框架之一.本文将对Karrigell做简要的介绍.

简介

Karrigell是一个功能强大且灵活的python/web框架,为web开发提供了多种解决方案.并且,很重要的,它的安装和使用都非常的简单.即使是python的初学者,也不会在使用Karrigell的时候犯迷糊,并且Karrigell也没有因为结构简单而限制python的一些特性.它提供了自己的web服务器完成工作,但是也非常容易的和其他的web服务器(例如apache)结合工作,所以如果你选择了Karrigell,你仍然可以使用之前一直在使用的web服务器.

这篇文章将从Karrigell的安装开始说起,介绍数个Karrigell的重要特性.

安装Karrigell

上面说了,Karrigell的安装一点都不麻烦.你要做的第一步是到 SourceForge 下载最新的Karrigell,当你下载了zip包后,将其解压缩到一个容易找到的目录.然后直接双击Karrigell.py启动Karrigell的内置web服务器.如果你现在并不想把Karrigell和其他的web服务器结合使用,那Karrigell的安装到此就结束了.

但如果你想要设置你的web服务器来和Karrigell结合,我们拿apache来举例子.我们现在需要Karrigell在apache的后台运行,来使apache将相关的请求传送给Karrigell
因为apache一般会运行在80端口,你需要给Karrigell设置一个其他的运行端口.有两个方法来达到这个目的,其一是使用命令行来完成,或者可以改变Karrigell的配置文件.先来看如何用命令行,我们将在8080端口运行Karrigell.

C:Karrigell>Karrigell.py -P 8080

第二种,你需要找到Karrigell.ini,然后加上这么一句

port=8080

现在你需要配置apache将有关的请求转向到Karrigell.一般来说,你希望apache可以处理任何的静态文件请求,比如php.你可以和apache共享同一个路径, 也可以设立单独的路径..然后设置那些文件需要apache把请求传递给Karrigell,你需要添加下面的内容到apache的 httpd.conf 文件.

RewriteEngine On
RewriteRule ^/(.*).py(.*) http://localhost:8080/$1.py$2 [L,P]
RewriteRule ^/(.*).ks(.*) http://localhost:8080/$1.ks$2 [L,P]
RewriteRule ^/(.*).hip(.*) http://localhost:8080/$1.hip$2 [L,P]
RewriteRule ^/(.*).pih(.*) http://localhost:8080/$1.pih$2 [P]

如果你想, 你可以设置全部的特定文件请求都转给Karrigell.在本文中,我们将使用testarea这个路径,所以我们这样设置httpd.conf:

RewriteEngine On
RewriteRule ^/testarea(.*) http://localhost:8080/testarea$1 [P]

当然你可以这样:

RewriteEngine On
RewriteRule ^/testarea/(.*).py(.*)
http://localhost:8080/testarea/$1.py$2 [L,P]
RewriteRule ^/testarea/(.*).ks(.*)
http://localhost:8080/testarea/$1.ks$2 [L,P]
RewriteRule ^/testarea/(.*).hip(.*)
http://localhost:8080/testarea/$1.hip$2 [L,P]
RewriteRule ^/testarea/(.*).pih(.*)
http://localhost:8080/testarea/$1.pih$2 [P]

Scripts和Services

Python scripts和Karrigell services将是python开发者使用Karrigell最容易接触的两个概念.Python scripts就是python的脚本,开发者使用print语句输出到用户浏览器的内容.如果你还不明白,建立一个testarea目录,然后我们将开始创建我们的第一个Python scripts.新建一个文件test.py:

print "

"
print "Hello!"
print "

"
print "Karrigell is configured and working."
print "
"

在浏览器中打开这个文件,如果之前你的Karrigell都正确设置,那你将会看到输出的内容.
Python scripts也可以容易的使用表单.让我们再来创建一个简单的页面,用户可以在这个页面输入自己的名字.新建文件askname.py:

if QUERY.has_key ( "name" ):
   print "Your name is", _name + "."
else:
   print "What is your name?
"
   print "

"
   print "
"
   print ""
   print "
"

Karrigell services的编写和Python scripts类似.这个的作用是映射请求到用户指定的方法.指定方法的名字通过跟在Karrigell services后面进行传递.比如,下面这个url将会调用test这个方法

http://localhost/testarea/test.ks/test

让我们实际编写下这个例子

def index():
   print "Index function."
def test():
   print "Test function."

如果不传递方法名而使用这些Python scripts,你的程序会默认执行index方法.如果传递了方法名test,那test方法就会被调用.调用一个不存在的方法会抛出异常.
使用Karrigell services来处理表单传递的数据也很简单.让我们来创建一个ascname.ks

def index():
   print "What is your name?
"
   print "

"
   print "
"
   print ""
   print "
"
def nameSubmit ( name ):
   print "Your name is", name + "."

当然,允许外部公开访问你的方法是一件很危险的事情,为了阻止不允许的用户访问你的方法,按照下面所述给你的方法加前缀:

def _private():
   pass

尝试访问_private方法将会抛出异常.

使用HIP

在askname.py,有一件事情值得注意,这个代码镉刑嗟膒rint语句.如果可以不使用print而直接输出,那将会是一件多么好的事情.幸运的是,Karrigell提供了这样的功能.这被称为HTML Inside Python,这将清除所有讨厌的print语句.并且从askname.py到TML Inside Python的转换也非常容易.我们只是需要移走那些print语句.删除askname.py里的print并将文件重命名为askname.hip

f QUERY.has_key ( "name" ):
   "Your name is", _name + "."
else:
   "What is your name?
"
   "

"
   "
"
   ""
   "
"

这就是HTML Inside Python.Karrigell将会检查你的文件并自动在需要的地方添加print.HTML Inside Python是Karrigell简单易学的一个有力证明.

Python Inside HTML

Karrigell提供了HTML Inside Python,那自然的,也会提供Python Inside HTML.和一般的做法一样,将python语句写在特殊的标记里,然后把最终结果发送到用户的浏览器.让我们来创建一个简单的示例,random.pih


Random number:

正如你说看到的,Python Inside HTML的概念如此的简单.而其实,代码块可以更加的简单:

但是,如果处理更复杂的逻辑,比如处理表单数据?表单数据可以像在Python scripts中被处理那样被处理.这是有一个askname.py的复制品askname.pih


   Your name is .


   What is your name?

  


  

  
  

请注意的使用.这将标识代码块的结尾,就像上面我们的条件语句的缩进一样.另一个可选择的办法是使用indent标签,这个也被用来标识代码缩进.



   Your name is .

   What is your name?

  


  

  
  

更多的特性

我们来这样试一下,标签可以通过Python scripts这样使用,tagtest.py

rom HTMLTags import *

print CENTER ( B ( "Test." ) )

session同样可以

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/531672.htmlTechArticle因为python并不是特别为web开发而生,所以现在很多的python使用者都在开发能够让python运行于web的框架.Karrigell就是现在为数众多的python/web框架...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!