Karrigell
是一个语法简洁直观的
Python Web
框架。它支持任何一种数据库,
ORM
或者模板引擎,也可以让开发人员选择不同的编码方式。
Karrigell
可以通过配置来与其它的
web
服务器一起工作如
Apache,Xitami,LightTPD
;
Karrigell
还可以结合
Python
的其它数据库
(sqlite, mySql, PostGreSQL, ZODB, etc)
接口来创建数据库应用程序。
Karrigell
还自带了一个对
MySQL
数据库表进行
CRUD
操作的应用程序,可以方便的创建和编辑
MySQL
并且可以在线生成脚本。
要想使用
Karrigell
创建动态页面,你需要知道
HTML
和
Python
的一些知识。你也要选择
Karrigell
提供的不同的编码方式。下面是
“Hello world”
程序使用的
5
种不同的编码方式:
Python script (hello.py)
|
Karrigell service (hello.ks)
|
print "Hello, world !"
|
def index(): print "Hello, world !" |
HTML Inside Python (hello.hip)
|
Python Inside HTML (hello.pih)
|
"Hello, world !"
|
Hello, world !
|
CGI script (hello.py in folder cgi-bin) |
|
print "Content-type: text/html" print "Hello, world !" |
|
如果要想像这样写一些Python代码的话,例如打印0到9的数字序列:
Python script
|
Karrigell service
|
print "<h1>Squares</h1>"
for i in range(10):
print "%s :<b>%s</b>" %(i,i*i)
|
def index():
print "<h1>Squares</h1>"
for i in range(10):
print "%s :<b>%s</b>" %(i,i*i)
|
HTML Inside Python
|
Python Inside HTML
|
"<h1>Squares</h1>"
for i in range(10):
"%s :<b>%s</b>" %(i,i*i)
|
<h1>Squares</h1>
<%
for i in range(10):
print "%s :<b>%s</b>" %(i,i*i)
%>
|
CGI script
|
|
print 'Content-type: text/html'
print
print "<h1>Squares</h1>"
for i in range(10):
print "%s :<b>%s</b>" %(i,i*i)
|
|
总之我们可以使用:
-
可以使用普通的 Python 脚本
-
Karrigell services 是一种 Python 脚本,它的每一个函数都匹配一个不同的 URL : foo.ks/bar 匹配 foo.ks 脚本中的 bar() 函数 ( 如果没有指定函数 , 默认使用 index() 函数 )
-
HTML inside Python 是另一种写 Python 脚本的方式,在运行时当遇到 print 语句就会把要输出的字符串发送给客户端浏览器
-
Python inside HTML 是一种非常像 ASP,JSP,PHP 的写法, Python 写在 HTML 页面 < 的 % %> 标记中
-
还可以直接使用 CGI 脚本:参见文档的 cgi module 章节
Python代码在一个包含HTTP环境,表单字段,自定义异常的命名空间下运行。当一个表单包括字段<INPUT name="myfield">,它的值可以在脚本中使用_myfield来得到。
在身份认证和Session方面,可以在脚本中使用两个叫做Authentication和Session的函数来处理。Authentication的第一个参数是一个测试函数,用来检查是否接受输入的AUTH_USER和AUTH_PASSWORD。Session()用来初始化一个session对象并设置或读取它的属性值或者得到一个session。
Include(file_or_script)函数在当前脚本插入脚本或者文件的输出结果;例如它可用在页头或者页尾。