本文共 1465 字,大约阅读时间需要 4 分钟。
在获取zk节点时,有些子节点名字直接就是编码后的url,就像下面这行一样:
url='dubbo%3A%2F%2F10.4.5.3%3A20880%2Fcom.welab.authority.service.AuthorityService%3Fanyhost%3Dtrue%26application%3Dwelab-authority%26dubbo%3D2.5.7'
先需要把这个url里进行解码, 转成如下这种:
dubbo://10.4.5.3:20880/com.welab.authority.service.AuthorityService?anyhost=true&application=welab-authority&dubbo=2.5.7
然后有用的参数: url中的ip地址, 参数中application对应的值
>>> import urlparse>>> urldata = urlparse.unquote(url)>>> print urldatadubbo://10.4.5.3:20880/com.welab.authority.service.AuthorityService?anyhost=true&application=welab-authority&dubbo=2.5.7#转成一个格式化对象>>> result = urlparse.urlparse(urldata)>>> print resultParseResult(scheme='dubbo', netloc='10.4.5.3:20880', path='/com.welab.authority.service.AuthorityService', params='', query='anyhost=true&application=welab-authority&dubbo=2.5.7', fragment='')#提取url中的参数,即url中?后的内容>>> args = urlparse.parse_qs(result.query) >>> print args{'application': ['welab-authority'], 'anyhost': ['true'], 'dubbo': ['2.5.7']}#获取我想要的信息 >>> ip= result.netloc>>> application = args.get('application',[])>>> print ip10.4.5.3:20880 >>> print application['welab-authority']
上面的测试中详细打印了输出信息, 这次就直接写在py脚本里了。
from urllib import parse#url解码urldata = parse.unquote(urldata)#url结果 result = parse.urlparse(urldata)#url里的查询参数 query_dict = parse.parse_qs(result.query)#获取我需要的信息ip = result.netloc application = query_dict.get('application',[])
转载于:https://blog.51cto.com/yangrong/2296222