diff options
author | w0rp <devw0rp@gmail.com> | 2021-03-20 22:11:22 +0000 |
---|---|---|
committer | w0rp <devw0rp@gmail.com> | 2021-03-20 22:11:42 +0000 |
commit | b1d833417bcb57e265e0d01df07b28f463529d4b (patch) | |
tree | 11421aeca89127b0cc9417f8ff4cb0d725199f49 /test/script/dumb_tcp_server.py | |
parent | 3838ae118d8f05fa1b1be7952a1c8aa3055d6728 (diff) | |
download | ale-b1d833417bcb57e265e0d01df07b28f463529d4b.zip |
#3633 - Put all dummy test files in test/test-files
Diffstat (limited to 'test/script/dumb_tcp_server.py')
-rw-r--r-- | test/script/dumb_tcp_server.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/script/dumb_tcp_server.py b/test/script/dumb_tcp_server.py new file mode 100644 index 00000000..c15db65e --- /dev/null +++ b/test/script/dumb_tcp_server.py @@ -0,0 +1,40 @@ +""" +This Python script creates a TCP server that does nothing but send its input +back to the client that connects to it. Only one argument must be given, a port +to bind to. +""" +import os +import socket +import sys + + +def main(): + if len(sys.argv) < 2 or not sys.argv[1].isdigit(): + sys.exit('You must specify a port number') + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind(('127.0.0.1', int(sys.argv[1]))) + sock.listen(0) + + pid = os.fork() + + if pid: + print(pid) + sys.exit() + + while True: + connection = sock.accept()[0] + connection.settimeout(5) + + while True: + try: + connection.send(connection.recv(1024)) + except socket.timeout: + break + + connection.close() + + +if __name__ == "__main__": + main() |