Welcome to the "Web services for the Python developer" column. But wait, don't hesitate when we say "for Python developers." This column is not purely for the Python Guru, or even the Python journeyman. As much as possible, we will try to accommodate beginners and those who are merely curious. We intend to direct our readers to high-quality resources for the relevant background information necessary for each topic in this series. It is our hope, that in this way Python developers of all levels will be able to gain something from this column. In many cases, however, Python's legendary readability (it has sometimes been called executable pseudocode) means that most readers will be able to follow the code with little trouble.
And there will be much code. This column will be hands-on and very practical. In most installments we shall actually be developing a useful web Service, which you should be able to employ with little effort for your own purposes. So get out your snake-charming pipes and get ready to construct web services more productively than you might have imagined. In this inaugural article, we shall explore the many features and resources that make Python such a great programming language for developing web services.
Since you're in the web services zone, there's a decent chance you know what a web Service is -- if not, see the links in the Resources section for some good reading on the general topic. But in order to place Python within the world of web services, we ask that you indulge us while we step back and build a little perspective.
The web has developed at a remarkable pace since it's birth barely a decade ago. First came the original collection of interconnected articles and links. Then came CGI and with it the first glimpse of the web's potential to revolutionize software development: CGI provided narrow, highly-specialized functions for presenting dynamic content. From these roots have come web services.
One way to think about what sets web services apart from other dynamic web content is to think of application objects versus components. Objects are highly specialized bundles of code and data that are designed to drop into a sharply defined slot in an application. Components are also objects, but they are usually more generally applicable and highly reusable in a variety of applications and environments. Normally, they are associated with formal protocols (for example, CORBA, DCOM, and EJB [see Buzzward breakdown]) for communicating with other code, describing their interface to other code, and discovering and managing them at run time.
Similarly, web services are, at the most basic level, simply a subclass of dynamic web content, differentiated by their generality, reusability, and the formal protocols (SOAP, UDDI, WebDAV [see Buzzward breakdown]) that standardize their communication, interface definition, management, and so on.
Python has always had extensive support for basic Internet protocols, which along with its readability and ease of maintenance has made it a strong foundation for dynamic programming. Here we list some of the core facilities of Python that are useful for web services development. Everything listed comes with Python and requires no additional download or installation.
-
HTTP: The core library includes
full-blown HTTP 1.1 server implementations, including file and CGI servers.
These are very easy to customize for your own purposes and they already help
automate web serving tasks for you. Naturally, there are also tools
for making HTTP requests, including the low-level
httplibwhich gives you control over all aspects of the HTTP request, and the quick and dirtyurllibwhich simply takes a URL and fetches the data from the appropriate location. Note that Python also provides SSL support, which allows secure web sites through HTTPs. - URL parsing and construction: There are modules for breaking down and putting together URLs from more natural Python data structures. These make it easy for your web service to handle URLs with request parameters, fragments, and so on.
- CGI: There is a full suite of tools for interpreting CGI requests and making it easy to write effective CGI handlers.
- HTML (and SGML): Python comes with modules for parsing HTML and its parent language SGML.
- XML: Python has had a strong array of XML support throughout XML's brief history. A couple of XML parsers, DOM, and SAX libraries come in the standard library.
- FTP, SMTP, NNTP, POP, IMAP, and so on: The library is rich with client modules for these other important Internet protocols. Each one provides a very fine level of control over the particulars of the session as well as an impressive toolkit for parsing the data formats into more Pythonic structures. These come in very handy for gluing web services to other Internet applications; for instance, they provide event notifications via e-mail.
- Low-level sockets: Just in case you really want to get down and dirty with network programming, you can break out of the convenience of the above-mentioned modules and twiddle bits and bytes across the network any way you can imagine. The Python library comes with client and server socket programming modules.
-
Browser controls: Python
comes with a neat module,
webbrowser, for launching and controlling a wide variety of web browsers (including Netscape, Internet Explorer and Konqueror). This is a useful tool in for bridging traditional components to a web service in an Intranet or similar environment.
With this panoply of facilities, Python has the basics of web services well covered. However, we have mentioned the higher-level considerations that make web services special. These features are generally the preserve of third-party additions and applications for Python.
A brief survey of third-party Python tools
Beyond the "batteries included" approach in the Python standard library, there is a rich vein of third-party add-ons for web service development (and almost any other purpose you can dream up). See the Resources section for links to sites that will assist you in locating tools for use with Python. All software discussed here is open source.
XML is a very popular data format for web service purposes. Its high structure and extensibility, broad support, and variety of related standards make it popular for encoding requests, responses, and communications between sub-components of the service. We have already touched on Python's built-in XML facilities. There are also many third-party XML tools. The Python XML SIG develops the PyXML package which adds a variety of parsers, DOM and SAX tools, data-marshalling tools for WDDX and XML-RPC, and miscellaneous XML processing tools. There is also Sean McGrath's Pyxie for an original approach to XML processing.
An additional array of XML tools is provided in 4Suite (co-developed by the authors), which builds on PyXML and adds many XML-related facilities including DOM (transient and persistent), XPath, XPointer, XSLT, XLink, RDF, and XInclude. With these facilities alone, many aspects of a web service can be developed with little custom code required. In particular, RDF is the most promising technology for describing relationships and data that must be indexed for web services. Python has RDF support both in 4Suite and in James Tauber's Redfoot framework.
SOAP
is certainly the darling protocol of web services. SOAP is a protocol for
sending messages to remote systems using strictly-specified XML embedded
in HTML, SMTP, or other lower-level protocols. Pythonware's
soaplib
provides basic SOAP and XML-RPC support for Python. There are currently
some interoperability problems between soaplib and other popular SOAP implementations,
but a new release is imminent that promises improved interoperability and
broader SOAP support. There is also some rougher and older SOAP code available
from Ken MacLeod's Scarab project.
Fourthought's 4Suite Server (also co-developed by the authors) is an XML data server based on 4Suite that provides out-of-the box support for storing, managing, transmitting, and processing XML. It supports communications using CORBA, HTTP, and very basic SOAP, and will soon add more protocols such as WebDAV and SMTP.
Digital Creations develops Zope, a popular Python-based application server; Zope provides general object services, template-based HTML output, and WebDAV. XML support is in development.
Chuck Esterbrook's Webware is a suite of tools for developing web-based applications in Python. It is similar to Java servlets, JSP, and similar facilities.
Finally, since most users still access the web using HTML-only web browsers, being able to render HTML is an important part of most web services. DOM has some facilities for building HTML documents and 4Suite can output HTML from DOM or XSLT. If a template-based approach is preferred there is Robin Friedrich's very mature HTMLgen module.
Hopefully these resources get you started. Practice if you can because from now on it will be mostly real-world code solving real-world problems. In the next few articles in this column, we shall develop a practical web service: a software library service. You shall see how to put to work many of the tools we have introduced in this article.
- Participate in the discussion forum.
- Get an overview of the purpose of this series by reading the second installment.
- Read the third installment of this series.
-
Our fellow web services columnist Graham Glass has an excellent overview
of web services in his article "The
web services (r)evolution, Part 1."
- Python is a popular general purpose, application-development
language famed for its readability, sensible design, and comprehensive standard
library.
- Read Python
Programming for Beginners on Linux Journal. The Python documentation
comes with a tutorial.
Magnus Lie Hetland has tutorials for programmers
and non-programmers,
which have been translated in quite a few languages. Josh
Cogliati's tutorial is also a very gentle introduction.
- The Vaults of Parnassus
is an on-line database of openly available software for Python.
- The
faqts Python knowledgebase is a good place to get answers to all sorts
of questions about Python.
-
In some of David Mertz's developerWorks columns he covers XML processing
in Python. See
-
The Python XML SIG develops
the PyXML package,
which beefs up Python's native XML support.
-
Sean McGrath's Pyxie provides XML parsing
and processing tools, generally using a line-oriented approach that is
quite developer friendly. Mr. McGrath is also author of the book XML Processing
with Python, published by Prentice-Hall, February 2000 (ISBN: 0130211192).
- 4Suite and 4Suite
Server, co-developed by the authors, implement many XML-related technologies.
- soaplib is Pythonware's
SOAP implementation. Another SOAP implementation is Ken MacLeod's Scarab
project.
-
James Tauber's Redfoot
project is a Python-based framework for RDF servers.
-
Robin Friedrich's HTMLGen
provides a template-based approach to rendering HTML documents.
- Zope, by Digital Creations, is a general purpose
application server for Python, and provides a variety of web services features.
-
Chuck Esterbrook's Webware
provides a set of web development tools that will be familiar to users
of ASP, Java servlets, JSP and the like.
Mike Olson (molson@fourthought.com) and Uche Ogbuji (uche@fourthought.com) are co-founders of and principal consultants at Fourthought Inc., where they develop the open-source tools 4Suite and 4Suite Server , and provide commercial consulting, training, and 4Suite customization for clients working with XML and Web services. They reside in Boulder, Colorado.

Uche Ogbuji is a consultant and co-founder of Fourthought Inc., a consulting firm specializing in XML solutions for enterprise knowledge management applications. Fourthought develops 4Suite, the open source platform for XML middleware. Mr. Ogbuji is a Computer Engineer and writer born in Nigeria, living and working in Boulder, Colorado, USA. You can reach him at uche@ogbuji.net.




