Querying a local XML-RPC server

If you do a lot of web programming, it's most likely that you will come across this task: to get some information from a website that runs an XML-RPC service. Before we go into the depth of an XML-RPC service, let's launch an XML-RPC server and talk to it first.

Getting ready

In this recipe, we will use the Python Supervisor program that is widely used to launch and manage a bunch of executable programs. Supervisor can be run as a background daemon and can monitor child processes and restart if they die suddenly. We can install Supervisor by simply running the following command:

$pip install supervisor

How to do it...

We need to create a configuration file for Supervisor. A sample configuration is given in this recipe. In this example, we define the Unix HTTP server socket and a few other parameters. Note the rpcinterface:supervisor section where rpcinterface_factory is defined to communicate with clients.

Using Supervisor, we configure a simple server program in the program:8_2_multithreaded_multicall_xmlrpc_server.py section by specifying the command and some other parameters.

Listing 8.1a gives the code for a minimal Supervisor configuration, as shown:

[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)
chmod=0700                 ; socket file mode (default 0700)

[supervisord]
logfile=/tmp/supervisord.log 
loglevel=info                
pidfile=/tmp/supervisord.pid 
nodaemon=true               

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[program:8_2_multithreaded_multicall_xmlrpc_server.py]
command=python 8_2_multithreaded_multicall_xmlrpc_server.py ; the 
program (relative uses PATH, can take args)
process_name=%(program_name)s ; process_name expr (default 
%(program_name)s)

If you create the preceding Supervisor configuration file in your favorite editor, you can run Supervisor by simply calling it.

Now, we can code an XML-RPC client that can act as a Supervisor proxy and give us the information about the running processes.

Listing 8.1b gives the code for querying a local XML-RPC server, as shown:

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter – 8
# This program is optimized for Python 2.7.
# It may run on any other version with/without modifications.
import supervisor.xmlrpc
import xmlrpclib

def query_supervisr(sock):
    transport = supervisor.xmlrpc.SupervisorTransport(None, None,
                'unix://%s' %sock)
    proxy = xmlrpclib.ServerProxy('http://127.0.0.1',
            transport=transport)
    print "Getting info about all running processes via Supervisord..."
    print proxy.supervisor.getAllProcessInfo()

if __name__ == '__main__':
    query_supervisr(sock='/tmp/supervisor.sock')

If you run the Supervisor daemon, it will show the output similar to the following:

chapter8$ supervisord
2013-09-27 16:40:56,861 INFO RPC interface 'supervisor' initialized
2013-09-27 16:40:56,861 CRIT Server 'unix_http_server' running 
without any HTTP authentication checking
2013-09-27 16:40:56,861 INFO supervisord started with pid 27436
2013-09-27 16:40:57,864 INFO spawned: 
'8_2_multithreaded_multicall_xmlrpc_server.py' with pid 27439
2013-09-27 16:40:58,940 INFO success: 
8_2_multithreaded_multicall_xmlrpc_server.py entered RUNNING state, 
process has stayed up for > than 1 seconds (startsecs)

Note that our child process, 8_2_multithreaded_multicall_xmlrpc_server.py, has been launched.

Now, if you run the client code, it will query the XML-RPC server interface of Supervisor and list the running processes, as shown:

$ python 8_1_query_xmlrpc_server.py 
Getting info about all running processes via Supervisord...
[{'now': 1380296807, 'group': 
'8_2_multithreaded_multicall_xmlrpc_server.py', 'description': 'pid 
27439, uptime 0:05:50', 'pid': 27439, 'stderr_logfile': 
'/tmp/8_2_multithreaded_multicall_xmlrpc_server.py-stderr---
supervisor-i_VmKz.log', 'stop': 0, 'statename': 'RUNNING', 'start': 
1380296457, 'state': 20, 'stdout_logfile': 
'/tmp/8_2_multithreaded_multicall_xmlrpc_server.py-stdout---
supervisor-eMuJqk.log', 'logfile': 
'/tmp/8_2_multithreaded_multicall_xmlrpc_server.py-stdout---
supervisor-eMuJqk.log', 'exitstatus': 0, 'spawnerr': '', 'name': 
'8_2_multithreaded_multicall_xmlrpc_server.py'}]

How it works...

This recipe relies on running the Supervisor daemon (configured with rpcinterface) in the background. Supervisor launches another XML-RPC server, as follows: 8_2_multithreaded_multicall_xmlrpc_server.py.

The client code has a query_supervisr()method, which takes an argument for the Supervisor socket. In this method, an instance of SupervisorTransport is created with the Unix socket path. Then, an XML-RPC server proxy is created by instantiating the ServerProxy() class of xmlrpclib by passing the server address and previously created transport.

The XML-RPC server proxy then calls the Supervisor's getAllProcessInfo() method, which prints the process information of the child process. This process includes pid, statename, description, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.173.199