简单资源地址分析器。
res-address的Python项目详细描述
python模块解析简单的网络资源地址,如 在许多数据库系统中用来表示数据库uri。
res_address将由Mongotail使用 解析通过命令行传递的地址(正在进行迁移),但可以使用 任何其他需要解析mongodb数据库地址的python应用程序, 或任何其他网络资源,如[[HOST OR IP][:PORT]/]RESOURCE。
用法:
>>>fromres_addressimportget_res_address>>>host,port,resource=get_res_address("localhost:27017/test")>>>print(host,port,resource)localhost27017test>>>host,port,resource=get_res_address("my_db")>>>print(host,port,resource)NoneNonemy_db>>>ipv6,port,resource=get_res_address("[::1]:9999/foo")>>>print(ipv6,port,resource)[::1]9999foo
地址可以是:
foo | foo resource on local machine (IPv4 connection) |
192.169.0.5/foo | foo resource on 192.168.0.5 machine |
remotehost/foo | foo resource on remotehost machine |
192.169.0.5:9999/foo | foo resource on 192.168.0.5 machine on port 9999 |
“[::1]:9999/foo” | foo resource on ::1 machine on port 9999 (IPv6 connection) |
:1234/foo | foo resource on port 1234 |
一些验证是通过主机、端口和资源字符串执行的,并且 如果某些检查失败,则启动异常,但要考虑到 无效的范围IP或不兼容的资源名称可能会传递:
>>>host,port,resource=get_res_address("localhost:INVALIDport/test")Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"res_address/__init__.py",line74,inget_res_addressraiseInvalidPortError('Invalid port number "%s"'%port,address,port)res_address.InvalidPortError:Invalidportnumber"INVALIDport"
所有验证异常都继承自AddressError:
- InvalidHostError
- InvalidPortError
- InvalidResourceError
- NotResourceProvidedError
运行测试
只需执行(python 2或3):
$ python setup.py test
或:
$ python -m unittest -v tests
关于
项目:https://github.com/mrsarm/python-res-address
作者:(2018-2019)Mariano Ruiz<;mrsarm@gmail.cm>;
许可证:lgpl-3