Index: trunk/BNC/tests/tools/fakecaster.py
===================================================================
--- trunk/BNC/tests/tools/fakecaster.py	(revision 11074)
+++ trunk/BNC/tests/tools/fakecaster.py	(revision 11074)
@@ -0,0 +1,128 @@
+#!/usr/bin/env python3
+"""Minimal Ntrip caster that accepts uploads (Ntrip 1 SOURCE or Ntrip 2 POST
+with chunked transfer encoding) and stores the received stream as a BNC raw
+file, so that it can be decoded again with  bnc --file .
+
+Any number of (re)connections is accepted, as BNC may close and reopen the
+upload connection; the payload of all connections is stored in arrival order.
+The BNC raw format needs an epoch per chunk (BNC derives the current time from
+it, e.g. to resolve SSR epoch times); all data are written as chunks stamped
+with --time, which should be close to the epochs of the uploaded data.
+
+    fakecaster.py --port 21011 --out upload.raw --sta CLOCK --time 2026-10-01T09:30:00
+"""
+
+import argparse
+import socket
+import sys
+import threading
+
+
+def dechunk(buf):
+    """Remove HTTP chunked transfer encoding; returns (payload, rest, done)."""
+    out = bytearray()
+    while True:
+        i = buf.find(b"\r\n")
+        if i < 0:
+            return bytes(out), buf, False
+        try:
+            n = int(buf[:i].split(b";")[0], 16)
+        except ValueError:
+            return bytes(out), b"", True
+        if len(buf) < i + 2 + n + 2:
+            return bytes(out), buf, False
+        out += buf[i + 2:i + 2 + n]
+        buf = buf[i + 2 + n + 2:]
+        if n == 0:
+            return bytes(out), b"", True
+
+
+class Store:
+    def __init__(self, path, sta, fmt, time):
+        self.lock = threading.Lock()
+        self.out = open(path, "wb")
+        self.out.write(b"1 Version of BNC raw file\n")
+        self.head = "%s %s %s" % (time, sta, fmt)
+        self.total = 0
+
+    def write(self, data):
+        if data:
+            with self.lock:
+                self.out.write(("%s %d\n" % (self.head, len(data))).encode())
+                self.out.write(data)
+                self.out.write(b"\n")
+                self.out.flush()
+                self.total += len(data)
+
+
+def serve(conn, store, timeout):
+    conn.settimeout(timeout)
+    buf = b""
+    try:
+        while b"\r\n\r\n" not in buf:
+            d = conn.recv(4096)
+            if not d:
+                return
+            buf += d
+        header, _, pending = buf.partition(b"\r\n\r\n")
+        chunked = b"chunked" in header.lower()
+        if header.startswith(b"SOURCE"):
+            conn.sendall(b"ICY 200 OK\r\n\r\n")
+        else:
+            conn.sendall(b"HTTP/1.1 200 OK\r\nNtrip-Version: Ntrip/2.0\r\n\r\n")
+        while True:
+            if chunked:
+                payload, pending, done = dechunk(pending)
+                store.write(payload)
+                if done:
+                    return
+            else:
+                store.write(pending)
+                pending = b""
+            d = conn.recv(65536)
+            if not d:
+                return
+            pending += d
+    except (socket.timeout, OSError):
+        return
+    finally:
+        conn.close()
+
+
+def main():
+    ap = argparse.ArgumentParser(description=__doc__,
+                                 formatter_class=argparse.RawDescriptionHelpFormatter)
+    ap.add_argument("--port", type=int, required=True)
+    ap.add_argument("--out", required=True)
+    ap.add_argument("--sta", default="CLOCK", help="station ID written to the raw file")
+    ap.add_argument("--format", default="RTCM_3.3")
+    ap.add_argument("--time", required=True, help="ISO epoch for the raw file chunks")
+    ap.add_argument("--timeout", type=float, default=600.0,
+                    help="stop after this many seconds without a new connection or data")
+    args = ap.parse_args()
+
+    store = Store(args.out, args.sta, args.format, args.time)
+    srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+    srv.bind(("127.0.0.1", args.port))
+    srv.listen(5)
+    srv.settimeout(args.timeout)
+    print("listening", flush=True)
+
+    threads = []
+    while True:
+        try:
+            conn, _ = srv.accept()
+        except socket.timeout:
+            break
+        t = threading.Thread(target=serve, args=(conn, store, args.timeout), daemon=True)
+        t.start()
+        threads.append(t)
+    for t in threads:
+        t.join(args.timeout)
+    print("received %d bytes in %d connection(s)" % (store.total, len(threads)), flush=True)
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())
