XML technologies / XML / Python & XML / Python and the design of distributed systems / Starting XML-Switch

(Excerpt from "Python & XML" by Christopher A. Jones & Fred L. Drake, Jr.)

The file runxs.py is shown in the example below, The startup script for XML-Switch: runxs.py and is essentially the main script for the XML-Switch service. The XMLSwitchServer object is an instance of Base-HTTPServer.HTTPServer, so it is started accordingly:

import XMLSwitchHandler
import BaseHTTPServer

XMLSwitchServer = BaseHTTPServer.HTTPServer(
('', 2112), XMLSwitchHandler.XMLSwitchHandler)

XMLSwitchServer.handle_request( )

The XMLSwitchHandler is passed during initialization, together with the number of the port to listen on. Whenever a request arrives, the newly started HTTPServer invokes its XMLSwitchHandler to handle the request.

In this example, however, the method handle_request is used, so the server runs only until it has handled one request. You can change this by calling XMLSwitchServer.serve_forever. In either case, you can always use runxs.py from the following example to start your server.

 

Example: the startup script for XML-Switch: runxs.py

"""
runxs.py
"""
import XMLSwitchHandler
import BaseHTTPServer

# Starte Switch
print "Starten von XMLSwitch..."
XMLSwitchServer = BaseHTTPServer.HTTPServer(
      ('', 2112), XMLSwitchHandler.XMLSwitchHandler)

# Behandle Anfragen
print "Server gestartet..."
for x in range(10):
   XMLSwitchServer.handle_request( )

Use an available command or shell prompt to start the server:

G:\pythonxml\c10> python runxs.py
Starten von XMLSwitch...
Server gestartet...

The server prints requests as they arrive:

centauri - - [03/Jun/2001 15:12:02] "POST / HTTP/1.0" 200 -
centauri - - [03/Jun/2001 15:12:04] "POST / HTTP/1.0" 200 –

The only additional interaction with the server is to run xcs.py, as shown in the example Starting xcs.py from the command line.

  

 

 

 

back to top