Python is 100% enterprise ready, with powerful database and XML support, and even a full CORBA implementation. But it's virtually impossible to find a book that shows you Python at work in a real enterprise environment. Web Programming in Python fills that gap, demonstrating exactly how to build robust, Web-based systems that integrate Python with other key open source technologies, including Linux, Apache, and MySQL. The book begins with a quick introduction and self-standing reference to Web programming in Linux environments using Python and Apache. You'll then review Python CGI programming from the ground up, discovering how to generate clean HTML code without adding unnecessary complexity to your code. Once you've mastered the fundamentals of Python Web development, the authors introduce threading, Linux networking, and other advanced techniques for maximizing the program robustness and scalability in. Finally, using a start-to-finish case study, you'll push web technology to its limits, as you build a powerful XML-based, database-driven application that utilizes CORBA distributed components.
From the Back Cover
Enterprise-class open source Web development step-by-step, start-to-finish!
- Build production-quality Web applications with Python and open source technology
- Includes in-depth coverage of Python's intrinsic support for CGI programming
- Demonstrates efficient template processing using a powerful yet simple module known as the Write Processor
- Contains complete tutorials on Python, Apache Web Server, and MySQL
- Features start-to-finish Web application case studies based on Slither, a new Python-based portal application development platform
Python is 100% enterprise ready, with powerful database, network, and Internet support. Now there's a book that shows you Python at work in a real enterprise environment. Web Programming in Python demonstrates exactly how to build robust, Web-based systems that integrate Python with three powerful open source technologies: Linux, Apache, and MySQL. Coverage includes all this, and more:
- Self-contained Python tutorial: from the basics to advanced object-oriented and metaprogramming techniques
- Fundamentals of Web development in Linux environments, covering file systems, processes, networking, and HTTP (the protocol of the web)
- Network setup and Apache configuration, including the configuration of Secure Sockets Layer to support HTTPS
- Python CGI programming: generating clean HTML code without unnecessary complexity and techniques for separating HTML from your business logic
- An effective logging framework to support server-side debugging without using a debugger
- MySQL: An immersive introduction to SQL and the book modules to integrate data access in your applications
- Object-oriented techniques for form processing and powerful template processing using the Write Processor module
In an extensive case study section, you'll discover the new open source Slither Application Development Framework, and use it to build two powerful collaboration and e-commerce applications.
If you're ready to use Python and open source in a real production environment, one book delivers the techniques and code you're looking for: Web Programming in Python.
The software in this book is distributed under open source licensing terms and is available freely from http://www.toolsofcomputing.com
About the Author
GEORGE K. THIRUVATHUKAL is a principal in Zirve! Technologies LLC and is visiting associate professor at Loyola University. He also holds an adjunct professorship at Northwestern University in the ECE department and its MITP program. He is an active researcher and developer in distributed systems, programming languages, and operating systems.
JOHN P. SHAFAEE is a software developer at Hostway Corporation in Chicago, Illinois, where he leads and develops projects with Python on the Linux platform. He is an active researcher in distributed systems and cluster computing.
THOMAS W. CHRISTOPHER is a principal in Tools of Computing LLC, a Chicago-area consultancy specializing in high-performance computing and object-oriented languages. He authored Python Programming Patterns and, with George K. Thiruvathukal, he co-authored High-Performance Java Platform Computing. Christopher was formerly a professor at the Illinois Institute of Technology.
Excerpt. © Reprinted by permission. All rights reserved.
1. Introduction
The Web is (still) a great place to be. At the time of this writing, there are millions of Web sites, and the number is anticipated to keep growing for decades to come. While industrialized nations comprise most of the Internet as we know it today, this is destined to change. Web technologies appear destined for true internationalization, and we believe the Web is well on its way to becoming an international phenomenon.
So why have we written a book about the Web? Why Python? Why Linux? Why Apache? And why a book that covers these and other open-source technologies (including our own) all in one text? We undertook the writing of this book when we noticed that one of the dominant platforms for hosting Web sites has a scarce number of quality books that address how actually to implement working commerce sites using the technology. This is not much of a surprise, since each technology that supports a Web strategy typically requires an entire book (or several) just to cover the technology adequately.
This book aims to be unlike any you've ever read about the Web. It is both a general reference book (the first half) and a collection of patterns and techniques for developing real Web applications/application servers. Each chapter covers a specific aspect of creating such applications and is organized toward building a real Web site, all in the comfort and safety of your own Linux box. Yes, you will not even require a Net connection should you wish to develop a Web site on a confined personal computer. Before we get to that point, however, a few words are in order about Linux, Python, and the Apache Web Server.
Linux
Inertia is the word du jour. Linux has established itself as major player due primarily to inertia. Hillary Clinton has said that "It takes a village to raise a child." While one might be inclined to dismiss this as rhetoric spoken by a politician, there is something to be said for it. Linux has every reason to be taken seriously because it has a very large, committed community that is determined to nurture it and ensure its success. For many of its followers, success for Linux has little to do with economic success. Success is defined by its continued advancement as a serious computing platform. And this success can only come about when developers join the effort to help in areas where help is needed. Because there is plenty of community from which talent and expertise can be drawn, Linux does continue to improve.
Why does the community care so much about Linux? This question is more than rhetorical. These authors have all struggled with this seemingly eternal question after repeated reboots of their personal computers running a particular operating system whose name begins with letter "W" that descends from an operating system beginning with the letter "D." The creator of Linux, Linus Torvalds, created the first version of Linux to make it possible for him to use an OS for which he would not have to pay a licensing fee. His primary need was to have a great cost-effective platform for developing systems software, including the operating system itself. We thank him for aspiring to this noble goal; otherwise, there probably would not be any serious alternatives, since most of the alternatives are proprietary and those that are nonproprietary (such as BSD) were not designed expressly to run on PC hardware (a somewhat daunting task) and were positioned at the higher end inhabited by servers and supercomputers.
Almost as soon as Linux was stabilized it became apparent that it was going to have to become something better than an OS for developing operating systems software. We three authors each have a geek coefficient near 1.0 and really enjoy hacking on systems (like Linus); however, such people are in the distinct minority of those who use computers. And thus Linux began the long, evolutionary process toward presenting a serious alternative to "W" on the desktop. To that seemingly insurmountable goal, the community has rallied to produce replacements for just about every single thing found on other operating systems. Most notably, great progress has been made on the desktop. There are excellent window managers, desktop applications (editors, word processors, spreadsheets, presentation graphics, HTML editors), development tools (Java, C, C++, Python), clustering software, server software, and much more available for Linux. Not long ago, when software was developed on the Unix platform, it was usually developed on Sun's operating systems, including Solaris. The landscape is completely different today-almost all applications are developed first on Linux and then ported to the other Unix variants.
Speaking of other Unices, Linux is probably not the best Unix implementation. One of the authors (GKT) has worked extensively with many different versions of Unix. Each has its strengths. Sun Solaris is a very nicely done operating system that has even been ported to Intel hardware, long before Linux was a robust OS with serious applications. It has always been a great server-class operating system, with an excellent threads model for exploiting multiprocessing capability. The development tools have always been among the best. IBM AIX is also a pretty decent operating system. Again, it is an OS positioned for the server side, with excellent support for multiprocessing (both SMP and multicomputing), high-performance file systems (e.g., the Journaling File System, JFS), and excellent power management on the IBM workstations. Even SGI and HP have ultimately been able to produce decent Unix implementations with advanced features, although both have a history of somewhat unstable releases. BSD has always been a good Unix implementation, again positioned more for server class machines.
None of this might seem important, except for one detail: Linux was not initially positioned as a server operating system; the others were. It started with a desktop focus but in fact has not been perfected for either focus. The desktop is somewhat clunky compared to the "W" or "A" environments, while the server aspects are still somewhat lacking, especially to exploit the power of bigger iron machines, such as symmetric multiprocessors. Nonetheless, the picture is changing. There is nearly continuous activity to make Linux a serious "clustering" option (ongoing at the time of writing), and thread support is improving.
It might seem like a misstep to write a book on a topic and, at the onset, send a mixed message. We think an informed user is a power user. Linux has a great deal going for it. It runs practically nonstop and appears to be getting better. In 1998-99, two authors (GKT and JPS) had Linux up and running for almost 150 days in their university laboratory before it was brought down. The cause: a power outage. This is a very strong point in its favor. Not that the other mentioned Unices cannot boast a similar record, but they should! After all, Linux is free.
A second advantage is the ability to experiment. Linux can be used to do some pretty crazy things. Usually, the people doing the crazy things are willing to share. For example, at home the authors all have their Linux boxes configured as routers. Each has a LAN connected to a DSL or cable modem. Linux can be configured to allow all of the computers (Linux and non-Linux) to share the connection, using something called ipchains. This will be covered briefly in one of the chapters, since it proves indispensible for developing and testing your Web site. This capability might not seem like a big deal but it really is a big deal. You would have to spend mucho dinero to purchase a router or software to perform the equivalent task on Windows. Worse, you would not be able to customize the sharing nearly as well. The ipchains software can even be used to forward incoming requests to another computer. Linux gives us unprecedented power to do cool things, and at zero cost. It is hard to envision more of a killer app than routers, which made the Internet what it really is today (and companies like Cisco very rich!)
Python
So the next question is, "Why Python?"
Every so many years we all find ourselves asking that question. The whole idea of creating any language in the first place is to communicate. Whether this communication is taking place with another human or a computer is not the question. In the computing field we are just learning to communicate intelligently. The new millenium brings many challenges in software engineering that are going to require higher performance design, development, and maintenance techniques. Python might well be the first in a series of languages dedicated to bringing about a needed change in how software is developed.
Let's take a look at why Python is so great.
Interpreted Environment
We believe that the most attractive feature of Python is that it is an interpreted language. This means that you can experiment with the Python languages and libraries without even writing a complete programa technique called rapid prototyping-which is extremely helpful from a learning standpoint. This is reminiscent of the early BASIC and Lisp programming languages, which also provided this capability.
Interpreted languages are ideal for Web application development. The reason boils down to this: Web application development is still very experimental. Unlike development of graphical applications on the Mac or Windows, the concepts of reuse and patterns are just beginning to take form. The ability to "see whether something will work" requires the ability to experiment. It requires the ability to do something interactively and see whether the desired result is achieved. And it requires the ability to do it quickly. Interpreted environments have always had the advantage of shortening the life cycle of software development, since the compilation step is eliminated. Performance is typically slower than that of a compiled language, depending on what you are doing. The fundamental performance differences (as we discussed for Java) are in handling of scalar data, which often run 10 to 100 times slower than native code. However, in handling higher level structures such as arrays, lists, tables, and strings, the performance differences begin to fade. In Python, the performance of these higher level structures is very good, especially when considered in the context of the overall environment. Scalar performance is not that great. However, as the Web is largely a story about working with text, computational performance is a secondary consideration.
Rapid Prototyping
Python may well be the first language really to deliver on the promise of rapid prototyping. With Python one is provided a language with very high-level syntax and semantics. This is perhaps best illustrated by example. Let us consider the Java versus Python solutions for building a list of values:
myList = 1, 2, 3.0 ;
In Java this is done as follows:
Vector myList = new Vector();myList.addElement(new Integer(1));myList.addElement(new Integer(2));myList.addElement(new Double(3.0));
This example highlights a key deficiency of Java: code bloat. The bloating factor is much greater when you consider that the Python code (as shown) can immediately be executed as a full Python program, whereas the Java code must be augmented with a class definition and main method, both of which are legislated by the Java language specification.
Rapid prototyping refers to the ability to go from conception (the idea) to realization (working software with tolerable performance) in the shortest possible time. The ideas of Python are not entirely new. Lisp, other functional programming languages, and Icon all are roots in the language tree leading to Python. The remainder of the points, however, illustrate just how different Python is from these languages.
Object-Oriented Optional
Python more closely follows the object model found originally in the Smalltalk environment. Thus it is possible to use Python as a pure object-oriented programming environment; however, unlike Java, the use of objects is not legislated. In other words, the first thing a programmer does not need to learn is the notion of classes and objects. This enables an entire class of applications that don't need objects to be developed (usually called scripts).
Although the use of classes is optional, in a sense this is a white lie. When you write Python code, you will be making use of built-in data types and high-level data structures. All of these types, in fact, are objects. The earlier example of list constructor syntax (1, 2, 3.0) builds a list object in Python; however, the key difference between Python and Java is its impact on users. In Python, users are shielded from the innards of objects until there's an actual need to define a general class of objects; Java forces you to think about very low-level considerations from the get-go, which comes as a surprise to most programmers, who have heard that Java is a simplification of C++. It may be simpler for compiler writers to implement the language, but the language in many respects is harder to program in.
Dignified Access to Legacy Code
Python's single greatest contribution may not be any of the aforementioned topics. It may well be the elaborate extensions framework that is provided that allows you to access legacy code, say, that was written in C, C++, and even FORTRAN.
The approach taken by Java is that most code virtually needs to be rewritten in Java. Although Java provides the JNI (Java Native Interface), the framework performs relatively poorly when compared to Python. The integration of native code with Java requires you to have innate knowledge of the peculiarities of Java's garbage collector. Python and its libraries are usually written in the C language (with some C++). Like Java, Python prohibits the direct manipulation of pointers in native code; however, unlike Java, Python makes it very easy to integrate codes. At the time of writing, tools exist to extract the interfaces from a C or C++ code and generate the Python wrapper. Similar tools are under development for Java but require extensive manual intervention.
This book will not discuss the issue of extension modules in depth, opting for a pragmatic approach; however, the extension modules have enabled Python to surpass Java in many respects, since many useful C and C++ libraries can be directly leveraged in a safe and platform-independent way. Furthermore, excellent performance is achieved.
True Portability and Write Once, Run Everywhere
This should not come as much of a surprise.
Python is the truly portable language that delivers on the write-once run everywhere principle. At the time of this writing, Java's portability has been achieved on two platforms with the Java 2 Enterprise Edition: Windows and Solaris. This does not mean that Java fails to work at all on other platformsit often doesn't work as well. The personal and embedded Java products are very stripped-down versions of the core Java language. Microsoft and HP have developed their own versions of Java that are incompatible with Sun's Java.
Python is not owned by a company and thus has a community that is committed to its ubiquity and evolution, in much the same fashion as Linux. What may come as a surprise is that Python has already achieved much more portability than Java. In fact, there is a version of Python that is implemented in Java (JPython), which in practical terms means that you can embed Python (the core language and runtime) in a Java program (something you might want to do if you're developing Web applications in Java proper) to take advantage of its scripting facilities, say, in a Servlet (we will show how this can be done). Python runs on dozens of tested platforms, including all flavors of Unix (not an easy feat), all versions of Windows 3.2 (there is even a graphical development environment called PythonWin), Macintosh, and many others (BeOS, QNX).
Of course, write once, run everywhere is best for situations where you actually want it. The truth of the matter is that Unix and Windows programmers alike are virtually united on this issue: They don't want it most of the time. It is often the case that you need platform-specific functionality. This is especially true in embedded computing, where you may have specific devices, and in systems programming, where you may need to make specific system calls, etc. Python allows you to step outside of the write-once run everywhere box when needed, and it will not punish you for it, unless you blatantly violate the rules.
Ready for the Enterprise
Python is 100 percent enterprise-ready. Before we even got a chance to breathe, Python had already evolved sophisticated database support (modules for MySQL, Oracle, and Postgres are alive and well). XML is available with full support for the DOM and SAX parsers. CORBA (a distributed object computing framework) and XML/RPC (a remote procedure calling system) are both available for Python. One is hard- pressed to find a language that is more ready than Python. From our early experiments, we have yet to encounter bugs.
Why Apache?
The Apache Web Server is a free Web server that runs on most versions of Unix and, recently, Windows NT and 2000. It is a commercial-grade server that was derived from the NCSA httpd implementation. Apache is a very popular server, especially with Web hosting companies, particularly due to its rich feature set. At this writing, it is used by approximately 60 percent of the Web sites out there.
Despite Apache's popularity, configuring it and taking advantage of its special features is not completely trivial. The documentation can and will get you to the answers but is really written by and for system administrators. There are other books on Apache proper; this book does not strive to be the quintessential Apache reference manual. Instead, we focus on a number of useful activities to get you to the point where you can host (or have someone else host) a fully functional Web site and take advantage of Linux and Python as well.
Apache is a great Web server, particularly from a developer's point of view. There are a number of features we plan to employ in this book:
- Logging
- CGI scripts
- Plug-in scripts
- Virtual hosts
- Running multiple instances
Of course, features themselves are relatively meaningless unless they support a development process. As an example, consider the notion of a virtual hosting environment. Support for virtual hosts is addressed by the Apache Web Server using the VirtualHost configuration section. Virtual hosts are extensively used in Web hosting companies to make your domain (http://www.domain.com) look like it's running on its own machine. There are different ways to support the notion, but in real hosting environments, hundreds (if not thousands) of domains may actually be hosted on a handful of machines.
There is another opportunity for virtual hosting, and that is to support the notion of a development sandbox. It is fairly straightforward to create a development sandbox, even on a machine that isn't connected to the Internet. The concept is used in most software development organizations at one stage or another in the development process. For Web programming, it is indispensible. This example, among others, will show how the Apache discussion is structured. Even if you already know it, you might benefit from reading about how we use it.
What About MySQL?
MySQL is a great database. We chose MySQL primarily because of its excellent overall balance between ease-of-use and power. MySQL is a full relational database. One of its particular attractions is that it does not pretend to be an enterprise database server (Oracle, Informix). There is nothing wrong with these databases, but they are not freely available and are not particularly easy to manage. We are not trying to discourage you from using these enterprise servers; however, unless you have hard-core data management needs, it is unlikely that you will benefit from them.
We wish to point out that we have considered the use of PostgreSQL. In fact, our data access and record set processing classes have all been tested with PostgreSQL. PostgreSQL is probably better than MySQL. In fact, we are pretty convinced from a technical point of view that PostgreSQL can give Oracle a run for its money; however, we are not crazy about it from an administrative or lightweightedness perspective. Nevertheless, there are things you get from PostgreSQL and enterprise databases that are not available in MySQL: referential integrity, triggers, object/relational support, etc. We are not convinced that these features are must-haves. Although argued to be relevant for years, they are seldom used in practice. One of the authors has actually worked in two environments where Informix and Oracle were used in significant, large-scale database applications. In these applications, features such as referential integrity, object/relational support, and others (such as parallel query execution) were seldom, if ever, used. And, ironically, the result is one that can best be summarized as a disadvantage. A big server solution is being used for managing a few tables. A sledgehammer is being used to hang up a picture on your wall. Any questions?
MySQL has been reported to scale up to tables having millions of records. So has PostgreSQL. Both are freely available. Unless you foresee having more than a million records in your tables, you are unlikely even to flex the muscles of MySQL or PostgreSQL.
If you don't want to take our word for it, consider many of the major sites that are still in business: Freshmeat, Slashdot, and SourceForge. NASA has even recently switched many of its servers to run MySQL from Oracle. If it is good enough for NASA, which has gone to the moon and the rest of the solar system, it is good for the world (and other worlds!).
Book Contents
This book aims to be a self-contained reference on building Web applications entirely from free software: Linux, the Apache Web Server, and Python. It is organized in two parts: Foundations and Applications. The intention is to spend equal time on each part.
Part I: Foundations
Without foundations, there is no discipline. The Web today is built around a number of interesting (cool) technologies with few documented patterns and development techniques. Patterns and techniques are essential building blocks of a software development process. In this section, the focus is entirely dedicated to developing principles, patterns, and techniques from the ground up to support the next part (applications). We will discuss each of the major topics (chapters) to be included and their purpose.
Chapter 2: Introduction to Python
This book assumes you are a practicing programmer who probably has some experience with Perl or Java. As we have chosen Python as the teaching (or learning) language for this book, we have divided the discussion into two chapters. This is the first of the two chapters, which is a self-contained reference and basic guide to the language. Python itself has a number of books devoted to it, and a number of good tutorial documents available on the Web. This chapter will provide everything you need to get bootstrapped with Python quickly.
Chapter 3: More Advanced Python
Python has foundations in a variety of language paradigms, including functional, object, procedural, and very high-level programming. It is not just a scripting language. The language also features a sophisticated extensions framework that allows you to grow the language. In this chapter, the functional and object-oriented aspects of Python are presented in detail. We also present the concepts of namespaces and metaprogramming, which represent two of Python's core language features (features that are not commonly found in other programming languages to the degree that they're supported in Python). In many of the WPL programming modules, we will make use of these advanced features. This chapter will be useful as a reference to the features as they are used in actual code.
Chapter 4: Introduction to GNU/Linux
Linux is a Unix-like operating system. Although it is well beyond the scope of this book to cover Linux in detail, we cover it at two different levels. First, we provide a more or less self-contained introduction to the basic principles behind Linux (and Unix, for that matter), and the essentials of using and managing it. We have chosen to limit this discussion to the command-line environment, as this is more representative of the kind of environment in which you will encounter Linux, especially if you choose a Web hosting provider to develop your site.
Chapter 5: Introduction to Internetworking and HTTP
This chapter provides a brief overview of networking and the essential principles needed to do serious Web development. This chapter will be useful to those who are new to but somewhat familiar with networking principles, and as a comprehensive overview. Networking protocols, addressing, routing, and basic TCP/IP services are covered. You will want to read this chapter to get a perspective on the HTTP protocol without having to pore over complex jargon and code examples. The CGI chapter will assume complete familiarity and detailed understanding of the HTTP protocol discussion.
Chapter 6: Network Setup and Apache Configuration
The Apache Web Server comes preconfigured with most of the major Linux distributions out there. However, as with most preconfigured software, somebody thought they knew what was best for you. Worse yet, something you wanted wasn't installed and you ended up having to do everything over again. Such is the nature of working with today's Linux distributions. This chapter will cover the basic installation and configuration of Apache and make you aware of its more advanced options, with the ultimate goal of getting a basic working setup established that allows you to view a collection of pages and to run Python CGI scripts. We will also talk about typical setups that will work for (1) home networks connected by DSL or cable modem, (2) a dedicated or co-located host, and (3) a shared hosting provider that allows you to run your own scripts.
Chapter 7: CGI Programming in Python
Python contains innate support for Common Gateway Interface (CGI) programming. This chapter provides an overview of the CGI module and how you can use it directly to write Web applications. As will be shown later in the book, we provide a layer of abstraction above this module, which allows you to develop applications in a more modular fashion. Nonetheless, an understanding of CGI is essential to developing for the Web as it is today and is likely to be for years to come. (This is true even if you program in so-called application frameworks like Servlets, which are based on the same programming model as CGI that deals with explicit HTTP requests and responses.) This understanding will also help you to see the value in more project-oriented frameworks such as Slither, covered in the last part of this book.
Chapter 8: Database Essentials Featuring MySQL
Database support is freely available for Linux, freeing you from the licensing fees typically associated with other commercial RDBMS. This chapter will introduce the basics of working with MySQL (one of the freely available databases) and address the issues of basic table definition, inserts, updates, deletes, table joins, and transactions. We will show how the ability to interpret Python on the fly can be used to evolve a stored procedure framework that, remarkably, makes a strong case for Python as a database extension language.
Database access is beginning to be standardized in much the same fashion as ODBC and JDBC in the Microsoft and JavaSoft worlds, respectively. With Python 2.0, a new database specification has appeared. While these specifications are on the right track, actual applications require the ability to operate at a higher level. We introduce the DatabaseFactory, a library designed to support our development with MySQL and Postgres in Python. We also introduce the Record class, which is used to support result sets.
Part II: Modules
Chapter 9: Template Processing with the Write Processor
XML looks like a technology that will radically change the world of Web and enterprise application development, and we use it in this book for some aspects of developing Web applications. However, HTML is here to stay. Web interfaces are typically envisioned by a concept team that dreams what the application could look like (without necessarily being functional). The development team puts "skins" onto the actual code. This is very much a template-driven approach and requires the ability to easily hook the interface (often consisting of a number of chunks of HTML) into the application code. In this chapter we present a sophisticated template processing framework (the so-called Write Processor) that allows substitutions to be performed, including simple one-time variable substitutions, include processing, and looping substitutions, which are pattern-oriented so that a collection of variables on one or more lines of template text can be substituted repeatedly using an implicit iteration on an underlying Python data structure. We show the beginnings of a general-purpose (yet customizable) approach for generating just about everything HTML and XML from basic building blocks, which is expanded further in the second half of the book.
Chapter 10: Form Processing with WebForm
WebForm is a much friendlier interface to the CGI module that automatically dispatches Python objects and binds them to a set of form variables. This abstraction will be a key building block for the PyCart portal library, which can be used to build virtually any Web commerce application.
Chapter 11: Debugging and Logging Techniques and Tools
Web programming, more so than other kinds of programming (e.g., stand-alone programs), suffers from a woefully inadequate environment for debugging or, more important, finding bugs. Increasingly, there is a perceptionaddressed by a recent article: Log4J (a Java-based logging framework) http://jakarta.apache.orgthat debuggers are the wrong thing for Web applications and that logs tend to provide more useful information to aid in problem resolution. We have worked out a framework for logging and debugging CGI programs. Applications can log events (easily) using the wpl.logging module. Unlike typical logging, wpl.logger supports the notion of event contexts, attributes, and details, all of which can be used to direct searches for success (or failure) events of interest.
Chapter 12: Miscellaneous Utility Modules and Classes
This chapter describes a number of useful classes and modules that we developed that simply did not fit in any chapter but nevertheless are used extensively in our Slither foundation class library and the applications developed toward the end of the book. Some of these modules are used to encapsulate useful utility functions.
The Path module is used to automate the construction of the Python path by allowing it to be constructed from a configuration file. As those who program in Java and Python already know, object-oriented programming has introduced one unpleasantry into programming: setting environment variables. Our aim here is to automate this as much as possible using the Path class.
The Trace module is used to provide very detailed traceback information, not only when issuing error messages, but when logging failure events.
The Directory class is a general-purpose, scoped data structure that is used in a number of different situations but most notably to keep persistent state information for different functions in a Web application with proper namespace management. It is the same concept as the Unix or Windows file system: You can store objects in the directory and create pathnames to refer to different subdirectories and entries within directories. The only wrinkle is that it is an entirely memory-resident concept. Every operation found in the file system is supported in the Directory class with the exception of permissions.
Finally, the KeyGen module is used for generating unique keys (identifiers). This is used for creating unique session names to index the persistent state information from disk. You'll hear a lot about this in the Slither chapter (and Slither applications) when we discuss something called the user profile.
Part III: Applications
Chapter 13: The Slither Application Development Framework
The Slither foundation classes are an implementation of an application development framework for Python Web programming. This class library allows you to write an application without being mired in the excruciating details typically associated with CGI programming. This class library makes extensive use of the Python language (in particular, its metaprogramming features for discovering things about your code), and both Python and our own class libraries as presented in the book. The key to our approach is lightweight design and flexibility and the ability to easily integrate HTML interface code in your application without ever directly mixing the two. We will strive to convince you (but have already witnessed how) this approach improves productivity when it comes to designing, implementing, and maintaining application code, especially over the long haul. Our approach is compared and contrasted with other frameworks available in the marketplace today toward the end of the chapter.
Chapter 14: Slither Applications
This chapter presents a "starter" application and two significant (i.e., nontrivial) applications written entirely using the Slither foundation classes and other core classes, such as the WriteProcessor. The two applications are very representative of the kinds of applications gaining attention on the Internet after the dot-com crash. The first, SlyWiki, is a collaborative text authoring system. We used Slither here to enhance the functionality of an existing tool called MoinMoin and enable other useful extensions. SlyWiki features a database to store all user, page, and access control information, page-related operations (creating, viewing, and editing), indexing, searching, file upload/download, and many other features.
SlitherShopping is a do-it-yourself shopping portal that allows a business to create its own online presence. It features both a customer and an administrative interface and, similar to SlyWiki, features an access control framework. Both of these applications are completely functional and can even be deployed securely. While companies have already burned through millions of dollars in venture capital trying to get people to use their proprietary versions of the same software, you can download these applications and extend them for your own needs.



