Welcome!

Linux Containers Authors: Liz McMillan, Elizabeth White, Zakia Bouachraoui, Pat Romanski, Stefana Muller

Related Topics: Linux Containers

Linux Containers: 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
</IfModule>

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
</IfModule>

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):

<html><body>
  [[ print 'Hello world!' ]]
</body></html>

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):

<html><body>
[[\
  for i in range(5):
    print 'Hello world!<br>'
]]
</body></html>

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.


IoT & Smart Cities Stories
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.
The Jevons Paradox suggests that when technological advances increase efficiency of a resource, it results in an overall increase in consumption. Writing on the increased use of coal as a result of technological improvements, 19th-century economist William Stanley Jevons found that these improvements led to the development of new ways to utilize coal. In his session at 19th Cloud Expo, Mark Thiele, Chief Strategy Officer for Apcera, compared the Jevons Paradox to modern-day enterprise IT, examin...
With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO and DXWorldEXPO are two of the most important technology events of the year. Since its launch over eight years ago, @CloudEXPO and DXWorldEXPO have presented a rock star faculty as well as showcased hundreds of sponsors and exhibitors! In this blog post, we provide 7 tips on how, as part of our world-class faculty, you can deliver one of the most popular sessions at our events. But before reading...
DSR is a supplier of project management, consultancy services and IT solutions that increase effectiveness of a company's operations in the production sector. The company combines in-depth knowledge of international companies with expert knowledge utilising IT tools that support manufacturing and distribution processes. DSR ensures optimization and integration of internal processes which is necessary for companies to grow rapidly. The rapid growth is possible thanks, to specialized services an...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...