[sr-dev] git:master: xmlrpc(s): added rpc client examples

Andrei Pelinescu-Onciul andrei at iptel.org
Mon Jul 13 19:10:00 CEST 2009


Module: sip-router
Branch: master
Commit: 4903fc5f9b5fa2b6047780dc192d6b41fd746d68
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=4903fc5f9b5fa2b6047780dc192d6b41fd746d68

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Mon Jul 13 19:04:52 2009 +0200

xmlrpc(s): added rpc client examples

Added rpc client examples/test scripts in perl and python. The
python script has 2 versions: one with re-defined transport (that
works properly without expecting the server side to immediately
close the connection after answering) and one using the default
xmlrpclib transport (which requires closing the connection from
the ser xmlrpc route).

---

 modules_s/xmlrpc/examples/xmlrpc_test.pl  |   67 +++++++++++++++++++++++++++++
 modules_s/xmlrpc/examples/xmlrpc_test.py  |   51 ++++++++++++++++++++++
 modules_s/xmlrpc/examples/xmlrpc_test2.py |   29 ++++++++++++
 3 files changed, 147 insertions(+), 0 deletions(-)

diff --git a/modules_s/xmlrpc/examples/xmlrpc_test.pl b/modules_s/xmlrpc/examples/xmlrpc_test.pl
new file mode 100644
index 0000000..dbb32b8
--- /dev/null
+++ b/modules_s/xmlrpc/examples/xmlrpc_test.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+# perl script for sending an xmlrpc command to ser's xmlrpc module,
+# extra verbose output
+# Usage:  perl xmlrpc_test.pl command [params...]
+#
+# History:
+# --------
+#  2009-07-13  initial version (andrei)
+#
+#
+
+use strict;
+use warnings;
+
+use XMLRPC::Lite;
+
+	my $rpc=shift @ARGV;
+	my @rpc_params=@ARGV;
+	my $k;
+	my %r;
+	my $i;
+
+	if (!defined $rpc) {
+		die "Usage: $0 rpc_command [args..]";
+	}
+
+# actual rpc call
+
+	my($rpc_call) = XMLRPC::Lite
+		-> proxy("http://127.0.0.1:5060") -> call($rpc, @rpc_params);
+	
+	my $res= $rpc_call->result;
+
+# extra verbose result printing (could be skipped)
+
+	if (!defined $res){
+		print "fault{\n";
+		$res=$rpc_call->fault;
+		%r=%{$res};
+		foreach $k (sort keys %r) {
+			print("\t$k: $r{$k}\n");
+		}
+		print "}\n";
+		exit -1;
+	}
+	if (ref($res) eq "HASH"){
+		print("{\n");
+		%r=%{$res};
+		foreach $k (keys %r) {
+			print("\t$k: ",  $r{$k}, "\n");
+		}
+		print("}\n");
+	} elsif (ref($res) eq "ARRAY"){
+		print "[\n";
+		for ($i=0; $i<@{$res}; $i++){
+			print "\t${$res}[$i]\n";
+		}
+		print "]\n";
+	}elsif (ref($res) eq "SCALAR"){
+		print "${$res}\n";
+	}elsif (!ref($res)){
+		print "$res\n";
+	}else{
+		print("ERROR: reference to ", ref($res), " not handled\n");
+	}
+
diff --git a/modules_s/xmlrpc/examples/xmlrpc_test.py b/modules_s/xmlrpc/examples/xmlrpc_test.py
new file mode 100644
index 0000000..0bf1591
--- /dev/null
+++ b/modules_s/xmlrpc/examples/xmlrpc_test.py
@@ -0,0 +1,51 @@
+import xmlrpclib, httplib, sys
+
+# Usage:  python xmlrpc_test.py command [params...]
+#
+# python script for sending an xmlrpc command to ser's xmlrpc module.
+# Note: it uses a re-defined transport class that does not depend on the
+# server side closing the connection (it can be used to send multiple
+# commands without closing the connections and it will work without any
+# special workarounds in the ser.cfg xmlrpc route).
+#
+# Credits: the transport class comes from ser_ctl, heavily trimmed to a very
+# basic version (the ser_ctl version is much more complex, supports
+# authentication, ssl a.s.o). See
+# http://git.sip-router.org/cgi-bin/gitweb.cgi?p=ser;a=blob;f=ser_ctl/serctl/serxmlrpc.py#l73 for the original (better) version.
+#
+#
+# History:
+# --------
+#  2009-07-13  initial version (andrei)
+#
+
+
+XMLRPC_SERVER = "127.0.0.1"
+XMLRPC_PORT = 5060
+
+class Transport:
+	def __init__(self):
+		self.conn=httplib.HTTPConnection(XMLRPC_SERVER, str(XMLRPC_PORT));
+
+	def _http_request(self, uripath, body, host):
+		self.conn.request("POST", uripath, body, {})
+
+	def request(self, host, uripath, body, verbose=0):
+		self._http_request(uripath, body, host)
+		response=self.conn.getresponse()
+		if response.status != 200:
+			raise xmlrpclib.ProtocolError(host+uripath, response.status,
+							response.reason, response.msg)
+		data=response.read()
+		parser, unmarshaller=xmlrpclib.getparser()
+		parser.feed(data)
+		parser.close()
+		return unmarshaller.close()
+
+if len(sys.argv) < 2:
+	sys.exit("Usage: "+sys.argv[0]+" rpc_command [args...]");
+transport=Transport()
+c=xmlrpclib.ServerProxy("http://" + XMLRPC_SERVER+ ":" + str(XMLRPC_PORT),
+						transport)
+res=getattr(c, sys.argv[1])(*sys.argv[2:])
+print res;
diff --git a/modules_s/xmlrpc/examples/xmlrpc_test2.py b/modules_s/xmlrpc/examples/xmlrpc_test2.py
new file mode 100644
index 0000000..0fdee1e
--- /dev/null
+++ b/modules_s/xmlrpc/examples/xmlrpc_test2.py
@@ -0,0 +1,29 @@
+import xmlrpclib, httplib, sys
+
+# Usage:  python xmlrpc_test2.py command [params...]
+#
+# python script for sending an xmlrpc command to ser's xmlrpc module.
+# This script uses python xmlrpclib directly and expects the remote side to
+# immediately close the connection after answering (broken xmlrpclib 
+# behaviour).
+# There are 2 way to make it work with ser xmlrpc module: define a
+# better transport class (that's what the xmlrpc_test.py script is doing) or
+# change ser xmlrpc route so that it will close the connection after each
+# processes xmlrpc request (e.g. add a drop -1 at the end).
+#
+# See also: xmlrpc_test.py (better version, using a redefined transport class).
+#
+# History:
+# --------
+#  2009-07-13  initial version (andrei)
+#
+
+XMLRPC_SERVER = "127.0.0.1"
+XMLRPC_PORT = 5060
+
+
+if len(sys.argv) < 2:
+	sys.exit("Usage: "+sys.argv[0]+" rpc_command [args...]");
+c=xmlrpclib.ServerProxy("http://" + XMLRPC_SERVER+ ":" + str(XMLRPC_PORT))
+res=getattr(c, sys.argv[1])(*sys.argv[2:])
+print res;




More information about the sr-dev mailing list