Home php教程 php手册 Karrigell 入门

Karrigell 入门

Jun 13, 2016 am 10:29 AM
python web user getting Started develop Now

因为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同样可以

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to view server version of Redis How to view server version of Redis Apr 10, 2025 pm 01:27 PM

Question: How to view the Redis server version? Use the command line tool redis-cli --version to view the version of the connected server. Use the INFO server command to view the server's internal version and need to parse and return information. In a cluster environment, check the version consistency of each node and can be automatically checked using scripts. Use scripts to automate viewing versions, such as connecting with Python scripts and printing version information.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

How to set the Redis memory size according to business needs? How to set the Redis memory size according to business needs? Apr 10, 2025 pm 02:18 PM

Redis memory size setting needs to consider the following factors: data volume and growth trend: Estimate the size and growth rate of stored data. Data type: Different types (such as lists, hashes) occupy different memory. Caching policy: Full cache, partial cache, and phasing policies affect memory usage. Business Peak: Leave enough memory to deal with traffic peaks.

What is the impact of Redis persistence on memory? What is the impact of Redis persistence on memory? Apr 10, 2025 pm 02:15 PM

Redis persistence will take up extra memory, RDB temporarily increases memory usage when generating snapshots, and AOF continues to take up memory when appending logs. Influencing factors include data volume, persistence policy and Redis configuration. To mitigate the impact, you can reasonably configure RDB snapshot policies, optimize AOF configuration, upgrade hardware and monitor memory usage. Furthermore, it is crucial to find a balance between performance and data security.

Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

What are the Redis memory configuration parameters? What are the Redis memory configuration parameters? Apr 10, 2025 pm 02:03 PM

**The core parameter of Redis memory configuration is maxmemory, which limits the amount of memory that Redis can use. When this limit is exceeded, Redis executes an elimination strategy according to maxmemory-policy, including: noeviction (directly reject write), allkeys-lru/volatile-lru (eliminated by LRU), allkeys-random/volatile-random (eliminated by random elimination), and volatile-ttl (eliminated by expiration time). Other related parameters include maxmemory-samples (LRU sample quantity), rdb-compression

See all articles