Simple Resource Address Parser.

pip install res-address==1.0.0


Simple Resource Address Parser

Python module to parse simple network resource addresses, like the ones used in many database systems to represent a database URI.

res_address will be used by Mongotail to parse the address passed through command line (migration in progress), but can be used by any other Python application that needs to parse a MongoDB database address, or any other network resource like [[HOST OR IP][:PORT]/]RESOURCE.


>>> from res_address import get_res_address
>>> host, port, resource = get_res_address("localhost:27017/test")
>>> print(host, port, resource)
localhost 27017 test
>>> host, port, resource = get_res_address("my_db")
>>> print(host, port, resource)
None None my_db
>>> ipv6, port, resource = get_res_address("[::1]:9999/foo")
>>> print(ipv6, port, resource)
[::1] 9999 foo

The address can be:

foo foo resource on local machine (IPv4 connection) foo resource on machine
remotehost/foo foo resource on remotehost machine foo resource on machine on port 9999
"[::1]:9999/foo" foo resource on ::1 machine on port 9999 (IPv6 connection)
:1234/foo foo resource on port 1234

Some validations are performed over the host, port and resource strings, and an exception is launched if some of the checks fails, but take into account that invalid range IPs or incompatible resource names may pass:

>>> host, port, resource = get_res_address("localhost:INVALIDport/test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "res_address/", line 74, in get_res_address
    raise InvalidPortError('Invalid port number "%s"' % port, address, port)
res_address.InvalidPortError: Invalid port number "INVALIDport"

All the validation exceptions inherit from AddressError:

  • InvalidHostError
  • InvalidPortError
  • InvalidResourceError
  • NotResourceProvidedError

Run the test

Just execute (Python 2 or 3):

$ python test


$ python -m unittest -v tests



Authors: (2018-2019) Mariano Ruiz <>

License: LGPL-3