Karrigell 是功能強大、靈活、安裝極簡的 Python Web 框架。內建 Web 服務器,亦易於與 Apache 等整合。適合 Python 初學者快速構建 Web 應用。
Karrigell.py 啟動內建服務器(預設端口 80)。Karrigell.ini 設 port=8080,或命令行 Karrigell.py -P 8080。httpd.conf 添加:
RewriteEngine On
RewriteRule ^/testarea(.*) https://localhost:8080/testarea$1 [P]
將 /testarea 下請求轉發至 Karrigell。亦可細化至 .py/.ks/.hip/.pih 等副檔名。
直接用 print 輸出 HTML:
print "<center>Hello!<br>Karrigell works.</center>"
處理表單:
if QUERY.has_key("name"):
print "Your name is", QUERY["name"] + "."
else:
print "<form><input name=name><input type=submit></form>"
方法映射模式:URL /test.ks/method 調用 method()。
def index(): print "Index"
def test(): print "Test"
表單處理更自然:
def index():
print "<form action=nameSubmit><input name=name><input type=submit></form>"
def nameSubmit(name):
print "Your name is", name + "."
私有方法以下劃線開頭(_private),外部不可訪問。
移除 print,直接寫 HTML/字符串,框架自動輸出:
if QUERY.has_key("name"):
"Your name is", QUERY["name"] + "."
else:
"What is your name?<br>"
"<form method=POST><input name=name><input type=submit></form>"
模板風格,代碼塊包裹在 <% ... %>:
<% import random %>
Random: <b><% print random.random() %></b>
<% if QUERY.has_key("name"): %>
Your name is <%= QUERY["name"] %>.
<% else: %>
<form method=POST><input name=name><input type=submit></form>
<% end %>
或用 <indent> 標籤管理縮進塊。
from HTMLTags import * → print CENTER(B("Test")) 程式化生成 HTML。
Session 支援內建,配置簡單。
Karrigell 以「簡單」為核心,提供 Script/Service/HIP/PIH 四種開發模式,內建服務器、易整合 Apache,是 Python Web 入門與輕量專案的優選。