Skip to content

Instantly share code, notes, and snippets.

@thomd
Last active February 11, 2016 12:03
Show Gist options
  • Save thomd/8106772 to your computer and use it in GitHub Desktop.
Save thomd/8106772 to your computer and use it in GitHub Desktop.
XML-RPC Sample with a Java Server and a Python Client

compile:

mvn clean compile assembly:single

start XML-RPC Server:

java -cp target/xmlrpc-test-server-1.0-jar-with-dependencies.jar net.thomd.JavaServer

monitor any traffic on loopback interface on port 8080:

ngrep -d lo0 port 8080

call method sumAndDifference(11,7):

python -c 'import xmlrpclib; print xmlrpclib.Server("http://localhost:8080/RPC2").sample.sumAndDifference(11,7)'
// src/main/java/net/thomd/JavaServer.java
package net.thomd;
import java.util.Hashtable;
import helma.xmlrpc.*;
public class JavaServer {
public JavaServer () {
// Our handler is a regular Java object. It can have a
// constructor and member variables in the ordinary fashion.
// Public methods will be exposed to XML-RPC clients.
}
public Hashtable sumAndDifference (int x, int y) {
Hashtable result = new Hashtable();
result.put("sum", new Integer(x + y));
result.put("difference", new Integer(x - y));
return result;
}
public static void main (String [] args) {
try {
WebServer server = new WebServer(8080);
server.addHandler("sample", new JavaServer());
} catch (Exception exception) {
System.err.println("JavaServer: " + exception.toString());
}
}
}
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.thomd</groupId>
<artifactId>xmlrpc-test-server</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>xmlrpc-test-server</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>xmlrpc-helma</groupId>
<artifactId>xmlrpc-helma</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>net.thomd.JavaServer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
@ReeganArockiasmy
Copy link

Hi i need a help
I have run this code i am getting an error, i dont know how to resolve this please help
python -c 'import xmlrpclib; print xmlrpclib.Server("http://localhost:8080/RPC2").sample.sumAndDifference(11,7)'

my error message
$ python -c 'import xmlrpclib; print xmlrpclib.Server("http://localhost:8080/RPC2").sample.sumAndDifference(11,7)' Traceback (most recent call last): File "<string>", line 1, in <module> File "/usr/lib/python2.7/xmlrpclib.py", line 1233, in __call__ return self.__send(self.__name, args) File "/usr/lib/python2.7/xmlrpclib.py", line 1591, in __request verbose=self.__verbose File "/usr/lib/python2.7/xmlrpclib.py", line 1273, in request return self.single_request(host, handler, request_body, verbose) File "/usr/lib/python2.7/xmlrpclib.py", line 1306, in single_request return self.parse_response(response) File "/usr/lib/python2.7/xmlrpclib.py", line 1482, in parse_response return u.close() File "/usr/lib/python2.7/xmlrpclib.py", line 794, in close raise Fault(**self._stack[0]) xmlrpclib.Fault: <Fault 0: 'java.lang.ClassNotFoundException: SAX driver not found: org.apache.xerces.parsers.SAXParser'>

But i am open in browser that url "http://localhost:8080/" i am geting this Method GET not implemented (try POST) in browser

@ReeganArockiasmy
Copy link

Add this depencies my pom file

<dependency> <groupId>xerces</groupId> <artifactId>xerces</artifactId> <version>1.4.0</version> </dependency>

It is work fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment