11 million installations can't be wrong

MySQL Journal

Subscribe to MySQL Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get MySQL Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

MySQL Journal Authors: Greg Schulz, Cloud Best Practices Network, Jayaram Krishnaswamy, Elizabeth White, Jnan Dash

Related Topics: Apache Web Server Journal, PHP Developer's Journal

Apache Web Server: Article

How to add spyce to your life

Tricks, tips, and techniques for making spyce work with your server.

(LinuxWorld) — There's a new method for doing server-side Python scripting for Web applications in town, and its name is spyce. Spyce takes an approach similar to PHP. It lets you intermingle Python scripts, statements and variables with HTML to produce dynamically generated Web pages. You could do the same thing with Python Server Pages (PSP) and Webware, but that approach forces you to run a separate Python-based application server alongside Apache. Spyce lets you avoid the extra application server because it can run via the Apache mod_python module, fastcgi, or plain CGI.

I happen to like the Python language, so I downloaded the software from the link provided in the resources section below. The installation is manual, but it's a cinch. My uneducated guess is that mod_python will probably give me the best performance, so I choose that method for using spyce. Here are the configuration options I add to my httpd.conf file for Apache version 1.3, which is what my server is set up to use.

Warning #1: I'm running this on a Debian system, so the paths could be different than depending on what distro you use and where you plan to install spyce. Warning #2: It might be tempting to turn on PythonOptimize, but don't! The spyce folks forbid it.

This first change tells Apache to load the mod_python module.

LoadModule action_module /usr/lib/apache/1.3/mod_python.so

I add index.spy to the list of default files for Apache to load in a directory.

<IfModule mod_dir.c>
    DirectoryIndex index.html index.htm index.shtml index.php index.spy

Finally, I add a section that enables spyce.

# Spyce via mod_python
<IfModule mod_python.c>
  AddHandler python-program .spy
  PythonHandler run_spyceModpy::spyceMain
  PythonPath sys.path+[r'/usr/local/spyce-1.2.2/']
  PythonOptimize Off

I reload Apache and run the sample hello.spy file. It won't load. I've had problems like this before, and the cause is usually an Apache module conflict. I'm running the PHP module with Apache concurrently with mod_python, so that's the most likely culprit. I don't want to give up PHP, so I figure I'll try removing a few of the other modules first. I comment out the CGI module. Now the hello.spy works fine.

Then I tackle my first spyce page.

It's quite easy to use spyce, although there are a couple of gotchas you may run into at first, some of which I'll cover next week. The basic spyce page looks something like this (file hello1.spy):

  [[ print 'Hello world!' ]]

Obviously, you use the double square brackets to escape out of HTML and execute your Python commands. If you use plain double square brackets, each Python command needs to be enclosed in its own set of brackets. If you want to create a chunk of Python code, complete with the required whitespace indenting that drives non-Python users crazy, follow the opening brackets with a backslash, thusly (file hello2.spy):

  for i in range(5):
    print 'Hello world!<br>'

There are a few other details you'll need to know to use spyce, but I'll cover those in more detail next week.

The hello2.spy file is not much more complicated than the sample hello.spy page or my above hello1.spy page. However, the block of Python code somehow causes the page not to load. I get error after error, and the error messages seem to change whenever I reload the page, even if I don't make any changes to the code.

I try several changes out of wishfully thinking that I'm not using spyce properly rather than having discovered a conflict with the PHP module. After a while I give up, comment out the line in the Apache configuration file that loads PHP, and reload Apache. Sure enough, the hello2.spy page works fine.

This is discouraging at first. PHP has a huge installed base. If you're not interested in PHP and host your own sites, that won't be a problem. However, commercial web hosts will run whatever most people want, and that's PHP. If it is difficult or impossible to support both PHP and mod_python, PHP will win.

I'm intent upon finding a way to run both, however. I think it through. Conflicts like these often pop up when two Apache modules use different versions of common C language header files, such as a header that provides regular expression capabilities. It's possible that I could make the two modules work together if I tweak the source code and recompile PHP and/or mod_python. Surely there must an easier way. After a few hours of intense meditation, a loud screech from my parrot wakes me up. Suddenly the ridiculously obvious answer hits me. Why not avoid mod_python by running spyce in fastcgi mode? Duh.

I reconfigure the Apache httpd.conf file. First, I add the following lines to load the actions and fast_cgi modules, which I'll need for the other settings I'll add later. It actually doesn't hurt if you don't comment out mod_python, but we'll play it safe.

Be careful to add the line that loads the fast_cgi module. Apache will default to running your spyce pages as regular CGI programs even if you forget to load fast_cgi, so you may not notice your error if you forget. What you may notice is that the pages load slower without fast_cgi, but it's hard to say without running benchmarks.

LoadModule action_module /usr/lib/apache/1.3/mod_actions.so
LoadModule fastcgi_module /usr/lib/apache/1.3/mod_fastcgi.so
# LoadModule action_module /usr/lib/apache/1.3/mod_python.so

I comment out the section that tells Apache to run spyce via mod_python, and add a section that tells Apache to run spyce via fastcgi. Again, beware of path differences on your system.

# Spyce via fcgi

AddHandler spyce-cgi-handler spy Action spyce-cgi-handler /spyce-cgi/run_spyceCGI.py ScriptAlias /spyce-cgi/ /usr/local/spyce-1.2.2/ <Location /spyce-cgi/> <IfModule mod_fastcgi.c> SetHandler fastcgi-script </IfModule> </Location>

################### # Comment out # Spyce via mod_python # #<IfModule mod_python.c> # AddHandler python-program .spy # PythonHandler run_spyceModpy::spyceMain # PythonPath sys.path+[r'/usr/local/spyce-1.2.2/'] # PythonOptimize Off #</IfModule>

I am finally ready to test the new configuration. I restart Apache and, lo and behold, my new spyce test program runs like a charm. I try out a PHP page, and that works, too. Problem solved.

It's time to tackle some object-oriented programming. Not long after I write a bit more complex program, things start to fall apart again. This time, however, the problem is one of misinterpreting how spyce programming works. I will cover those gotchas next week. If you're a Python fan, you'll want to tune in again.

More Stories By Nicholas Petreley

Nicholas Petreley is a computer consultant and author in Asheville, NC.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.