CGI s Special Environment Variables

Your script might find more than 20 special CGI variables in its environment. The important ones are covered a bit later, but first look at a very simple CGI script that gives you the tools you need to explore the variables yourself. It's called PrintEnvironment.cgi The cgitb module will give you exception reporting and stack tracebacks in your web browser, similar to what you see when a command-line Python script throws an exception. It'll save you from getting mysterious 500 error codes, and...

Creating a Real Glade Application

Of course, the Glade version of our two-button application doesn't do anything, any more than the version that just used Python code did. In this section, we'll create a complex GUI, with some signal handlers, for an application called pyRAP. This is a chat-themed GUI that could be used as a client for the Python Chat Server described in Chapter 16. Create a new Glade project called PyRAP, and create a new window as shown previously. To create a basic GUI, start with a Vertical Box widget, also...

Further Enhancing PyRAP

Tree Views can quickly become quite tricky. Not every column needs to be visible to the user. In some columns, you may want to store references to the actual object being displayed. For example, if you had a Server class, you could specify a Server object as an invisible object on the line, and if the user doubleclicks on that line, we can pass that information on to the Server object itself. The trick to doing this is to set_value with a number and then never use that number in insert_row as...

How It Works

This script is almost the same as the createtable.py script from Chapter 14. This shows the freedom the Python DB API gives you, as you are not tied to any one database vendor. Other than the code to establish the connection to the database, you'll find your database code can work with multiple databases. To establish a connection to HSqlDB, you can use code like the following from com.ziclix.python.sql import zxJDBC Modify as needed for your database. db zxJDBC.connect(url, user, pw, driver)...

Introducing Wish ListBargain Finder

Amazon lets individuals and booksellers advertise their used copies of books on their site, and Amazon presents the lowest used price for a book alongside its own price for a new book. If you look back at that XML search result for James Joyce, you'll see that A Portrait of the Artist as a Young Man is available new from Amazon for 8.10 (OurPrice), but there are also people selling used copies for as low as 1.95 (UsedPrice). That's a pretty good price, even when you factor in shipping. Many of...

MIME Multipart Messages

This isn't quite the e-mail message described earlier. That message was a short piece of text (Here's that picture I took of you.) and an attached image. This message is just the image. There's no space for the text portion in the body of the message putting it there would compromise the image file. The Content-Type header of a mail message can be text plain or image jpeg it can't be both. So how do mail clients create messages with attachments In addition to...

The cgi Module Parsing HTML Forms

When you click one of the Submit buttons on SimpleHTMLForm.html, notice that you're not exactly GETting the resource cgi-bin PrintFormSubmission.cgi, the resource specified in the action attribute of the < FORM> tag. You're GETting a slightly different resource, something with the long, unwieldy identifier of 2& button Submit. This is how a GET form submission works The web browser gathers the values of the fields in the form you submitted and encodes them so they don't contain any...

The Twisted Framework

The Twisted framework is an alternative way of writing networked applications in Python. While the classes in SocketServer are designed around spawning new threads and processes to handle requests, Twisted uses a loop over the select function (as in the client example above) to timeshare between all pending processes. Download the Twisted framework libraries from the Twisted web site at http twistedmatrix. com projects core , and install them. For simple applications, programming in Twisted can...

Working with Cursors

A Cursor is a Python object that enables you to work with the database. In database terms, the cursor is positioned at a particular location within a table or tables in the database, sort of like the cursor on your screen when you're editing a document, which is positioned at a pixel location. To get a Cursor, you need to call the cursor method on the Connection object Once you have a cursor, you can perform operations on the database, such as inserting records. Enter the following script and...

Working with the Python DB API

Once you have a connection, you can use the same techniques shown in Chapter 14. The zxJDBC module provides a DB 2.0 API-compliant driver. (Well, mostly compliant.) For example, you can create a database table using the following code cursor.execute( create table user (userid integer, username varchar, firstname varchar, lastname varchar, phone varchar) index userid on user (userid)) After creating a table, you can insert rows using code like the following insert into user values Be sure to...

The Xmlrpc Introspection API

An unofficial addendum to the XML-RPC specification defines three special functions in the system namespace, as a convenience to users who might not know which functions an XML-RPC server supports, or what those functions might do. These special functions are the web service equivalent of Python's ever-useful dir and help commands. Both SimpleXMLRPCServer and CGIXMLRPCRequestHandler support two of the three introspection functions, assuming you call the register_introspection_functions method...

If Something Goes Wrong

A REST web service is expected to flag error conditions using HTTP status codes, in conjunction with error documents that describe the problem. As you might expect, XML-RPC does a similar thing in a more structured way. If an XML-RPC server can't complete a request for any reason, it will return a response containing a fault, instead of one containing a return value in params. A sample fault response is as follows lt xml version 1.0 gt lt methodResponse gt lt fault gt lt value gt lt struct gt...