Skip to main content

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.

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

Usage:

>>> from res_address import get_res_address
>>> scheme, host, port, resource, query, username, password = get_res_address("localhost:27017/test?timeout=5")
>>> print(scheme, host, port, resource, query, username, password)
None localhost 27017 test timeout=5 None None
>>> print(get_res_address("my_db"))
(None, None, None, 'my_db', None, None, None)
>>> scheme, ipv6, port, resource, query, username, password = get_res_address("https://[::1]:9999/foo")
>>> print(scheme, ipv6, port, resource)
https [::1] 9999 foo

The address can be:

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

http://192.169.0.5:9999/foo

foo resource on 192.168.0.5 machine on port 9999, scheme http

“[::1]:9999/foo”

foo resource on ::1 machine on port 9999 (IPv6 connection)

:1234/foo

foo resource on port 1234

user:pass@localhost/foo

foo resource on localhost, with basic authentication

localhost/foo?timeout=500

foo resource on localhost, with query string

The only required component in the URI is the resource. Some validations are performed over the host, port and resource strings, and an exception is launched if some of the checks fail, but take into account that invalid range of IP addresses or incompatible resource names may pass:

>>> address = get_res_address("localhost:INVALIDport/test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "res_address/__init__.py", 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

Difference with urllib.parse

If you need a library that can fully parse web URLs you should use urllib.parse instead. This library intends to be used with simpler URIs for database resources, with only one resource name set, e.g. “my_db” or “localhost:123/my_db”, but not “localhost:123/my_db/my_table”. Also a URL like “localhost:123” that is a valid HTTP URL, is not as database URL because the “resource” (the database name) is not set.

Also to get the keys and values of the query component returned by get_res_address(), a function like urllib.parse.parse_qs() is recommended:

>>> from res_address import get_res_address
>>> from urllib.parse import parse_qs
>>> scheme, host, port, resource, query, username, password = get_res_address("localhost/db?timeout=500")
>>> parse_qs(query).get('timeout', None)
['500']

Run the tests

Just execute (Python 2.7 or 3.5+):

$ python setup.py test

Or:

$ python -m unittest -v tests

About

Project: https://github.com/mrsarm/python-res-address

Authors: (2018-2022) Mariano Ruiz <mr…@gmail.cm>

License: LGPL-3

Metadata

Release files for res-address 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for res-address 2.0.0
File Size Uploaded
res-address-2.0.0.tar.gz 18.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for res-address 2.0.0
File Interpreter ABI Platform
res_address-2.0.0-py3-none-any.whl Python 3 none any Details
res_address-2.0.0-py2-none-any.whl Python 2 none any Details

Total release size: 56.0 kB

Release files / res-address-2.0.0.tar.gz

Download URL res-address-2.0.0.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ae59a5b073106d32bca50972df1c4a37619eb2b66c9441b500aa3334e75f2fd0
BLAKE2b-256 checksum
How to use checksums
6ea92668ce5753c13a197f8681ebb5029d459510b670413aa34457d07de179fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

Release files / res_address-2.0.0-py3-none-any.whl

Download URL res_address-2.0.0-py3-none-any.whl
Size 20.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb607979460a1de2a9605f95c7c6c1fe54973eb8385d5e252ec8238c6b0f155f
BLAKE2b-256 checksum
How to use checksums
4f184241bba8bd00c5313d54acfd837ab0729dd6a1a373d7fcd58a01f17373ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

Release files / res_address-2.0.0-py2-none-any.whl

Download URL res_address-2.0.0-py2-none-any.whl
Size 17.2 kB
Tags Python 2
SHA-256 checksum
How to use checksums
8b5f1d61ef9e2030c5bbb61eb82a434f88f04d468d46bfdc52e0c033d424fcaa
BLAKE2b-256 checksum
How to use checksums
ab4949a6b823fe3f472b0087a4c2c97a58d85d5193505ac90b2ab2599a450657
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page