net.port-list
Parse a port list such as 22,80,8000-8010 into port numbers, in order and without duplicates.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 27 tests, run in TypeScript, Python and Rust.
What it does
Parses a port list as people type it on a command line or in a config file, `22,80,8000-8010`, into port numbers: each port once, in the order it was first given (a scan of `443,22` probes 443 first). Ranges are inclusive, and every port is checked with `net.port`'s `isPort`, so 0 and 65536 are refused.
## Accepted
For example
parse_port_list(22)→ 22 a single portparse_port_list(443,22,80)→ 443, 22, 80 a list keeps the order given, not sortedparse_port_list(8000-8003)→ 8,000, 8,001, 8,002, 8,003 an inclusive range
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
def parse_port_list(text: str) -> List[int]
| text | string | comma-separated ports and inclusive ranges low-high; spaces around items are allowed |
| returns | int[] | each port once, in the order first given |
Your code names it in one line, in the file that uses it
from fune.net.port_list import parse_port_list # net.port-list@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import re
from typing import List
from .net_port import is_port ← from net.port ^1.0.0 · built alongside by fune
NUMBER = re.compile(r"[0-9]+")
def _trim_blank(s: str) -> str:
"""Spaces and tabs only: a newline inside a port list is a mistake, not padding."""
return s.strip(" \t")
def _is_number(s: str) -> bool:
"""Whole ASCII digits with no leading zero: "80", not "080", "+80" or "٨٠"."""
return NUMBER.fullmatch(s) is not None and (len(s) == 1 or s[0] != "0")
def _port(token: str) -> int:
# More than five digits cannot be a port.
value = 0 if len(token) > 5 else int(token)
if not is_port(value):
raise ValueError('port "%s" is out of range 1-65535' % token)
return value
def parse_port_list(text: str) -> List[int]:
"""Parse a port list such as "22,80,8000-8010" into port numbers: each port
once, in the order first given. Ranges are inclusive.
"""
if not isinstance(text, str):
raise TypeError("text must be a string")
if _trim_blank(text) == "":
raise ValueError("port list is empty")
out: List[int] = []
seen = set()
def add(p: int) -> None:
if p not in seen:
seen.add(p)
out.append(p)
for raw in text.split(","):
item = _trim_blank(raw)
if item == "":
raise ValueError("empty item in port list")
if "-" in item:
ends = item.split("-")
if len(ends) != 2 or not _is_number(ends[0]) or not _is_number(ends[1]):
raise ValueError('invalid port range "%s"' % item)
low = _port(ends[0])
high = _port(ends[1])
if low > high:
raise ValueError('port range "%s" runs backwards' % item)
for p in range(low, high + 1):
add(p)
else:
if not _is_number(item):
raise ValueError('invalid port "%s"' % item)
add(_port(item))
return outInstall
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add net.port-list
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./net.port-list-1.0.0-python.fune, or fetch it from a terminal with fune pull net.port-list@1.0.0:python.
The whole function, every language, is one file too: net.port-list-1.0.0.fune, 12,546 bytes, sha256 a9386de8ca0833c7dca907585ed78b1740aafad25d5a3d15532e62b3ff6119fa. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
# fune: before net.port-list
after — your function gets the result and the arguments, and returns the final result.
# fune: after net.port-list
replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.
# fune: replace net.port in net.port-list
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show net.port-list --steps.
# fune: step net.port-list after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| a single port | 22 | → | 22 |
| a list keeps the order given, not sorted | 443,22,80 | → | 443, 22, 80 |
| an inclusive range | 8000-8003 | → | 8,000, 8,001, 8,002, 8,003 |
| ports and ranges mixed | 22,80,8000-8002 | → | 22, 80, 8,000, 8,001, 8,002 |
| spaces and tabs around items are allowed | 22 , 80 , 8000-8001 | → | 22, 80, 8,000, 8,001 |
| duplicates are dropped, keeping the first | 80,22,80,22 | → | 80, 22 |
| a port inside an earlier range is not repeated | 20-23,22,19 | → | 20, 21, 22, 23, 19 |
| a one-port range | 443-443 | → | 443 |
| the extremes 1 and 65535 | 65535,1 | → | 65,535, 1 |
| empty text | → | error: port list is empty |
Show the other 17 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| only spaces | → | error: port list is empty | |
| an empty item between commas | 22,,80 | → | error: empty item in port list |
| a trailing comma | 22,80, | → | error: empty item in port list |
| a word is not a port | 22,ssh | → | error: invalid port "ssh" |
| a leading plus is refused | +22 | → | error: invalid port "+22" |
| a leading zero is refused | 080 | → | error: invalid port "080" |
| a trailing newline is not trimmed | 22,80 | → | error: invalid port "80 " |
| Arabic-Indic digits are not port numbers | ٨٠ | → | error: invalid port "٨٠" |
| 0 is not a port | 0 | → | error: port "0" is out of range 1-65535 |
| 65536 is not a port | 65536 | → | error: port "65536" is out of range 1-65535 |
| six digits is out of range | 100000 | → | error: port "100000" is out of range 1-65535 |
| a range that runs backwards | 8010-8000 | → | error: port range "8010-8000" runs backwards |
| a range with a missing end | 8000- | → | error: invalid port range "8000-" |
| a negative port reads as a malformed range | -22 | → | error: invalid port range "-22" |
| a range with an out-of-range end | 65000-70000 | → | error: port "70000" is out of range 1-65535 |
| a three-part range | 1-2-3 | → | error: invalid port range "1-2-3" |
| a number instead of text is refused | 22 | → | error: text must be a string |
More from the author
- Items separated by commas; spaces and tabs around an item are ignored. - An item is a port (`80`) or an inclusive range (`8000-8010`). - Duplicates, including ports already covered by an earlier range, are dropped silently, keeping the first.
## Refused, each with its own error
| input | error | |-------|-------| | `""`, `" "` | `port list is empty` | | `22,,80`, `22,` | `empty item in port list` | | `ssh`, `+22`, `080`, `80\n`, non-ASCII digits | `invalid port "..."` | | `0`, `65536`, `100000` | `port "..." is out of range 1-65535` | | `8000-`, `-22`, `1-2-3` | `invalid port range "..."` | | `8010-8000` | `port range "8010-8000" runs backwards` |
Leading zeros are refused rather than read as decimal, because some tools read `080` as octal. A newline is not trimmed: a list read from a file should be split into lines first. Service names (`ssh`) are not looked up; use `net.port`'s `portService` to go the other way.
A very wide range such as `1-65535` is allowed and returns 65535 ports; a caller that sweeps should cap what it is willing to probe.
Files
| Path | Bytes |
|---|---|
| README.md | 1,407 |
| impl/python.py | 1,927 |
| impl/rust.rs | 2,412 |
| impl/typescript.ts | 1,903 |
| vectors.json | 2,753 |