Running the XML Switch

The file runxs.py is shown in Example 10-13, and actually is the primary script for the XML Switch service. The XMLSwitchServer object is an instance of the BaseHTTPServer.HTTPServer, so it is launched accordingly:

import XMLSwitchHandler
import BaseHTTPServer

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

XMLSwitchServer.handle_request(  )

The XMLSwitchHandler is passed in at construction time along with the port number to listen to. Whenever a request comes in, the HTTPServer just launched invokes its XMLSwitchHandler to manage the request.

In this example, however, the handle_request method is used. Now the server runs only until it services one request. You can change this by calling XMLSwitchServer.serve_forever. Regardless, you can always use runxs.py, shown in Example 10-13, to start your server.

Example 10-13. The XML Switch launching script: runxs.py

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

# start up
print "XMLSwitch starting..."
XMLSwitchServer = BaseHTTPServer.HTTPServer(
    ('', 2112), XMLSwitchHandler.XMLSwitchHandler)

# run server
print "Running..."
for x in range(10):
  XMLSwitchServer.handle_request(  )

To launch the server, use a spare command or shell prompt:

G:\pythonxml\c10> python runxs.py
XMLSwitch starting...
Running...

The server outputs requests as they happen:

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

Get Python & XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.