Explorar o código

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).
Andrei Pelinescu-Onciul %!s(int64=16) %!d(string=hai) anos
pai
achega
4903fc5f9b

+ 67 - 0
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");
+	}
+

+ 51 - 0
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;

+ 29 - 0
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;