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 crowd. One such framework is Karrigell. Read on to learn more.
The first two methods Karrigell presents to developers are scripts and services. A script is simply a Python script that uses print to output to the user's browser. If you haven't done so 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 have 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 has specified his or her name in a form. Name it 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>"
Services are written like Python scripts, too. However, they are designed to map requests to functions defined by the user. The desired function is 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:
If you call the script without passing a function name, then you will be redirected to the index function. If you call the script passing the test function name, then the test function will be executed. Attempting to call a function not defined will produce an error.
Configuring services to accept form data is quite easy. Let's recreate askName.py as the service askName.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, making every single one of your service's functions accessible to the outside world could be 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 will result in an error message.