Tip

Notable Python language update on view

I last took a look at Python (in version 2.4) for web service clients several years ago. As a scripting language, Python is popular for utility quick projects and comes pre-installed on many systems. Python scripts,

    Requires Free Membership to View

identified by the .py file type, turn up in many places. Python is available for a huge variety of platforms. The basic language is completely open source but the license permits the development of custom commercial versions. Sun is putting a major effort into the development of Jython for running Python programs on the Java Virtual machine with access to Java libraries. Many developers comment that their Python projects get to working code surprisingly quickly.

A major effort by the Python developer group has created Python 3.0, released in late 2008. Major changes to the handling of strings (now Unicode) and cleanup of obsolete methods mean that backwards compatibility to Python 2 programs has been lost. As a parallel development, Python version 2.6 has also been released at the same time with the intent of creating an easy migration path for Python developers to eventual use of version 3.0. Google's App Engine, which lets you run your web applications on Google hardware, currently supports Python version 2.5 but is being updated to 3.0. Since many of the Python developer community, including Guido van Rossum, the original author of Python, work for Google, this update should occur rapidly.

Support for Networking

The Python standard library comes with extensive support for networking, from socket level to HTTP and email protocols. The socket tools support both IPv4 and IPv6 addressing. The low level protocols UDP and TCP are supported and the standard library contains support for creating your own custom servers.

A large variety of Python based web application frameworks have sprung up, but the Python community appears to have settled on WSGI, the Web Server Gateway Interface, as a unifying specification. Commercial and open source frameworks supporting WSGI include the Google App Engine. Generally these frameworks have not been completely updated to Python 3.

XML Support

Python 3 supports the W3C DOM model for XML at the "level 2" recommendation. There is also a "lightweight" DOM implementation corresponding to the W3C level 1 (circa 1998) without the added complexity of namespace support. A rather clever "pulldom" API provides for creating partial DOM structures during SAX parsing, especially handy when working with very large XML documents. Primitive parsing functions are not written in Python but use the "expat" parser written in C for speed. Expat is a non-validating parser. Limited XPath support exists but is not well documented.

The Python community does not appear to be too enthusiastic about creating support for SOAP, WSDL and WS-* standards, although some projects exist. REST style web services would appear to be a better fit for Python given Python's emphasis on simplicity.

Python and JSON

JSON, short for JavaScript Object Notation, support in the Python standard library is new and appears in both version 2.6 and version 3.0. JSON has become many developer's technology of choice for communicating data to Rich Internet Applications (RIA). JSON is compact and easily accessed in browsers with code similar to that used to evaluate XMLHttpRequest results but cannot communicate complex hierarchies of data like XML can.

The JSON library lets you serialize the data content of a Python object, for example in response to a browser request. Furthermore, you can populate Python data structures from JSON text. The Google App Engine provides extensive support for creating and using JSON formatted data.

Python and XML-RPC

XML-RPC is the simple protocol for executing remote procedure calls using XML encoded messages. This is, of course, the idea which accumulated lots of extra complexity and evolved into SOAP. XML-RPC implementations are available for many languages in addition to Python, including Java and .NET.

Example code in the version 3 distribution shows a huge number of alternative ways to create a server to handle XML-RPC style requests to execute Python methods and return properly formatted XML. Your program does not have to know a thing about XML, the Python toolkit handles all of the interpretation of a request in terms of Python methods and variables, and formats the response.

An exceptionally cool feature provides for creating HTML documentation of a Python XML-RPC method in response to a simple HTML GET directed to the service URL. To refresh your memory as to how simple XML-RPC is in comparison to SOAP, I ran the example XML-RPC server and client programs included in the Python 3.0 standard library and captured the request and response. Here is the request generated by only two lines of code:

POST /RPC2 HTTP/1.1
Host: localhost:8080
Accept-Encoding: identity
Content-Length: 187
Content-Type: text/xml
User-Agent: xmlrpclib.py/1.0.1 (by www.pythonware.com)

<?xml version='1.0'?>
<methodCall>
<methodName>pow</methodName>
<params>
<param>
<value><int>2</int></value>
</param>
<param>
<value><int>5</int></value>
</param>
</params>
</methodCall>

On the server side, a couple of lines created a server instance and registered the "pow" method. The server response was:

HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.0
Date: Thu, 29 Jan 2009 20:29:17 GMT
Content-type: text/xml
Content-length: 122

<?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><int>32</int></value>
</param>
</params>
</methodResponse>

Conclusion

If you already have a body of Python to support, the switch from earlier versions to Python 3 will require significant effort but this will pay off due to the support of Unicode. If you need a language to develop applications to support Unicode and run on a wide variety of operating system platforms, Python is an obvious choice. If you are looking for a scripting language to do some rapid prototyping of SOA style applications with, you will probably find the functions you want in Python 3.0. If you need WS-* support for SOAP/WSDL, you should probably look elsewhere.

Resources

The official home of the Python Programming Language

A summary of the new features in Python 3.

Python on the Google App Engine.

This site gives the syntax of JSON and lists languages and toolkits supporting it.

My article on the XML-RPC beginning of web services.

This Wikipedia article lists many XML-RPC implementations.


This was first published in February 2009

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.