I'm building a python server that basically has a bunch of ports listening for connections. when a connection is made, the port spits out a message, closes the connection and , resets (ie waits for the next connection).
desired function:
create TCP socket for each port in portlist (this is dynamically populated via arguments)
listen for connection
when a connection made, send message, and disconnect
continue to listen
I have a feeling that my answer may ly in multi threading, but I'm not sure...My code so far:
for port in portlist:
ds = ("0.0.0.0", port)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(ds)
server.listen(1)
while 1:
connection, address = server.accept()
message="message\n"
connection.send(message)
connection.close()