Created
October 1, 2016 12:55
-
-
Save royvandam/633918d9a549d1f1e2ee20c600464f6e to your computer and use it in GitHub Desktop.
Small python script that connects one or more TCP servers to each other.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import socket | |
import select | |
def usage(name): | |
print('Usage {} [hostname:port](2..n)'.format(name)) | |
if (len(sys.argv) < 3): | |
usage(sys.argv[0]) | |
sys.exit(1) | |
connections = [] | |
for endpoint in sys.argv[1:]: | |
hostname, port = endpoint.split(':') | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((hostname, int(port))) | |
connections.append(s) | |
print("Connected to {}".format(endpoint)) | |
fd_set = set([ c.fileno() for c in connections ]) | |
fd_map = { c.fileno(): c for c in connections } | |
while True: | |
r, w, x = select.select(fd_set, [], []) | |
for fd in r: | |
data = fd_map[fd].recv(4096) | |
if not len(data): | |
sys.exit(0) | |
for c in connections: | |
if c.fileno() == fd: | |
continue | |
c.sendall(data) |
Author
royvandam
commented
Oct 1, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment