发布时间:2019-09-27 07:09:51编辑:auto阅读(1764)
在获取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 urldata
dubbo://10.4.5.3:20880/com.welab.authority.service.AuthorityService?anyhost=true&application=welab-authority&dubbo=2.5.7
#转成一个格式化对象
>>> result = urlparse.urlparse(urldata)
>>> print result
ParseResult(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 ip
10.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',[])
上一篇: python3 Django 报错Run
下一篇: python3之类的 __slots__
47750
46246
37132
34638
29231
25893
24762
19866
19426
17916
5720°
6323°
5841°
5891°
6991°
5830°
5850°
6364°
6318°
7682°