Невозможно использовать имя метода (из веб-сервиса), представленное в виде строки. Я попытался использовать getattr, но не смог расширить его для suds - PullRequest
0 голосов
/ 07 апреля 2011
from suds.client import Client #@UnresolvedImport
from suds.transport.https import HttpAuthenticated #@UnresolvedImport
import urllib2

class methodinvokeclass():
    def methodinvokemethod(self,*args):

        method=args[1]
        c=args[2]
        print c
        response=c.service.method("90210")# I know this wont work,coz of method, but even I cant get me way thru with getattr
        #response=c.service.LatLonListZipCode("90210")

        print response

if __name__=="__main__":

        invokemethodname="LatLonListZipCode"#Webservice name which I want to invoke...later !

        f=open("C:\wsdllocation.txt",'r')# picks up the WSDL file location from the above file
        webservwsdl=f.readline()
        f.close()
        y=methodinvokeclass()#dummy object
        z=methodinvokeclass()#dummy object

        t = HttpAuthenticated(username='x', password='x')#helps me getting thru my corporate firewall
        t.handler = urllib2.HTTPBasicAuthHandler(t.pm)#helps me getting thru my corporate firewall
        t.urlopener = urllib2.build_opener(t.handler)#helps me getting thru my corporate firewall
        c = Client(url=webservwsdl,transport=t)#SUDs client !!!!!

        x=y.methodinvokemethod(z,invokemethodname,c)# invoking the code above

1 Ответ

0 голосов
/ 07 апреля 2011

Вы можете использовать getattr для извлечения метода SOAP из сервиса.Используйте это вместо:

impl = getattr(c.service, method)
response = impl('90210')
print response

Вот рабочий пример использования тестовой службы SOAP webservicex :

url = 'http://www.webservicex.net/stockquote.asmx?WSDL'
client = Client(url=url)
name = 'GetQuote'
impl = getattr(client.service, name)
print impl('IBM')

Вывод:

<StockQuotes>
<Stock>
    <Symbol>IBM</Symbol>
    <Last>163.81</Last>
    <Date>4/7/2011</Date>
    <Time>11:47am</Time>
    <Change>-0.23</Change>
    <Open>164.10</Open>
    <High>164.5463</High>
    <Low>163.44</Low>
    <Volume>1573461</Volume>
    <MktCap>199.8B</MktCap>
    <PreviousClose>164.04</PreviousClose>
    <PercentageChange>-0.14%</PercentageChange>
    <AnnRange>116.00 - 167.72</AnnRange>
    <Earns>11.52</Earns>
    <P-E>14.24</P-E>
    <Name>International Bus</Name>
</Stock>
</StockQuotes>
...