现在,大多公司都采用soa架构。那么我们怎么知道soa提供哪些服务方法呢?是通过浏览器不断的点击尝试?还是通过wcf?还是通过阅读soa接口文档,在软件开发行业,总会有文档落后于代码的情况?这些手法都太拙劣了。其实我们可以通过一些更快捷高效的手段达到目的。
萌生这个想法的背景是,我们公司部署了几个soa服务。然后我们测试自动化非常分散,形式各种各样,五花八门,简直就是八仙过海各显神通,非常不易于统计自动化覆盖率统计。于是乎,就想到了使用脚本扫描统计的办法。不多说
了,直接上代码,
脚本如下:
#coding=utf-8 import requests from lxml import etree from suds.client import Client import csv preSoa = r"http://192.168.1.20:8085/" soaurl =requests.get(preSoa) detail_html = etree.HTML(soaurl.content) links =detail_html.xpath(ur"//a") hrefs = [preSoa +x.attrib["href"] for x in links if ".config" not in x.text and "bin" not in x.text and "Demo" not in x.text and ".asax" not in x.text ] svclinks =[] def getsvcurl(templink): tempsoaurlcontent = requests.get(templink).content temphtml = etree.HTML(tempsoaurlcontent) temphtmllink =temphtml.xpath(ur"//a/text()") svclink = [templink + x + "?singleWsdl" for x in temphtmllink if ".svc" in x] [svclinks.append(x) for x in svclink] [getsvcurl(x) for x in hrefs] getsvcurl(preSoa) print "svc count:{0}".format(len(svclinks)) client = Client(svclinks[0]) svcfuns =[] def dealsvcmethods(svclink): methods=[x[0] for x in Client(svclink).sd[0].ports[0][1]] [svcfuns.append({"funcname":x,"svc":svclink}) for x in methods] [dealsvcmethods(x) for x in svclinks] with open(ur"E:\主站soa2016-10-25.csv", 'ab+') as csvfile : writer = csv.DictWriter(csvfile,fieldnames = ["funcname","svc"]) writer.writeheader() writer.writerows(svcfuns)
当然,使用这种方式是有前提条件的。首先,soa站点必须要公开服务。有了这个,我们能够很容易找出前后版本新增的服务方法。