用Python写了一个测试Web服务的简单工具

写了一个测试和分析Web服务的简单工具,功能简单:)
#!/usr/bin/env python
# -*- coding: cp936 -*-
# Author: Jamsa

import sys
from SOAPpy import WSDL

class Analyzer:
    def __init__(self,wsdlurl):
        self.wsdlurl = wsdlurl
        self.server = WSDL.Proxy(wsdlurl)
    
    #获取所有方法对象
    def getMethods(self):
        return self.server.methods

		#获取方法的详细信息
    def getMethodInfo(self,methodname):
        callInfo = self.server.methods[methodname]
        return callInfo

#获取方法信息
def methodsInfo(analyzer):
    methods = analyzer.getMethods()
    print 'Total: ',len(methods),' methods'
    for methodname in methods.keys():
        print 'Method: \n\t',methodname
        method = methods[methodname]

        inparams = method.inparams
        print 'Input: '
        for inparam in inparams:
            print '\t',inparam.name,'\t',inparam.type

        outparams = method.outparams
        print 'Output: '
        for outparam in outparams:
            print '\t',outparam.name,'\t',outparam.type

#执行方法,只能是字符串或数字类型:)聊胜于无
def executeMethod(analyzer,methodname,params):
    print 'Execute remote method: ',methodname,'with params: ',params
    #method = analyzer.getMethods[methodname]
    proxy = analyzer.server
    paramstr = ''
    for param in params:
        paramstr = '"' + param + '",'
    paramstr = ','.join(paramstr.split(',')[:-1])
    evalstr = 'proxy.'+methodname+'('+paramstr+')'
    print evalstr
    try:   
        ret = eval('proxy.'+methodname+'('+paramstr+')')
        print 'Remote return:'
        print '\t',ret
        return ret
    except:
        print 'Execute ',methodname,' error: '
        print sys.exc_type, sys.exc_value
        
        
#使用方法: analyzer.py info|exe url [methodname] [params]
#info 表示要获取该Web服务的所有方法的详细信息
#exe 表示执行方法
#url WSDL的地址,不是Web服务的地址
#methodname 在执行方法时指定要执行的方法名
#params 在执行方法时要传递的参数字符
if __name__ == '__main__':
    if len(sys.argv) < 3:
        print 'Usage: analyzer.py info|exe url [methodname] [params]'
        sys.exit()
    
    opt = sys.argv[1]
    url = sys.argv[2]
        
    #url = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl'
    print 'Get WSDL ...'
    try:
        analyzer = Analyzer(url)
    except:
        print 'Error: '
        print sys.exc_type, sys.exc_value
        sys.exit()
    
    if opt == 'info':
        methodsInfo(analyzer)
        sys.exit()

    if opt == 'exe':
        if len(sys.argv)<4:
            print 'Usage: analyzer.py exe url methodname [params]'
        methodname = sys.argv[3]
        params = sys.argv[4:]
        executeMethod(analyzer,methodname,params)
        sys.exit()
    
    print 'Usage: analyzer.py info|exe url [methodname] [params]'
    sys.exit()

你可能感兴趣的:(Web,.net,python)