Invoke Web Service From Python

Last post 03-05-2009 2:28 AM by Shakeel.ahmad. 0 replies.

Sort Posts:

  • Invoke Web Service From Python

    03-05-2009, 2:28 AM
    • Member
      2 point Member
    • Shakeel.ahmad
    • Member since 04-16-2008, 3:33 AM
    • Lahore, Pakistan
    • Posts 11

    Hello All!

    I have developed a test web service, which contains a Testing function which have 2 string parameters and returns a string. This web service is accessible from dotnet client. But when I try to acces it from Python, it throws exception. Please help me.

    code of python is written below.

    python code:

    #!/usr/bin/env python

    import os
    import cPickle
    import pprint
    from time import *
    import httplib
    import urllib
    import sys
    import re
    from elementtree import ElementTree as ET

    import logging
    import logging.config

    # makes the XML pretty to look at
    def indent(elem, level=0):
        i = "\n" + level*"  "
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + "  "
            for e in elem:
                indent(e, level+1)
            if not e.tail or not e.tail.strip():
                e.tail = i
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

     

    def CreateSoapMessage(action):

        envelope = """
            <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
              <soap:Body>
                %s
              </soap:Body>
            </soap:Envelope>
            """ %(action)

        return(envelope)   


    def SendHTTP(httpmessage):
        blen = len(httpmessage)
        requestor = httplib.HTTP('localhost', 8080); 
        requestor.putrequest('POST', '/Test_Shakeel/Service.asmx','HTTP/1.1')
        requestor.putheader('Host', 'localhost')
        requestor.putheader('Content-Type', 'application/soap+xml; charset=utf-8')
        requestor.putheader('Content-Length', str(blen))
        requestor.endheaders()
        requestor.send(httpmessage)
        (status_code, message, reply_headers) = requestor.getreply()
        reply_body = requestor.getfile().read()
        return reply_body

     

    class Client:


        def pythonlogger(self,level,message):
            if level == "info":
                logger.info(message)
            if level == "warn":
                logger.warn(message)
            if level == "error":
                logger.error(message)
            return

        def Testing (self,first,second):
            #wylessclient = WylessClient()
            action = """<Testing xmlns="http://tempuri.org">
                    <a>%s</a>
                    <b>%s</b>
                </Testing>""" %(first,second)
            httpmessage = CreateSoapMessage(action)
            print httpmessage
            reply_body = SendHTTP(httpmessage)
            print reply_body
            # Get response from server and create an elementtree
            xmlin = ET.XML(reply_body)
            results = xmlin.find(".//http://localhost:8080}TestingResult")
            token = results.text
            return token

    client1 = Client()
    retvalue = client1.Testing('first','second')

     Exception with output is shown below.

     <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
              <soap:Body>
                <Testing xmlns="http://tempuri.org">
                    <a>first</a>
                    <b>second</b>
                </Testing>
              </soap:Body>
            </soap:Envelope>
           
    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Sender</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Unable to handle request without a valid action parameter. Please supply a valid soap action.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

    Traceback (most recent call last):
      File "C:\Python25\WylessClient_Orignal.py", line 137, in <module>
        retvalue = client1.Testing('first','second')  File "C:\Python25\WylessClient_Orignal.py", line 84, in Testing
        retvalue = client1.Testing('first','second')  File "C:\Python25\Lib\site-packages\elementtree\ElementTree.py", line 327, in find
        return ElementPath.find(self, path)
      File "C:\Python25\Lib\site-packages\elementtree\ElementPath.py", line 183, in find
        return _compile(path).find(element)
      File "C:\Python25\Lib\site-packages\elementtree\ElementPath.py", line 173, in _compile
        p = Path(path)
      File "C:\Python25\Lib\site-packages\elementtree\ElementPath.py", line 90, in __init__
        "expected path separator (%s)" % (op or tag)
    SyntaxError: expected path separator (:)

    Thanks a lot.

    Shakeel Ahmad Ishaq
Page 1 of 1 (1 items)