[BangPypers] Writing a Python Client to access a web service.

Vinayak Hegde vinayakh at gmail.com
Tue Jun 3 11:34:33 CEST 2008


On Tue, Jun 3, 2008 at 2:37 PM, Heshan Suriyaarachchi
<heshan.suri at gmail.com> wrote:
> Hi,
>     I have written a web service and have deployed it using Apache Axis2.
> What I want to do is to invoke this service using a python script. Can
> anyone point me in the direction of how to do this. I will be grateful If
> anyone can give me a web reference to a code sample.I can do this with java.
> To give you an idea of what kind of a thing I want to do , I am attaching my
> java client code below.
>
> import org.apache.axis2.addressing.EndpointReference;
> import org.apache.axis2.client.ServiceClient;
> import org.apache.axis2.client.Options;
> import org.apache.axiom.om.OMElement;
> import org.apache.axiom.om.OMFactory;
> import org.apache.axiom.om.OMAbstractFactory;
> import org.apache.axiom.om.impl.builder.StAXOMBuilder;
> import javax.xml.stream.XMLStreamException;
> import java.io.ByteArrayInputStream;
>
> public class myClient {
>     private static EndpointReference targetEPR =
>             new
> EndpointReference("http://localhost:8070/axis2/services/annotationScript/deduct");
>     //http://localhost:8080/axis2/services/annotationScript/f
>
>     public static OMElement getPayload() throws XMLStreamException {
>         String str1 = "<f><a>4</a></f>";
>         String str2 =
> "<deduct><var1>1.8</var1><var2>4.87594</var2></deduct>";
>         String str3 =  "<add><var1>1.8</var1><var2>4.87594</var2></add>";
>         StAXOMBuilder builder = new StAXOMBuilder(new
> ByteArrayInputStream(str2.getBytes()));
>         return builder.getDocumentElement();
>     }
>
>     public static void main(String[] args) throws Exception {
>         ServiceClient sender = new ServiceClient();
>         Options op = new Options();
>         op.setTo(targetEPR);
>         sender.setOptions(op);
>         OMElement response = sender.sendReceive(getPayload());
>         System.out.println(response);
>     }
> }

Dive into python has a good tutorial on webservices using python.
http://www.diveintopython.org/soap_web_services/

The rest of the book is pretty readable as well.

-- Vinayak


More information about the BangPypers mailing list