python - Client-Server socket setup, how to respond to a determined client -
this question has been edited focus in simpler problem
so have basic client-server socket installation, in client send json {'id': '1', 'value': 'a'}
. @ server side, if receive message id 2
want send message client id 1
, telling him new value
c
.
this message should "private", i.e., id 1
should receive it, no broadcasting allowed.
how should approach problem? how keep track of connections @ server side send message determined client? problem it's server 1 sending message client, not responding client's message. guess must combination of threading , queues, still haven't figured out how it.
this code have right @ server, keeping track of clients using dict, it's not working (bad file descriptor @ sendall('c')
line:
track_clients = {} while true: print "waiting connection" connection, client_address = sock.accept() try: print "connection ", client_address data = json.loads(connection.recv(1024)) track_clients[data['id']] = connection if data['id'] == '2': conn = track_clients['1'] conn.sendall('c') connection.sendall(json.dumps(data)) finally: connection.close()
you can have @ channels http://channels.readthedocs.org/en/latest/. alongside redis (https://pypi.python.org/pypi/redis/)
Comments
Post a Comment