mirror of
https://codeberg.org/ET-Kiel/tk102gpx.git
synced 2026-06-28 07:29:51 +02:00
Compare commits
No commits in common. "02e96d82a1ff32038e62a81070d7bd06a40182bb" and "ac06d2300a6dca61845ac973bbdd8b6486a7eef9" have entirely different histories.
02e96d82a1
...
ac06d2300a
1 changed files with 0 additions and 54 deletions
54
tk102cat.py
54
tk102cat.py
|
|
@ -1,54 +0,0 @@
|
||||||
#! /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()
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue