Skip to content

Instantly share code, notes, and snippets.

@royvandam
Created October 1, 2016 12:55
Show Gist options
  • Save royvandam/633918d9a549d1f1e2ee20c600464f6e to your computer and use it in GitHub Desktop.
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.
#!/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)
@royvandam
Copy link
Author

Death by electrocution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment