diff --git a/README.md b/README.md index f0f3c00..dae56f5 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,14 @@ Teletext TCP Packet Server -------------------------- packet-server.py takes a teletext packet stream and serves it to multiple clients. -The script attempts to consume 42 byte packets from stdin every 20ms +The script attempts to consume 42 byte packets from stdin every 20ms unless an input filename is specified with `-i` test.py is a test client which connects to the server and outputs packets to stdout -The port is set using the -p argument, and the number of lines per field by -l +The port is set using the `-p` argument, and the number of lines per field by `-l` -The server can be tested using example.t42: -`(while cat example.t42; do :; done) | python3 packet-server.py -p 19761 -l 16` +The server can be tested using example.t42: +`python3 packet-server.py -p 19761 -l 16 -i example.t42` A suitable packet stream can be generated using https://github.com/peterkvt80/vbit2 -For example: `~/vbit2/vbit2 --dir ~/teletext | python3 packet-server.py -p 19761 -l 16` +For example: `vbit2 --dir ~/teletext | python3 packet-server.py -p 19761 -l 16` \ No newline at end of file diff --git a/packet-server.py b/packet-server.py index 2280cd8..2ebbe39 100644 --- a/packet-server.py +++ b/packet-server.py @@ -54,9 +54,10 @@ def server6(PORT): if __name__ == "__main__": PORT = 0 linesPerField = 0 + inputfile = sys.stdin try: - opts, args = getopt.getopt(sys.argv[1:],"p:l:") + opts, args = getopt.getopt(sys.argv[1:],"p:l:i:") except getopt.GetoptError as err: print(err) sys.exit(2) @@ -74,6 +75,12 @@ def server6(PORT): except: print("invalid lines per field") sys.exit(2) + elif opt in ('-i'): + try: + inputfile = open(arg, "r") + except: + print("failed to open file") + sys.exit(2) if PORT <= 1024 or PORT > 65535: print("invalid port, use -p ") @@ -94,7 +101,9 @@ def server6(PORT): starttime=time.time() while(True): - data = bytearray(sys.stdin.buffer.read(42 * linesPerField)) + data = bytearray(inputfile.buffer.read(42 * linesPerField)) + if not data: + inputfile.seek(0) # loop input file if (linesPerField < 16): data.extend(bytearray(42*(16-linesPerField))) for q in clientQueues[:]: