The Python server

The AFIMS server

With the analyser system being located in Sydney while the various people who wish to monitor the system are situated in Sydney, Brisbane and Melbourne, it became apparent that a 'net-based server would be an ideal solution for allowing remote monitoring (and control) of the analysers.

The key functions of the server are:

  1. Read in a configuration file

  2. Lock the serial port, initialise the port and the RS-485 network

  3. Initialise each of the nodes defined in the configuration file and make sure they are functioning correctly and have the correct experimental settings

  4. Accept client connections until the server is shutdown

  5. On shutdown, close down the nodes and the RS-485 network

Note that this is a broad overview; there are also security checks, etc.

When a client connection is established, the client/server can:

or just connect and chart the concentration profiles over time if an analysis cycle has already been initiated.

The server is written in Python (with one small section of C code to handle initialising the serial port and the low level read/write details).

What is Python?

To quote the Python tutorial:

Python is a simple, yet powerful programming language that bridges the gap between C and shell programming, and is thus ideally suited for 'throw-away programming' and rapid prototyping. Its syntax is put together from constructs borrowed from a variety of other languages; most prominent are influences from ABC, C, Modula-3 and Icon.

The Python interpreter is easily extended with new functions and data types implemented in C. Python is also suitable as an extension language for highly customizable C applications such as editors or window managers.

Python is available for various operating systems, amongst which several flavors of Unix, Amoeba, the Apple Macintosh O.S., and MS-DOS.

Why Python?

Python's disadvantages

Yes! It's true... Python does have some disadvantages!!!

Why was Python used here?

Python was used, initially, to develop a series of test programs to check the development of the hardware and the embedded controller software.

Prior to using Python, the testing software was developed in C, and as the hardware and controller software was slowly assembled, the test programs would be extended, piece by piece. This became very unwieldy, very rapidly - despite our best efforts at designing the test software before we started (aka the joys of research!). There was also the familiar edit-compile-test cycle that tends to inhibit prototyping software.

The combination of Python's interactive interpreter and object orientation allowed us to rapidly develop (and extend) test classes that modelled the hardware. The very high level nature of the language allows one to easily interface to real-world systems and interactively manipulate (and extend) the system.

We were so pleased with Python's power and elegance that we moved beyond using it as a rapid development/prototyping language, and have used it as the basis for the final server product as well. Python's networking support at both the socket level and protocol level (eg. http, nttp and ftp support are all available as library functions) makes it an excellent language for rapidly developing robust networking tools.

Some examples

This simple script initialises the network, connects to node 2 on the network and then pings it once a minute to make sure that it is running.

import rs485                          # RS-485 interface library
import node                           # basic node class library

rs485.init()                          # initialise the network

myNode = node.Node(2)                 # set up node 2

while 1:                              # loop until the node goes off-line
    status =            # ping the node

    if status != NODE_UP:
        print "Node is not responding"
        print "Node is responding..."

    time.sleep(60)                    # sleep for 60 seconds
The following script creates a new class of node called TestNode that pings the node when the node object is created to make sure it is connected and offers a new method which simply pings the node once a minute for ten minutes to check the node.

import node

class TestNode(Node):

    def __init__(self):               # object initialisation
      if != NODE_UP:
          raise NodeException, "Node is not connected"

    def testMe(self):
        for i in range (0, 10):
            if status != NODE_UP:
                print "Node is not responding"
                print "Node is responding..."

            time.sleep(60)             # sleep for 60 seconds
While these scripts are trivial examples, note that there are no braces or BEGIN/END statements: control blocks are determined by the indentation level. You either love it or you hate it!

Note also that typing is dynamic, but, unlink Tcl, Python supports a wide range of data types, such as integers, floats, strings, lists, tuples, and arrays.

[Index] [Prev] [Next]