connlog — a connection journal, live in a window
connlog is a LightGUI (Python/XFrame) port of the connlog utility from util_box: log new TCP connections over time (poll /proc/net/tcp) and report new/closed ones. The twist versus watching ss is that it is a connection journal — you see the churn, short-lived connections that come and go between snapshots, instead of a single frozen moment. It is one of the 50 apps in util_boxgui, the project recreating util_box utilities as LightGUI apps. The full source is apps/python/connlog/connlog.py in that repo.
Polling starts on launch — every 2 seconds, the CLI default — and the baseline line is already in the log when the window appears. Every connection that appears or disappears after that gets its own timestamped NEW / CLOSED line. Click Stop to pause the journal, Start to resume it.
The GUI layout

The window is 640×480 and holds three widgets, created with create() + set_prop() and drawn on a single Gtk.DrawingArea:
- Status label: a
GtkLabelshowing the baseline count of tracked TCP connections, how many opened and closed since launch, and whether polling is running or paused. - Stop/Start button: a
GtkButtonthat pauses polling (its label becomesStart) or resumes it. - Log area: a
GtkTextViewfilling the rest of the window, showing the timestamped journal in a 13-point monospace font, with scroll bars. It is read-only — clicking it just drops focus.
Two honest limitations, both inherited from how LightGUI draws its widgets with Cairo on one drawing area: there is no native GTK keyboard focus — this app has no text entry, so there is nothing to type, and the only interaction is the Stop/Start button; and the log area does not really scroll — the text-view scroll bars are drawn, not functional. The log keeps the newest 200 lines, and long lines (IPv6 addresses) are clipped at the right edge with an ellipsis. A third limitation is the app's own: the CLI's -i / -t flags have no GUI equivalent — the interval is fixed at the CLI default of 2 seconds, and polling runs indefinitely until the window closes.
The source, in sections
connlog.py is about 330 lines. It is modelled on the LightGUI Python examples gtkbutton.py and gtktextview.py: widgets are created with create() + set_prop(), drawn on a Gtk.DrawingArea by widget.draw(cr), hit-tested on click, and dispatched through a _HANDLERS table. The live polling is a GLib.timeout_add() callback on the GTK main loop — the same loop the harness already runs. The polling half, though, is not modelled on anything — it is vendored verbatim from the CLI, so the GUI can never drift from it.
The snapshot. STATES, parse_addr() and snapshot() are copied byte-for-byte from the CLI script. /proc/net/tcp and /proc/net/tcp6 are read, each connection is keyed by local+remote+state, and IPv4 addresses are un-reversed from the hex form in /proc — 0100007F is 127.0.0.1, not 1.0.0.127:
STATES = {1: "ESTABLISHED", 2: "SYN_SENT", 3: "SYN_RECV", 4: "FIN_WAIT1",
5: "FIN_WAIT2", 6: "TIME_WAIT", 7: "CLOSE", 8: "CLOSE_WAIT",
9: "LAST_ACK", 10: "LISTEN", 11: "CLOSING"}
def parse_addr(s):
try:
ip_hex, port_hex = s.split(":")
except ValueError:
return s
if len(ip_hex) == 8:
b = bytes.fromhex(ip_hex)
return f"{b[3]}.{b[2]}.{b[1]}.{b[0]}:{int(port_hex, 16)}"
return f"[v6:{ip_hex}]:{int(port_hex, 16)}"
def snapshot():
conns = {}
for path in ("/proc/net/tcp", "/proc/net/tcp6"):
try:
with open(path) as f:
next(f)
for line in f:
parts = line.split()
if len(parts) < 4:
continue
try:
state = int(parts[3], 16)
except ValueError:
continue
key = (parse_addr(parts[1]), parse_addr(parts[2]), STATES.get(state, f"STATE{state}"))
conns[key] = True
except OSError:
pass
return connsOne poll tick. _poll() is the CLI's run() loop, one iteration at a time: diff the new snapshot against the previous one, journal every NEW and CLOSED connection, update the counters and the status label. It returns True so the timer keeps firing:
def _poll():
if _RUNNING[0]:
cur = snapshot()
for key in cur:
if key not in _PREV[0]:
_log("NEW %s -> %s [%s]" % key)
_OPENED[0] += 1
for key in _PREV[0]:
if key not in cur:
_log("CLOSED %s -> %s [%s]" % key)
_CLOSED[0] += 1
_PREV[0] = cur
_status()
return TrueWiring it up. main() takes the baseline snapshot and writes the first journal line before the window even appears — exactly like the CLI's run() before its first poll — then arms the 2-second timer:
_PREV[0] = snapshot()
_log("baseline: %d TCP connections tracked" % len(_PREV[0]))
_status()
GLib.timeout_add(INTERVAL_MS, _poll)
window.show_all()
Gtk.main()The Stop/Start button flips a flag the timer checks, so no timer juggling is needed: pause stops the diffing, resume picks it up from the latest snapshot.
Building and running it
There is nothing to build — it is pure Python. Run it with the LightGUI Python package on PYTHONPATH, under a real X server or under Xvfb (display :100 is the one used for this app). It needs python3-gi / gir1.2-gtk-3.0 and the LightGUI Python checkout:
cd apps/python/connlog
PYTHONPATH=/root/lg-review/lightgui_python python3 connlog.py
# Under Xvfb:
Xvfb :100 -screen 0 1024x768x24 &
DISPLAY=:100 PYTHONPATH=/root/lg-review/lightgui_python python3 connlog.pyThe PYTHONPATH above points at the LightGUI Python checkout on the build machine — on your own machine, point it at wherever you cloned LightGUI Python. The window title is connlog - util_boxgui. It reads only /proc/net/tcp and /proc/net/tcp6, so it needs no privileges — but on a quiet machine the journal stays at the baseline line until something connects, which is the correct, boring result.
What the CLI version does differently
Same polling, same connection keys, same journal lines, same baseline — and the differences are all about the medium. The CLI is connlog [-i SECONDS] [-t SECONDS], polls for a fixed total time (30 seconds by default), then prints a summary: N opened, M closed line and exits; the GUI polls indefinitely until the window closes, and the opened/closed totals live in the status label instead of a final summary line. The CLI's -i / -t flags have no GUI equivalent. And the CLI prints to stdout; the GUI shows the journal in the log area and never writes to stdout. The snapshot parsing was verified against the CLI's own self-test — two live snapshots of /proc/net/tcp{,6} parse cleanly, and the journal lines match the CLI's format exactly.
See also
- util_boxgui — the project this app belongs to: all 50 apps, with screenshots
- connlog.py — the full source
- connlog — the util_box CLI this ports
- XFrame widgets
- All 547 Python widgets