Web Hosting Philippines, Offshore Programming, Offshore SEO Philippines, Cheap Webhosting Manila Philippines
Home -> Resources -> Python -> Spyce Development Tips and Techniques

What is Spyce?

Spyce is a mature Python server pages technology and one of the most versatile around. It works very much like straight PHP except that the language used is Python. Unlike Python frameworks and web stacks such as Django and TurboGears, Spyce requires very little additional study to get started with and lets you utilize your existing knowledge of Python libraries almost immediately.

One of the most important benefits of using Spyce over more arcane Python web technologies such as Django/TurboGears/etc... is that it will be much easier to find programmers to inherit your codebase if you ever find yourself needing to do this. Instead of looking for a Django, TurboGears, CherryPy, etc... expert, you only need someone who knows Python itself.

Python's expressiveness means that complex web applications written using the html-embedded script style can still retain a conciseness that promotes code manageability. Combining Python's innate expressivity with a handful of minimalistic, but powerful mechanisms such as custom tags and Spyce lambdas, Spyce gives power far exceeding what you can expect with plain PHP, enabling you to tackle demanding tasks that would otherwise be difficult without a framework. Following are a list of important tips and techniques that may be hard to figure out from the native Spyce documentation (which beginners are apt to find confusing to navigate). Neotitan's VPS hosting packages support mod_python and Python 2.5.x and will let you securely host Spyce-based websites with a minimum of fuss.

1. Use <% and %> instead of [[ and ]]

The Spyce documentation teaches usage of [[ and ]] as server script body delimiters, but Spyce will happily support the more familiar ASP/JSP style <% and %> delimiters which most people might be more comfortable with. For our development work here at Neotitans, we use this convention because it lets our favorite text editor, SciTE, color highlight the Python code within our Spyce scripts very nicely as shown below.

Spyce color syntax highlighting in Scite
click to enlarge

2. Accessing POST and GET variables in Spyce (.spy) scripts

Every Spyce script contains the implicit module request which contains the calls necessary for accessing the POST and GET variables sent by the browser. The simplest way to access these are via the calls request.get1("varname"), request.post1("varname"), request.getpost1("varname") and request.postget1("varname"). These return a string containing the value of the GET/POST variable.

request.getpost1("varname") will return the value of GET variable varname if that is present or alternatively, the value of the POST variable varname if that is the one present. request.postget1("varname") works in the other way.

The calls request.get("varname"), request.post("varname"), request.getpost("varname") and request.postget("varname") work exactly like the above but return the values of the GET/POST variables within a list. Use these when you are expecting multiple values within a single GET/POST variable.


request.get1("userid")     # returns value of GET variable userid as a string
request.post1("userid")    # returns value of POST variable userid as a string

request.getpost1("userid") # returns value of GET variable userid if present or
                       # of POST variable otherwise
request.postget1("userid") # like the last example, but checks in reverse order

request.get("userid")      # Works like the above calls, but returns the values
request.post("userid")     # of the GET/POST variables as a list of strings.
request.getpost("userid")  # Useful if you are expecting multiple values within
request.postget("userid")  # a single GET or POST variable.


See the Spyce request module reference page for more detailed info.

3. How to exit a Spyce script in the middle of code without showing an exception message

When you want to terminate your Spyce script execution somewhere in the middle of the code, you might be tempted to use sys.exit(). This will raise a SystemExit exception and will show an exception page, which is usually not what you want. The correct way to exit a spyce script cleanly in the middle of the code is to either raise spyceDone or just do a plain return.


if userid:
  print "Hello ",userid
  return    # will not print closing body and html tag below

See http://spyce.sourceforge.net/docs/docs-runtime_except.html for more detailed info.

4. Configuring Spyce to work with lighttpd and FastCGI

Step 1. Unpack Spyce into an appropriate directory such as /usr/local/spyce-2.1.

Step 2. Under the /var directory, create a directory /var/lighttpd and assign it to owner/group www.www

Step 3. Under the /var directory, create a directory /var/run/lighttpd and assign it to owner/group www.www

Step 4. Insert and modify lighttpd.conf in the apppropriate sections with the following lines:

## change uid to <uid> (default: don't care)
server.username            = "www"

## change uid to <uid> (default: don't care)
server.groupname           = "www"

#### accesslog module
accesslog.filename = "/var/log/lighttpd/access.log"

# files to check for if .../ is requested
index-file.names   = ( "index.spy", "index.html", "index.htm", "default.htm" )

#### fastcgi module
fastcgi.server     = ( ".spy" => (  "localhost" => (
                                      "socket"   => "/var/run/lighttpd/spy-fastcgi.socket",
                                      "bin-path" => "/usr/local/spyce-2.1/spyceCGI.py"  )

Step 5. As of Spyce 2.1.3, I needed to modify the following line in spyceCGI.py:

try: path = findScriptFile(environ['PATH_TRANSLATED'])


try: path = findScriptFile(environ['SCRIPT_FILENAME'])

in order to get fastcgi Spyce to work.

 Want to tip via bitcoin?

(address string below for double checking)

© 2021 by Andy Sy
last updated: 2008-Jul-17

Web Development / Rich Internet Applications (RIA) Development

Programming Languages


Database Development

VPS Hosting, Ruby on Rails hosting Philippines, RoR hosting, Philippines VPS and VDS
   © 2003-2019 Neotitans Technologies Inc.