Karrigell for Python (an introduction for foreigners)

Source: Internet
Author: User

http://www.devshed.com/c/a/Python/Karrigell-for-Python/

The basic usage of Karrigell, the article is written clear and concise

Since Python is not specifically designed for web development, a number of technologies created by Python users exist that Aim to provide a web development environment. While the exact approach to the situation varies among each framework, a few of these frameworks really stand out in the C Rowd. One such framework is Karrigell. Read on to learn more.

Introduction

While Karrigell are very powerful and flexible, offering multiple solutions to web development, it's surprisingly simple t o Set up and work with. Python novices won ' t find any obstacles when working with Karrigell, and Python experts won ' t feel too limited. It offers it own Web server that gets the job did, but it also can is easily integrated with technologies such as Apache , so don't have a to sacrifice the use of other technologies when choosing Karrigell.

This article would guide you through the installation of Karrigell and would explore some of the capabilities of the Web FRA Mework.

Installing Karrigell

Fortunately, there really isn ' t complicated magic involved with the installation of Karrigell. The obvious first step is to obtain the Web framework, available at SourceForge:

Http://sourceforge.net/projects/karrigell

Once you ' ve downloaded Karrigell, extract it to an easily accessible directory, such as. Next, then simply execute karrigell.py to start the built-in server. If you have want to run the included server and not set up Karrigell to work with another Web server, your work was done, a nd safely skip to the next section.

Otherwise, you'll need to configure your Web server to interact with Karrigell. We ll take a look at Apache. We'll need the Karrigell Web server to run behind Apache, so that Apache can pass off the relevant requests to Karrigell.

Since Apache would likely is running on port and you'll need to set the Karrigell server on another port. You can either does this through the command line or by editing Karrigell ' s configuration file. We'll use the port 8080 for Karrigell. The first method works something like this:

C:karrigell >karrigell.py-p 8080

To change Karrigell ' s configuration file to set a default port, simply add this to Karrigell.ini:

port=8080

Now we had to configure Apache to redirect requests for Karrigell. Likely, you'll want Apache to deal with all the static files of your website, along with other technologies such as PHP. To does this, you can create identical directory structures in both Karrigell and apache–or at least for the directors WH ere you want to utilize Karrigell–and configure Apache to redirect requests to karrigell-associated files to the Karrige ll server. All the need to does to the set this and is the add these lines to Apache httpd.conf file:

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]

If you want to, you can also set Apache to redirect all of the particular files in a directory to Karrigell. We'll be using a directory called Testarea throughout this article, so to configure Apache to redirect all requests to a F Ile within that directory to Karrigell, add this to httpd.conf:

Rewriteengine on
Rewriterule ^/testarea (. *) http://localhost:8080/testarea$1 [P]

You could also modify the first set of configuration directives so, they only apply to a certain directory:

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]

{Mospagebreak title=scripts and Services}

The first and methods Karrigell presents to developers is scripts and services. A script is simply a Python script this uses print to output to the user ' s browser. If you haven ' t-already, create a Testarea directory, and we can begin our first script. Create the file test.py:

Print "<center>"
Print "Hello!"
print "<br/><br/>"
Print "Karrigell is configured and working."
Print "</center>"

Point your browser to the file, and if you had Karrigell set up as described above, you should see the message described Above.

Form data is fairly easy-to-handle with Python scripts. Let's create a simple script whose output differs depending on whether the user have specified his or hers name in a form. Name It askname.py:

If Query.has_key ("name"):
Print "Your name is", _name + "."
Else
Print "What's Your name?<br/>"
Print "<form>"
Print "<input type= ' text ' name= ' name '/><br/>"
Print "<input type= ' submit ' value= ' Proceed '/>"
Print "</form>"

Services is written like Python scripts, too. However, they is designed to map requests to functions defined by the user. The desired function is a passed along in the URL after the name of the service. For example, the following URL would call the test function of the service TEST.KS:

Http://localhost/testarea/test.ks/test

Let ' s actually create the TEST.KS service:

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

If you call the script without passing a function name and then you'll be redirected to the index function. If the script passing the test function name, then the test function would be executed. Attempting to call a function not defined would produce an error.

Configuring services to accept form data are quite easy. Let ' s recreate askname.py as the service ASKNAME.KS:

def index ():
Print "What's Your name?<br/>"
Print "<form action= ' Namesubmit ' >"
Print "<input type= ' text ' name= ' name '/><br/>"
Print "<input type= ' submit ' value= ' Proceed '/>"
Print "</form>"
def namesubmit (name):
Print "Your name is", name + "."

Of course, making every single one of the Your service's functions accessible to the outside world could is a security hazard. To prevent users from accessing certain functions, simply prefix them with an underscore:

Def _private ():
Pass

Attempting to access the _private function would result in an error message.

{Mospagebreak title=being HIP}

In askname.py, one thing seems to be very noticeable:there is a lot of printstatements in the code. Wouldn ' t it being nice if we could eliminate them? Fortunately, Karrigell provides a method that does just. It ' s called HTML Inside Python, and it allows all those nasty print statements to be eliminated. It isn ' t very hard-to-convert askname.py to a HTML in Python file, either. We simply need to remove the print statements and change the extension. Delete askname.py ' s print statements and rename it to Askname.hip:

If Query.has_key ("name"):
"Your name is", _name + "."
Else
"What's Your name?<br/>"
"<form method= ' POST ' >"
"<input type= ' text ' name= ' name '/><br/>"
"<input type= ' submit ' value= ' Proceed '/>"
"</form>"

Truthfully, that's all there are to HTML Inside Python. Karrigell would examine your file and add print statements when necessary. HTML Inside Python is a wonderful demonstration of the simplicity of Karrigell.

Python Inside HTML

Since Karrigell features HTML Inside python, it's only logical to contain Python Inside html. The underlying idea–and even the look of the finished Code–is common, and it's been featured in other frameworks. Special tags are wrapped around Python code, and the overall result was sent to the user ' s browser. Let's create a simple example, RANDOM.PIH:

<% Import Random%>
Random Number: <b><% print random.random ()%></b>

As can see, the concept behind Python Inside HTML are pretty simple. Also, note that the block of code that prints the random number could is shortened even more:

<%= random.random ()%>

However, what's more complex operations, such as handling form data? Form data can is accessed just like it can is in Python scripts. Here is a recreation of the askname.py script, called Askname.pih:

<% if Query.has_key ("name"):%>
Your name is <%= _name%>.
<% End%>
<% Else:%>
What is your name?<br/>
<form method= ' POST ' >
<input type= ' text ' name= ' name '/><br/>
<input type= ' Submit ' value= ' Proceed '/>
</form>
<% End%>

Notice the use of <% end%>. This simply marks the end of a block of indentation, such as the indentation called for by our conditional statement. An alternative are the indent tag, which uses the indentation present in the code:

<indent>
<% if Query.has_key ("name"):%>
Your name is <%= _name%>.
<% Else:%>
What is your name?<br/>
<form method= ' POST ' >
<input type= ' text ' name= ' name '/><br/>
<input type= ' Submit ' value= ' Proceed '/>
</form>
</indent>

{mospagebreak title=a Few more Features}

Although I ' m not a huge fan of the practice, tags can be generated within Python scripts, as shown here in tagtest.py:

From htmltags Import *

Print CENTER (B ("Test."))

Sessions is also possible with Karrigell, and Karrigell presents a nice, object-oriented approach to Sessions. Let's create a simple script this demonstrates sessions in Karrigell. Upon accessing the script for the first time, the user would receive a "lucky number" of sorts. If The user refreshes, the same number would still appear since it'll be stored inside of a session. However, the user would be given the option of resetting he or her lucky number by closing the session. Create a Karrigell service named Luckynumber.ks in which to house this script:

Import Random

user = Session ()

def index ():
If not ' Luckynumber ' in Dir (user):
User.luckynumber = Random.randint (0, 20)
Print "Your Lucky number:", User.luckynumber
print "<br/><br/>"
Print "<a href= ' reset ' >reset Lucky number</a>"
def reset ():
User.close ()
Print "Your Lucky number has been reset."
print "<br/><br/>"
Print "<a href= ' index ' >Back</a>"

Conclusion

Karrigell offers four methods of web development using Python:python scripts, Karrigell services, HTML Inside Python and Python Inside HTML. Each method is a unique and has it benefits, but they all share one thing in Common:each method are very simple to employ I n Applications. Karrigell approaches web development in a straightforward manner, presenting simple solutions to a simple problem. Even installing Karrigell and configuring Apache and Karrigell to interact are a surprisingly simple process. Because of all this, Karrigell appeals to both newcomers and experts in Python alike.

Karrigell for Python (an introduction for foreigners)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.