Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for arbitrary sending of messages within multiconn-server.py #112

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions python-sockets-tutorial/multiconn-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

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.
#
# pending_messages.append( (key.fd, message) )
#


def accept_wrapper(sock):
conn, addr = sock.accept() # Should be ready to read
Expand All @@ -33,6 +43,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) > 0 and pending_messages[0][0] == key.fd:
data.outb = pending_messages[0][1]
pending_messages.pop(0)


if len(sys.argv) != 3:
Expand Down