Original text: http://www.devshed.com/c/a/Python/Karrigell-for-Python/
Because python is not specifically designed for web development, many python users are now developing programs that allow python to run on The framework of the web. Karrigell is one of the many python/web frameworks now. This article will give a brief introduction to Karrigell.
Introduction
Karrigell is a powerful and flexible python/web framework that provides a variety of solutions for web development. And, very importantly, its installation and use are very simple. Even beginners of python will not be confused when using Karrigell, and Karrigell does not limit some features of python because of its simple structure. It provides You can use your own web server to complete the work, but it is also very easy to work with other web servers (such as apache), so if you choose Karrigell, you can still use the web server you have been using before.
This article will start from Karrigell Let’s start with the installation and introduce several important features of Karrigell.
Installing Karrigell
As mentioned above, the installation of Karrigell is not troublesome at all. The first step you need to do is to download the latest Karrigell from SourceForge. After you download the zip package , extract it to an easy-to-find directory. Then double-click Karrigell.py to start Karrigell's built-in web server. If you don't want to use Karrigell with other web servers now, the installation of Karrigell ends here.
But if you want to set up your web server to integrate with Karrigell, let's take apache as an example. We now need Karrigell to run in the background of apache, so that apache will send relevant requests to Karrigell
Because apache usually runs on port 80, you need to set another running port for Karrigell. There are two ways to achieve this purpose. One is to use the command line to complete it, or you can change the configuration file of Karrigell. Let’s first see how Using the command line, we will run Karrigell on port 8080.
C:Karrigell>Karrigell.py -P 8080
Second, you need to find Karrigell.ini, and then add the sentence
port=8080
Now you need to configure apache to transfer the relevant The request is redirected to Karrigell. Generally speaking, you want apache to handle any static file request, such as php. You can share the same path with apache, or you can set up a separate path. Then set which files require apache to pass the request to Karrigell, you need to add the following to apache's 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, you can set all requests for a specific file to be forwarded to Karrigell. In this article, we The testarea path will be used, so we set httpd.conf like this:
RewriteEngine On
RewriteRule ^/testarea(.*) http://localhost:8080/testarea$1 [P]
Of course you can do this:
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 and Services
Python scripts and Karrigell services will be the two concepts most accessible to python developers using Karrigell. Python scripts are python scripts, and developers use The print statement outputs the content to the user's browser. If you don't understand yet, create a testarea directory, and then we will start creating our first Python scripts. Create a new file test.py:
print "<center>"
print "Hello!"
print "<br /><br />"
print "Karrigell is configured and working."
print "</center>"
Open this file in the browser. If your Karrigell has been set up correctly before, you will see the output.
Python scripts can also easily use forms. Let's create a simple page where users can enter their names. Create a new file askname.py:
if QUERY.has_key ( "name" ):
print "Your name is", _name + "."
else:
print "What is your name?<br />"
print "<form>"
print "<input type='text' name='name' /><br />"
print "<input type='submit' value='Proceed' />"
print "</form>"
Karrigell services are written similarly to Python scripts. The purpose of this is to map requests to user-specified methods. The name of the specified method is passed after the Karrigell services. For example, the following url will call test This method
http://localhost/testarea/test.ks/test
lets us actually write this example
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 abnormal.
It's also easy to use Karrigell services to handle data passed in forms. Let's create an ascname.ks
def index():
print "What is 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, allowing external public access to your methods is a very dangerous thing. To prevent unauthorized users from accessing your methods, prefix your methods as described below :
def _private():
Pass
attempts to access the _private method will throw an exception.
Using HIP
in askname.py, there is one thing worth noting. This code has a print statement. If it can be output directly without using print, it will be a What a great thing. Luckily, Karrigell provides such functionality. This is called HTML Inside Python and this will clean up all those nasty print statements. And the conversion from askname.py to TML Inside Python is also very easy. We just Those print statements need to be removed. Delete the print in askname.py and rename the file to askname.hip
f 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>"
This is HTML Inside Python. Karrigell will inspect your file and automatically add print where needed. HTML Inside Python is a testament to how easy Karrigell is to learn.
Python Inside HTML
Karrigell provides HTML
Inside Python, naturally, 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 create a simple example, random.pih
<% import random %>
Random number: <b><% print random.random() %></b>
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. This is a replica of askname.py 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 %>
Note the use of <% end %>. This will mark the end of the block of code, 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.
<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>
Let’s try
more features
like this. Tags can be used through Python scripts, tagtest.py
rom HTMLTags import *
print CENTER ( B ( "Test." ) )
session can also be processed in Karrigell, and Karrigell also provides a good session mechanism close to object-oriented. Let us create a simple code to demonstrate in Karrigell session. Going back to our first example, the user will be given a lucky number. If the user refreshes the page, the number will still be there because the number will be saved in the sessin. Of course, the user will have the option to re-fetch a new one Lucky number, this is done by closing sessin. Create a Karrigell services called luckynumber.ks and enter the following code:
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 provides four methods for web opening, Python scripts, Karrigell services, HTML Inside Python and Python Inside HTML. Each method has its own characteristics. But they all have one common advantage: simplicity of use. Karrigell turns web development into an elegant and simple thing. Whether you use Karrigell alone or in combination with apache, it is surprisingly simple. Because, no matter Whether for beginners or veterans, Karrigell is a good choice.