From 373a79dd4d3ef1a4f36e0ff41e6ec217bef8029b Mon Sep 17 00:00:00 2001 From: Paolo Villaflores Date: Sun, 3 May 2020 11:09:18 +1000 Subject: [PATCH 1/3] Add support for arbitrary sending of messages within multiconn-server.py --- python-sockets-tutorial/multiconn-server.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/python-sockets-tutorial/multiconn-server.py b/python-sockets-tutorial/multiconn-server.py index 69075facce..0f68cdf489 100755 --- a/python-sockets-tutorial/multiconn-server.py +++ b/python-sockets-tutorial/multiconn-server.py @@ -7,6 +7,11 @@ sel = selectors.DefaultSelector() +pending_messages = [] # Capture pending messages: (fd,message) where fd is the socket fd, a numeric integer value. For example: + # The rest of the code can add items here to send messages. + # For example: + # pending_messages.append( (key.fd, message) ) + def accept_wrapper(sock): conn, addr = sock.accept() # Should be ready to read @@ -33,7 +38,9 @@ def service_connection(key, mask): print("echoing", repr(data.outb), "to", data.addr) sent = sock.send(data.outb) # Should be ready to write data.outb = data.outb[sent:] - + elif len(pending_messages) and pending_messages[0][0] == key.fd: + data.outb = pending_messages[0][1] + pending_messages.pop(0) if len(sys.argv) != 3: print("usage:", sys.argv[0], " ") From 0a9eed88cbe0cff2666f839a80430ce9d710cec3 Mon Sep 17 00:00:00 2001 From: Paolo Villaflores Date: Sun, 3 May 2020 11:13:03 +1000 Subject: [PATCH 2/3] Minor fix. --- python-sockets-tutorial/multiconn-server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-sockets-tutorial/multiconn-server.py b/python-sockets-tutorial/multiconn-server.py index 0f68cdf489..0f9ca26e1e 100755 --- a/python-sockets-tutorial/multiconn-server.py +++ b/python-sockets-tutorial/multiconn-server.py @@ -38,7 +38,7 @@ def service_connection(key, mask): print("echoing", repr(data.outb), "to", data.addr) sent = sock.send(data.outb) # Should be ready to write data.outb = data.outb[sent:] - elif len(pending_messages) and pending_messages[0][0] == key.fd: + elif len(pending_messages) > 0 and pending_messages[0][0] == key.fd: data.outb = pending_messages[0][1] pending_messages.pop(0) From eecf610ff0954c1899e42396462161efa08ba527 Mon Sep 17 00:00:00 2001 From: Paolo Villaflores Date: Sat, 16 May 2020 14:58:55 +1000 Subject: [PATCH 3/3] run flake8 and black --- python-sockets-tutorial/multiconn-server.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/python-sockets-tutorial/multiconn-server.py b/python-sockets-tutorial/multiconn-server.py index 0f9ca26e1e..5130d8fd7c 100755 --- a/python-sockets-tutorial/multiconn-server.py +++ b/python-sockets-tutorial/multiconn-server.py @@ -7,10 +7,15 @@ sel = selectors.DefaultSelector() -pending_messages = [] # Capture pending messages: (fd,message) where fd is the socket fd, a numeric integer value. For example: - # The rest of the code can add items here to send messages. - # For example: - # pending_messages.append( (key.fd, message) ) +pending_messages = [] +# +# Capture pending messages: (fd,message) +# where fd is the socket fd, a numeric integer value. For example: +# +# The rest of the code can add items here to send messages. +# +# pending_messages.append( (key.fd, message) ) +# def accept_wrapper(sock): @@ -42,6 +47,7 @@ def service_connection(key, mask): data.outb = pending_messages[0][1] pending_messages.pop(0) + if len(sys.argv) != 3: print("usage:", sys.argv[0], " ") sys.exit(1)