Skip to content

Instantly share code, notes, and snippets.

@ReeganArockiasmy
Forked from thomd/JavaServer.java
Created February 10, 2016 09:55
Show Gist options
  • Save ReeganArockiasmy/962bcdfcb80680b89ca8 to your computer and use it in GitHub Desktop.
Save ReeganArockiasmy/962bcdfcb80680b89ca8 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment