Home >Backend Development >PHP Tutorial >Getting Started with Karrigell_PHP Tutorial

Getting Started with Karrigell_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:29:36913browse

因为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."

If you use these Python scripts without passing the method name, your program will execute the index method by default. If the method name test is passed, the test method will be called. Calling a method that does not exist will throw an exception.
It is also very simple to use Karrigell services to process the data passed by the form. Let us create an ascname.ks

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

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

Of course, allowing external public access to your methods is a very dangerous thing. To prevent unauthorized users from accessing your methods, prefix your methods as follows:

def _private():
pass

Attempting to access the _private method will throw an exception.

Use HIP

In askname.py, there is one thing worth noting. This code contains the print statement. It would be a great thing if you could output directly without using print. Fortunately, Karrigell provides This is called HTML Inside Python and this will get rid of all the nasty print statements. And the conversion from askname.py to TML Inside Python is also very easy. We just need to move those print statements. Delete askname.py print in and rename the file to askname.hip

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

"
"
"
""
"
"

This is HTML Inside Python. Karrigell will inspect your files and automatically add prints where needed. HTML Inside Python is a testament to how easy Karrigell is to learn.

Python Inside HTML

Karrigell provides HTML Inside Python, so naturally, it will also provide Python Inside HTML. As usual, write python statements in special tags, and then send the final results to the user's browser. Let's To create a simple example, random.pih

<% import random %>
Random number: <% print random.random() %>

As you can see, the concept of Python Inside HTML is so simple. In fact, the code block can be even simpler:

<%= random.random() %>

But what if you are dealing with more complex logic, such as processing form data? Form data can be processed just like it is processed in Python scripts. There is a replica of askname.py askname.pih

<% if QUERY.has_key ( "name" ): %>
Your name is <%= _name %>.
<% end %>
<% else: %>
What is your name?






<% end %>

Please note the use of <% end %>. This will mark the end of the code block, just like the indentation of our conditional statement above. Another alternative is to use the indent tag, which is also used To identify code indentation.


<% if QUERY.has_key ( "name" ): %>
Your name is <%= _name %>.
<% else: %>
What is your name?






More features

Let’s try it like this. Tags can be used through Python scripts, tagtest.py

rom HTMLTags import *

print CENTER ( B ( "Test." ) )

Session can also be used

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531672.htmlTechArticleBecause python is not specifically designed for web development, many python users are now developing python A framework that runs on the web. Karrigell is one of the many python/web frameworks now...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn