python 读取consul配置
自动化通过rcp client调用远端服务接口时,都需要将远端测试服务ip、端口记录在配置文件。
但由于,服务发布或重启会导致ip、端口变动。
以下将通过python-consul 自动去读取consul配置,获取相应服务的ip、port
1 import consul 2 #connect consul 3 c = consul.Consul(host='10.1.x.x',port=8500,scheme='http') 4 data = c.catalog.service('xxxxme.srv.account') 5 for value in data[1]: 6 print("accountSrv addr: " + value['ServiceAddress'],value['ServicePort'])
输出结果如下:
参考文档:http://python-consul.readthedocs.io/en/latest/
将上述代码封装,并使用unittest调用时,返回如下警告:
C:\python3.6\lib\collections\__init__.py:431: ResourceWarning: unclosed <socket.socket fd=864, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('10.100.x.x', 37321), raddr=('10.1.x.x', 8500)>
exec(class_definition, namespace)
C:\python3.6\lib\site-packages\urllib3\util\timeout.py:94: ResourceWarning: unclosed <socket.socket fd=900, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('10.100.x.x', 37320), raddr=('10.1.x.x', 8500)>
self._connect = self._validate_timeout(connect, 'connect')
查了一番资料,可以通过warnings库来忽略掉相关告警。
import warnings warnings.simplefilter("ignore", ResourceWarning)