Compare commits

...

2 commits

Author SHA1 Message Date
Stephan I. Böttcher
02e96d82a1 Merge branch 'master' of codeberg.org:ET-Kiel/tk102gpx 2023-09-16 19:20:13 +02:00
Stephan I. Böttcher
668c29245f new script tk102cat.py
netcat does not cut it.  The loop accepts at most one connection at each
time.  There will eb overlap when the mobile cell changes.

This script accepts up to 100 connections and merges whatever comes in.
2023-09-16 19:17:45 +02:00

54
tk102cat.py Executable file
View file

@ -0,0 +1,54 @@
#! /usr/bin/python3
import os, sys, time, select, socket, getopt, fcntl
port = 10201
out = sys.stdout.buffer
files = []
timeout = None
def logger(s):
print(s, file=sys.stderr)
oo,files = getopt.getopt(sys.argv[1:], "p:", ("port=",))
for o,v in oo:
if o=="-p" or o=="--port":
port = int(v,0)
poll = select.poll()
sockets = {}
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", port))
sock.listen(2)
poll.register(sock, select.POLLIN)
logger("listening on port %d, socket fd %d" % (port,sock.fileno()))
while True:
c = poll.poll(timeout)
for fd, ev in c:
if fd==sock.fileno():
s = sock.accept()
logger("connection from %s" % repr(s[1]))
if len(sockets) > 99:
logger("too many connections %s" % repr(sockets))
s[0].send("too many connections, good bye")
s[0].close()
else:
sockets[s[0].fileno()] = s
poll.register(s[0], select.POLLIN | select.POLLHUP | select.POLLRDHUP)
elif fd in sockets:
so = sockets[fd][0]
if ev & select.POLLIN:
d = so.recv(4096, socket.MSG_DONTWAIT)
if d:
out.write(d)
out.flush()
if ev & (select.POLLHUP | select.POLLRDHUP):
logger("closing connection from %s" % repr(sockets[fd][1]))
del sockets[fd]
poll.unregister(fd)
so.close()