Friday, March 22, 2013

Raspberry Pi SOAP Web Service Client Using Python Suds

I wrote about my first attempt at creating a Java SOAP Web service that runs on a Beaglebone. To test the service, I wanted a quick and easy client (besides the invaluable SoapUI, which has a free version in additional to the Pro version). There is a very handy SOAP client library for Python called Suds, so I used that to create a basic script to call the Web service.

In my case, the Java SOAP Web service is running on a Beaglebone connected to my home network. My WSDL is at http://192.168.0.106:9876/temp?wsdl. The service you are using will, of course, vary. The service has two simple methods that return the current temperature from the TMP102 sensor in degrees Celsius (getTempC) or degrees Fahrenheit (getTempF).

Python 2 Code


import time
from suds.client import Client

url = "http://192.168.0.106:9876/temp?wsdl"
client = Client(url)
while True:
   resultC = client.service.getTempC()
   resultF = client.service.getTempF()
   print " %2.3f C / %2.3f F" % (resultC, resultF)


6 comments:

  1. I have a need to write a soap client on my raspberry which will interface with a Windows 7 webservice all running on my intranet.
    Looking at your "Raspberry Pi SOAP Web Service Client Using Python Suds" I decided to give python a go even though my background is C, C++ and PHP.
    Not really knowing what I am doing I have create:-

    #!/usr/bin/python
    url = "http://192.168.0.10//CISWebService/Mediamanager.asmx?WSDL"
    client = Client(url)
    while True:
    iServerNo = client.GetSequenceNo("CIS")
    iClientNo = client.GetSequenceNo("CIS")
    print " %2 / %2" % (iServerNo, iClientNo)

    When I run it I get:-

    ./DevitSoap.py
    Traceback (most recent call last):
    File "./DevitSoap.py", line 3, in
    client = Client(url)
    NameError: name 'Client' is not defined

    Please tell me how I got this wrong.

    ReplyDelete
    Replies
    1. Hi Alf -

      Do you have the import lines (esp. from suds.client import Client) in your code? You may have left that out when you posted your code for the sake of brevity. Right off hand, though, leaving out the imports is the most obvious potential cause of the problem.

      --Brad

      Delete
    2. You are correct. I had left it out.
      My code now looks like:-

      #!/usr/bin/python
      import time
      from suds.client import Client
      url = "http://192.168.0.10//CISWebService/Mediamanager.asmx?WSDL"
      client = Client(url)
      while True:
      iServerNo = client.GetSequenceNo("CIS")
      iClientNo = client.GetSequenceNo("CIS")
      print " %2 / %2" % (iServerNo, iClientNo)

      and gives a result of

      ./DevitSoap.py
      Traceback (most recent call last):
      File "./DevitSoap.py", line 3, in
      from suds.client import Client
      ImportError: No module named suds.client

      again please tell me how I fix this.

      Delete
    3. and after doing an sudo apt-get install python-suds I get:-

      ./DevitSoap.py
      Traceback (most recent call last):
      File "./DevitSoap.py", line 7, in
      iServerNo = client.GetSequenceNo("CIS")
      AttributeError: 'Client' object has no attribute 'GetSequenceNo'

      However i know that GetSequenceNo takes a parameter because of what I get from a wdsl call from a php app running on another PC

      array(20) {
      [0]=>
      string(59) "GetMediaListResponse GetMediaList(GetMediaList $parameters)"
      [1]=>
      string(68) "GetMediaListAllResponse GetMediaListAll(GetMediaListAll $parameters)"
      [2]=>
      string(59) "GetSoundListResponse GetSoundList(GetSoundList $parameters)"
      [3]=>
      string(77) "GetTerminalDataXMLResponse GetTerminalDataXML(GetTerminalDataXML $parameters)"
      [4]=>
      string(59) "GetRouterXmlResponse GetRouterXml(GetRouterXml $parameters)"
      [5]=>
      string(80) "GetTerminalSoundXMLResponse GetTerminalSoundXML(GetTerminalSoundXML $parameters)"
      [6]=>
      string(62) "SetSequenceNoResponse SetSequenceNo(SetSequenceNo $parameters)"
      [7]=>
      string(50) "GetConfigResponse GetConfig(GetConfig $parameters)"
      [8]=>
      string(62) "GetSequenceNoResponse GetSequenceNo(GetSequenceNo $parameters)"
      [9]=>
      string(95) "UpdateClientMediaLogTimeResponse UpdateClientMediaLogTime(UpdateClientMediaLogTime $parameters)"
      [10]=>
      string(59) "GetMediaListResponse GetMediaList(GetMediaList $parameters)"
      [11]=>
      string(68) "GetMediaListAllResponse GetMediaListAll(GetMediaListAll $parameters)"
      [12]=>
      string(59) "GetSoundListResponse GetSoundList(GetSoundList $parameters)"
      [13]=>
      string(77) "GetTerminalDataXMLResponse GetTerminalDataXML(GetTerminalDataXML $parameters)"
      [14]=>
      string(59) "GetRouterXmlResponse GetRouterXml(GetRouterXml $parameters)"
      [15]=>
      string(80) "GetTerminalSoundXMLResponse GetTerminalSoundXML(GetTerminalSoundXML $parameters)"
      [16]=>
      string(62) "SetSequenceNoResponse SetSequenceNo(SetSequenceNo $parameters)"
      [17]=>
      string(50) "GetConfigResponse GetConfig(GetConfig $parameters)"
      [18]=>
      string(62) "GetSequenceNoResponse GetSequenceNo(GetSequenceNo $parameters)"
      [19]=>
      string(95) "UpdateClientMediaLogTimeResponse UpdateClientMediaLogTime(UpdateClientMediaLogTime $parameters)"
      }

      So the error must be they call is wrong?

      Delete
    4. Hi Alf -

      Note that in the example code the call uses client.service, not simply the client object. What happens if you try iServerNo = client.service.GetSequenceNo("CIS") ?

      --Brad

      Delete
  2. hi. i'm using a DHT11 Sensors on Raspberry Pi. i have installed Adafruit_DHT_Drivers

    i'm trying to send two data. temp and humidity to a local web service like you did

    this is my code



    #!/usr/bin/python
    from suds.client import Client
    import subprocess
    import re
    import sys
    import time
    import datetime


    url = "http://172.20.132.73/SCS/WebService/webS.php?wsdl"
    client = Client(url)

    while(true):

    output = subprocess.check_output(["./Adafruit_DHT", "2302", "4"]);
    print output
    matches = re.search("Temp =\s+([0-9.]+)", output)
    if (not matches):
    time.sleep(3)
    continue
    temp = float(matches.group(1))

    # search for humidity printout
    matches = re.search("Hum =\s+([0-9.]+)", output)
    if (not matches):
    time.sleep(3)
    continue
    humidity = float(matches.group(1))

    resultC = client.service.hello(temp, humidity)
    print (resultC)
    system.sleep(50)





    however i do encounter this problem:
    pi@raspberrypi /var/www $ sudo python websvc2.py
    File "websvc2.py", line 30
    resultC = client.service.hello(temp, humidity)
    ^
    IndentationError: unindent does not match any outer indentation level


    do advise.. :(

    ReplyDelete