Python: How to send message to client from server at any time?
Question: I'm building a discussion board style server/client application were the
client connects to the server, is able to post messages, read messages, and
quit.

See client code below:

    
    
    import socket
    
    target_host = "0.0.0.0"
    target_port = 9996
    
    #create socket object
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    
    #connect the client
    client.connect((target_host,target_port))
    
    #receiving user name prompt
    print client.recv(1024)
    
    usrname = str(raw_input()) 
    
    #send username
    client.send(usrname)
    
    #check if username was unique
    while client.recv(1024) == "NU": #should exit once "ACK" received
        print client.recv(1024) #print username promt again
    
        usrname = str(raw_input()) #client enters in another username
        client.send(usrname)
    
    #receive user joined message/welcome message/help menu
    response = client.recv(1024)
    print response
    
    print client.recv(1024)
    
    #loops until client disconnects
    while True:
    
        request = str(raw_input("\n\nWhat would you like to do? "))
        client.send(request)
    
        if request == "-h":
            help_menu = client.recv(1024) 
            print help_menu
    
        elif request == "-p":
            #get subject
            subject_request = client.recv(1024)
            print subject_request
    
            subject = str(raw_input())
            client.send(subject)
    
            #get contents of post
            contents_request = client.recv(1024)
            print contents_request
    
            contents = str(raw_input())
            client.send(contents)
    
            #get post notification 
            message_post = client.recv(1024)
            print message_post
    
        elif request == "-r":
            #get id value of post 
            id_request = client.recv(1024)
            print id_request
    
            message_id = str(raw_input())
            client.send(message_id)
    
            #get contents of post 
            message_contents = client.recv(2048)
            print message_contents
    
        elif request == "-q":
           break
        else:
            print client.recv(1024)
    

When a client joins though, I wish to notify all other clients that are
connected that a new client has joined, but each client may be at a different
point in the code (some may be in the middle of a post, sitting idle at the
"what would you like to do?" statement, etc).

So how would I set my client code up that it will be able to accept a message
from the server the moment another client joins?


Answer: There are many ways to do it. Here an overview of how I would.

Are you familiar with
["select"](https://docs.python.org/2/library/select.html) operations? They
allow you to listen on multiple file descriptors and get notified whenever one
becomes active. I would start by using that to both listen for keyboard inputs
and server messages.

Then there are 2 things to be done. Branch depending on the active canal. If
it's a keyboard input you can relay the command to the server. If it's a
server message, you need to branch again on the message type to act
accordingly.

Edit: Never assume what the server message is about. Even though you may have
just sent a query, the server may be sending data about something else.

