public
Created

TCP server that listens for connections but never replies. Useful for testing certain kinds of timeout behaviors.

  • Download Gist
okcupidgirl.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# okcupidgirl.py ignores you
from __future__ import print_function
import argparse
import socket
import select
 
 
def listen(port):
print("Listening on port {}".format(port))
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)
server.bind(("", port))
server.listen(1)
inputs = [server]
outputs = []
while True:
ready, _, _ = select.select(inputs, outputs, inputs)
for s in ready:
if s is server:
conn, addr = s.accept()
conn.setblocking(0)
inputs.append(conn)
print("Accepting connection from ", addr)
else:
data = s.recv(1024)
if data:
print("Ignoring data from ", s.getpeername(), ": ", data)
else:
print("Closing connection from ", s.getpeername())
s.close()
inputs.remove(conn)
 
 
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Accept connections, never reply')
parser.add_argument("port", type=int)
args = parser.parse_args()
 
listen(args.port)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.