Tecnologías XML / XML / Python & XML / Python y el diseño de sistemas distribuidos / Iniciar XML-Switch

(Extracto de «Python & XML», de Christopher A. Jones y Fred L. Drake, Jr.)

El archivo runxs.py se muestra en el ejemplo siguiente, El script de inicio de XML-Switch: runxs.py y es, en esencia, el script principal del servicio XML-Switch. El objeto XMLSwitchServer es una instancia de Base-HTTPServer.HTTPServer, por lo que se inicia de la misma manera:

import XMLSwitchHandler
import BaseHTTPServer

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

XMLSwitchServer.handle_request( )

El XMLSwitchHandler se proporciona durante la inicialización junto con el número del puerto en el que se escuchará. Cada vez que llega una solicitud, el HTTPServer recién iniciado invoca su XMLSwitchHandler para procesar la solicitud.

Sin embargo, en este ejemplo se utiliza el método handle_request, por lo que el servidor solo se ejecuta hasta haber atendido una solicitud. Puede cambiar este comportamiento llamando a XMLSwitchServer.serve_forever. En cualquier caso, siempre puede utilizar runxs.py del siguiente ejemplo para iniciar su servidor.

 

Ejemplo: el script de inicio de 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( )

Utilice una consola o un terminal disponible para iniciar el servidor:

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

El servidor muestra las solicitudes a medida que llegan:

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

La única interacción adicional con el servidor consiste en ejecutar xcs.py, como se muestra en el ejemplo Iniciar xcs.py desde la línea de comandos.

  

 

 

 

volver arriba