luxos.ips#

ipaddress manipulation

exception luxos.ips.DataParsingError#

Bases: LuxosBaseException

luxos.ips.ip_ranges(txt: str, gsep: str = ':', strict: bool = True) list[tuple[str, int | None]]#

return a list of ips given a text expression.

Eg.
>>> for ip in ip_ranges("127.0.0.1"):
...     print(ip)
127.0.0.1
>>> for ip in ip_ranges("127.0.0.1-127.0.0.3"):
...     print(ip)
127.0.0.1
127.0.0.2
127.0.0.3

NOTE: use the : (gsep) to separate ips groups, and - (rsep) to define a range.

luxos.ips.iter_ip_ranges(txt: str, port: int | None = None, gsep: str = ',', strict: bool = True) Generator[tuple[str, int | None], None, None]#

iterate over ip ranges.

The txt string cav have one of these formats:

  1. a single ip and (optional) port: 127.0.0.1 or 127.0.0.1:8080

  2. an (inclusive) range using two ips separated by a - (minus) sign: 127.0.0.1-127.0.0.3

  3. a combination of the above separated by a , (comma) sign: 127.0.0.1,192.168.0.1-192.168.0.10

Example:

for ip in iter_ip_ranges("127.0.0.1,127.0.0.3-127.0.0.15:9999"):
    print(ip)

(127.0.0.1, None),
(127.0.0.3, 9999),
...
(127.0.0.15, 9999),
luxos.ips.load_ips_from_csv(path: Path | str, port: int | None = 4028, strict: bool = False) list[tuple[str, int | None]]#

Load ip addresses from a csv file.

Parameters:
  • path – a Path object to load data from (csv-like)

  • port – a fallback port if not defined

  • strict – abort with AddressParsingError if there’s a malformed entry

Raises:

AddressParsingError – if strict is set to True and there’s a malformed entry in path.

Notes

The strict argument if set to False will ignore malformed lines in csv. If set to True it will raise AddressParsingError on invalid entries.

Example

foobar.csv file:

# comment (or empty lines) will be ignored
127.0.0.1 # a single address
127.0.0.2-127.0.0.10 # a range of addresses

# you can specify a port
127.0.0.11:9999
127.0.0.12-127.0.0.20:8888

You can read into a list of (host, port) tuples as:

for ip in load_ips_from_csv("foobar.csv"):
    print(ip)

(127.0.0.1, 4028)
(127.0.0.2, 4028)
(127.0.0.3, 4028)
...
(127.0.0.10, 4028)
luxos.ips.load_ips_from_yaml(path: Path | str, port: int | None = 4028, strict: bool = False) list[tuple[str, int | None]]#

Load ip addresses from a yaml file.

Parameters:
  • path – a Path object to load data from (csv-like)

  • port – a fallback port if not defined

  • strict – abort with AddressParsingError if there’s a malformed entry

Raises:

AddressParsingError – if strict is set to True and there’s a malformed entry in path.

Notes

The strict argument if set to False will ignore malformed lines in csv. If set to True it will raise AddressParsingError on invalid entries.

Example

foobar.yaml file:

miners:
    luxos_port: 9999  # default fallback port
    addresses:
        - 127.0.0.1 # a single address
        - 127.0.0.2-127.0.0.10 # a range of addresses

        # you can specify a port
        - 127.0.0.11:9999
        - 127.0.0.12-127.0.0.20:8888
luxos.ips.parse_expr(txt: str) None | tuple[str, str | None, int | None]#

parse text into a (start, end, port) tuple.

Parses a text in these forms:

<ip> <ip>:port <startip>:<endip> <startip>:<port>:<endip>

Eg.
>>> parse_expr("127.0.0.1")
("127.0.0.1", None, None)
>>> ips.parse_expr("127.0.0.1:1234:127.0.0.3")
("127.0.0.1", "127.0.0.3", 1234)
luxos.ips.splitip(txt: str) tuple[str, int | None]#