mirror of
https://codeberg.org/ET-Kiel/tk102gpx.git
synced 2026-06-28 07:29:51 +02:00
Compare commits
2 commits
ac06d2300a
...
02e96d82a1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02e96d82a1 | ||
|
|
668c29245f |
1 changed files with 54 additions and 0 deletions
54
tk102cat.py
Executable file
54
tk102cat.py
Executable 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue