im not too bad at python, and ive tried to make a threaded socket client, but ive hit a problem in my code that i cant solve. Here is the code:
import socket
import threading
class ThreadedServer(object):
def __init__(self, host, port, num):
self.num = num
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
self.sock.listen(1)
while True:
client, address = self.sock.accept()
client.settimeout(600)
threading.Thread(target = self.listenToClient,args = (client,address)).start()
def listenToClient(self, client, address):
size = 1024
# fix this, please
if self.num == 1:
print("Client 1 connected")
if self.num == 2:
print("Client 2 connected")
if self.num == 3:
print("Client 3 connected")
while True:
data = client.recv(size)
data = data.decode("utf-8")
print("Client", self.num, ": ", data)
# Reply
if data == "lad":
response = "nice"
else:
response = data
client.send(response.encode('utf-8'))
if __name__ == "__main__":
while True:
host = input("Host? ")
port_num = input("Port? ")
try:
port_num = int(port_num)
print("Please connect client")
break
except ValueError:
pass
ThreadedServer(host,port_num, 1).listen()
ThreadedServer(host,port_num, 2).listen()
ThreadedServer(host,port_num, 3).listen()
ThreadedServer.listenToClient()
You see, when i run this, i also connect it to a rather simple client, which sends me data.
In this code, the data i receive is amply called 'data'.
The problem i have in this code is that in the section where i repeat 'if self.num:', i plan for it to give me the following code when i connect 3 client:
Client 1 connected
Client 2 connected
Client 3 connected
but instead it simply repeats 'Client 1 connected' 3 times.
It would be great if anyone could try to solve this problem for me, thanks in advance
Ps: If anyone would like the client too, just ask for it, and i will edit this post to add it in.
You get correct output according to the code you run. Your code has two problems. The first problem is in the main suite. Look at the line ThreadedServer(host,port_num, 1).listen(). It says: create object of class ThreadServer and call method listen() which runs forever (listens to connections forever). You will never start 2 more servers ThreadedServer(host,port_num, 2).listen() and ThreadedServer(host,port_num, 3).listen(). This is good but should be fixed. So you have 1 server which can accept multiple connections. But you count servers instead of clients (this is the second problem which gives you the output you do not expect).
The solution is the following.
Run only 1 server (1 object of class ThreadedServer).
Add counter to the method listen():
def listen(self):
counter = 0
self.sock.listen(1)
while True:
client, address = self.sock.accept()
client.settimeout(600)
counter += 1
threading.Thread(
target=self.listenToClient,
args=(client, address, counter)).start()
Change method listenToClient() a little bit:
def listenToClient(self, client, address, client_id):
size = 1024
print("Client {} connected".format(client_id))
while True:
data = client.recv(size)
data = data.decode("utf-8")
print("Client", self.num, ": ", data)
# Reply
if data == "lad":
response = "nice"
else:
response = data
client.send(response.encode('utf-8'))
And you will get output:
Client 1 connected
Client 2 connected
Client 3 connected
Related
I'm in the middle of a project that uses peer-to-peer and server-client architecture. I have a server that communicates with another device through sockets (I have three sockets for both server and device). Each socket is responsible for a certain type of information. I also need the tasks associated with each socket to work in parallel.
Let me explain further: I have a device (GPU) that is processing images. This GPU will need to send to server three different things in parallel. So I'm using three threads and three sockets. On the server side I have three threads("listeners") that will need to connect to the three GPU "clients". After there are three connections, three more threads are open one for each socket and task. Basically the gpu tries to connect to the server (3 sockets) and when the connections are made, each listener creates another thread for the task. When I open the Flask application everything works fine, the info is driven from server to browser without problems. But when I open the app on another tab or browser or computer, the server part hangs. At this point only one task is working on server side.
I would like to give you some code, but this is company property so I can't.
I'll give you a pseudo-code.
if __name__=="__main__"
threading.Thread(target=app.listen1).start()
app.run(threaded)
def listen1():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s1, socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s2, socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s3:
s1.bind((HOST, PORT))
s1.listen()
s2.bind((HOST, PORT2))
s2.listen()
s3.bind((HOST, PORT3))
s3.listen()
while True:
conn1, addr1 = s1.accept()
conn2, addr2 = s2.accept()
conn3, addr3 = s3.accept()
ident = json.loads(conn1.recv(1024))
if db.existStream(ident['id'])==True:
conn1.send(b'True')
threading.Thread(target=client1, args=(conn1, addr1)).start()
conn2.send(b'True')
threading.Thread(target=client2, args=(conn2, addr2)).start()
conn3.send(b'True')
threading.Thread(target=client3, args=(conn3, addr3)).start()
else:
conn1.send(b'False')
conn2.send(b'False')
conn3.send(b'False')
def client1(conn, addr):
buffer1=b''
while True:
length= int.from_bytes(conn.recv(1024), 'big')
if length==0:
break;
conn.send(b"ACK")
while len(buffer1)<length:
data = conn.recv(262144)
buffer1 += data
buffer2=json.loads(buffer1.decode('utf-8'))
overlay(buffer2['frame'], buffer2['sectorX'])
if 'confidence' in buffer2:
db.guardaFrame(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'], buffer2['azimuth'], buffer2['elevation'])
db.guardaAlerta(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'], buffer2['azimuth'], buffer2['elevation'], buffer2['confidence'])
else:
db.guardaFrame(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'], buffer2['azimuth'], buffer2['elevation'])
buffer1=b''
def client2(conn, addr):
buffer1=b''
while True:
length= int.from_bytes(conn.recv(1024), 'big')
if length==0:
break;
conn.send(b"ACK")
while len(buffer1)<length:
data = conn.recv(262144)
buffer1 += data
global Frame
Frame= b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + buffer1 + b'\r\n'
buffer1=b''
def client3(conn, addr):
buffer1=b''
while True:
length= int.from_bytes(conn.recv(1024), 'big')
if length==0:
break;
conn.send(b"ACK")
while len(buffer1)<length:
data = conn.recv(1024)
buffer1 += data
buffer2=json.loads(buffer1.decode('utf-8'))
global azimuth
azimuth=buffer2['azimuth']
So, in conclusion, everything works fine when the user only opens a browser window. When the user opens on another computer (of the same network), on in another tab, the clients hang and don't do their tasks (except one). I hope this is enough for you to understand.
Thanks
EDIT: Forgot to mention, the code should be able to accept more equal devices. I mean I need to "listen" for more connections.
EDIT2: So I decided to show the code for better understanding. The "If ..." is on another file.
Wrapping your threading in a class is a bad idea, since the instantiation of the class will force it to be single threaded.
class Server:
def __init__(self, port):
self.port = port
def client(self):
task
def listen(self):
socket created
while true:
do stuff
break
return
if __name__=="__main__"
ports = [123,124,125]
threads = [for p in ports in threading.Thread(Server(p).listen)]
threads.join()
Here is a non functioning example of how gevent works to make this async:
import gevent
from gevent import monkey, spawn
monkey.patch_all()
def listener(HOST, PORT, client):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s
s.bind((HOST, PORT))
s.listen()
while True:
conn, addr = s.accept()
ident = json.loads(conn.recv(1024))
if db.existStream(ident['id']) == True:
conn.send(b'True')
spawn(client,conn, addr)
else:
conn.send(b'False')
def client1(conn, addr):
buffer1 = b''
while True:
length = int.from_bytes(conn.recv(1024), 'big')
if length == 0:
break;
conn.send(b"ACK")
while len(buffer1) < length:
data = conn.recv(262144)
buffer1 += data
buffer2 = json.loads(buffer1.decode('utf-8'))
overlay(buffer2['frame'], buffer2['sectorX'])
if 'confidence' in buffer2:
db.guardaFrame(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'],
buffer2['azimuth'], buffer2['elevation'])
db.guardaAlerta(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'],
buffer2['azimuth'], buffer2['elevation'], buffer2['confidence'])
else:
db.guardaFrame(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'],
buffer2['azimuth'], buffer2['elevation'])
buffer1 = b''
def client2(conn, addr):
buffer1 = b''
while True:
length = int.from_bytes(conn.recv(1024), 'big')
if length == 0:
break;
conn.send(b"ACK")
while len(buffer1) < length:
data = conn.recv(262144)
buffer1 += data
global Frame
Frame = b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + buffer1 + b'\r\n'
buffer1 = b''
def client3(conn, addr):
buffer1 = b''
while True:
length = int.from_bytes(conn.recv(1024), 'big')
if length == 0:
break;
conn.send(b"ACK")
while len(buffer1) < length:
data = conn.recv(1024)
buffer1 += data
buffer2 = json.loads(buffer1.decode('utf-8'))
global azimuth
azimuth = buffer2['azimuth']
if __name__ == "__main__":
hosts = (('host1','80',client1),('host2','80',client2),('host3','80',client3))
threads = [spawn(listener, h,p,c) for h,p,c in hosts]
gevent.joinall(threads)
I'm working on a Minecraft server client bypasses but there's some errors that I don't know how to fix. The error says:
typeError: __init__() takes exactly 3 arguments (2 given)
I can't seems to find the issue. I already search everywhere for tutorials how to fix this, but can't find any.
Java balls lol ok new word ?? ok why
Here is my code:
import socket
import select
import time
import sys
buffer_size = 4096
delay = 0.0001
forward_to = ('domain.tld', 25565)
class Forward:
def __init__(self):
self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def start(self, host, port):
try:
self.forward.connect((host, port))
return self.forward
except Exception, e:
print e
return False
class TheServer:
input_list = []
channel = {}
def __init__(self, host, port):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind((host, port))
self.server.listen(200)
def main_loop(self):
self.input_list.append(self.server)
while 1:
time.sleep(delay)
ss = select.select
inputready, outputready, exceptready = ss(self.input_list, [], [])
for self.s in outputready:
if self.s == self.server:
self.on_accept()
break
self.data = self.s.recv(buffer_size)
if len(self.data) == 0:
self.on_close()
break
else:
self.on_recv()
def on_accept(self):
forward = Forward().start(forward_to)[0], forward_to[1]
clientsock, clientaddr = self.server.accept()
if forward:
print clientaddr, "You have connected"
self.input_list.append(clientsock)
self.input_list.append(forward)
self.channel[clientsock] = forward
self.channel[forwar] = clientsock
else:
print "Cannot connect to remote server."
print "Closing connection with the client side."
clientsock.close()
def on_close(self):
print self.s.getpeername(), "It has disconnected"
self.input_list.remove(self.s)
self.input_list.remove(self.channel[self.s])
out = self.channel[self.s]
self.channel[out].close()
self.channel[self.s].close()
del self.channel[out]
del self.channel[self.s]
def on_recv(self):
data = self.data
print data
self.channel[self.s].send(data)
if __name__ == '__main__':
server = TheServer('localhost, 12345')
try:
server.main_loop()
except KeyboardInterrupt:
print "CTRL C - Close The Server"
sys.exit(1)
I can't seems to find the issue. I already search everywhere for tutorials how to fix this, but can't find any.
I can't seems to find the issue. I already search everywhere for tutorials how to fix this, but can't find any.
When you execute server = TheServer('localhost, 12345'), you are calling the constructor (the __init__ function) of the class TheServer.
Take a look at this part of the code:
def __init__(self, host, port):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSADOR, 1)
self.server.bind((host, port))
self.server.listen(200)
You see 3 parameters: self, host and port. The first argument of every class method is always a reference to the current instance of the class. The other two are mandatory parameters that you must provide to the constructor when instanciating the class.
The problem is, in your call server = TheServer('localhost, 12345') you passed just one argument: a string 'localhost, 12345' so your class understands that the host is 'localhost, 12345' and the port is missing. That's the error: the class was expecting 3 parameters, but you gave just two. Replace the line with
server = TheServer('localhost', '12345')
I am writing a simple threaded server that will send a message to all clients. I have an object that is reset after posting the change message, however I am having a hard time figuring out how to reset that object only after all threads have posted the change message.
To add some context to the problem. I am building a multi user Tkinter python app which connects to a remote database to retrieve information and the application needs to know when data changes so that when a user updates data, all other running instances of the app will get the update. From what I understand, MySQL does not support asynchronous application updates. Instead of running a query every 5 seconds on the database to see if there is a change, I am putting this code server side so that it will send a message to a socket on the client that a change has occurred on the database.
The main loop is just a dummy that will simulate a change
Here is my code:
import socket, threading, time, select, os
class dbMonitor:
isDBAltered = False
def postChange(self):
self.isDBAltered = True
def __str__(self):
return str(self.isDBAltered)
class ThreadedServer(object):
def __init__(self, port,dbMon):
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setblocking(0)
self.sock.bind((socket.gethostname(), self.port))
self.dbMon = dbMon
def listen(self):
self.sock.listen(100)
read_list = [self.sock]
while True:
read,write,error = select.select(read_list,[],[],1)
for s in read:
if s is self.sock:
client, address = self.sock.accept()
client.settimeout(60)
threading.Thread(target = self.listenToClient, args = (client,address)).start()
def listenToClient(self, client, address):
read_list = [client]
size = 1024
while True:
response = b'Ack'
if self.dbMon.isDBAltered:
response = b'CHANGE'
try:
client.send(response)
except:
client.close()
return False
self.dbMon.isDBAltered = False
read,write,error = select.select(read_list,[],[],1)
for s in read:
if s is client:
try:
data = client.recv(size)
print(data)
if data:
client.send(response)
else:
raise error('Client disconnected')
except:
client.close()
return False
def mainLoop():
while True:
time.sleep(15)
print(dbMon)
dbMon.postChange()
dbMon = dbMonitor()
server = ThreadedServer(5005,dbMon)
threading.Thread(target = mainLoop, args=()).start()
threading.Thread(target = server.listen(), args=()).start()
How do I get self.dbMon.isDBAltered = False to execute only after all threads have executed:
response = b'CHANGE'
try:
client.send(response)
You're trying to synchronize something that's asynchronous... This is massively more complicated than it should be. Your dbmon is only storing a boolean flag... why not just asynchronously modify the "database" instead? For example, if the "database" was a thread-safe buffer, you could just append to that buffer or modify that buffer without synchronizing each thread individually, pull the information written to that buffer and write it to the client socket they belong to in another event loop (this is pretty much what asyncore does)
That said, I have some (probably nonworking, but I hope you get the idea) reference modified code for you to go off of if you want to continue pursing this avenue.
Basically, dbmon will keep a mapping of thread ids to [creation time, modified flag]
Our predicate returns true iff all threads created before a certain threshold have ALL set the modified flag. We set the modified flag when we send the response in the data = client.recv(size) portion of your code. And then we wait on that condition in the server send. We keep notifying all waiting threads on each client receive so that when the condition is finally met, our waiting server threads will all unblock and send the subsequent response.
import socket, threading, time, select, os
import collections
class dbMonitor:
def __init__(self):
self.isDBAltered = {}
self.lock = threading.Lock()
def newThread(self, tid):
self.lock.acquire()
# time of creation, boolean whether that thread has sent response
self.isDBAltered[tid] = [time.time(), False]
self.lock.release()
def threadDone(self, tid):
self.lock.acquire()
self.isDBAltered.pop(tid, None)
self.lock.release()
def altered(self, tid):
self.lock.acquire()
self.isDBAltered[tid][1] = True
self.lock.release()
def reset(self, tid):
self.lock.acquire()
self.isDBAltered(tid)[1] = False
self.lock.release()
def __str__(self):
return str(self.isDBAltered)
class ThreadedServer(object):
def __init__(self, port,dbMon):
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setblocking(0)
self.sock.bind((socket.gethostname(), self.port))
self.dbMon = dbMon
self.lock = threading.lock()
self.cv = threading.Condition()
self.thresh = 2000
def condition_pred(self):
# unblock if threads currently running for longer than self.thresh have all set their flags
return all([timecreate[1] if time.time() - timecreate[0] > self.thresh else True for tid,timecreate in self.dbMon.isDBAltered])
def listen(self):
self.sock.listen(100)
read_list = [self.sock]
while True:
read,write,error = select.select(read_list,[],[],1)
for s in read:
if s is self.sock:
self.lock.acquire()
client, address = self.sock.accept()
client.settimeout(60)
T = threading.Thread(target = self.listenToClient, args = (client,address)).start()
self.dbmon.newThread(T.ident)
self.lock.release()
def listenToClient(self, client, address):
read_list = [client]
size = 1024
while True:
response = b'Ack'
with self.cv:
self.cv.wait_for(self.condition_pred)
self.dbMon.reset(threading.get_ident())
response = b'CHANGE'
try:
client.send(response)
except:
client.close()
self.dbmon.threadDone(threading.get_ident())
return False
read,write,error = select.select(read_list,[],[],1)
for s in read:
if s is client:
with self.cv:
try:
data = client.recv(size)
print(data)
if data:
client.send(response)
self.dbMon.altered(threading.get_ident())
self.cv.notifyAll()
else:
raise error('Client disconnected')
except:
client.close()
self.dbmon.threadDone(threading.get_ident())
return False
I have the following python script (a bit inefficient, I know) for a P2P chat program using sockets:
#!usr/bin/env python
import socket import threading import select import time import datetime
def main():
class Chat_Server(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = 1
self.conn = None
self.addr = None
def run(self):
HOST = ''
PORT = 23647
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST,PORT))
s.listen(1)
self.conn, self.addr = s.accept()
# Select loop for listen
while self.running == True:
inputready,outputready,exceptready \
= select.select ([self.conn],[self.conn],[])
for input_item in inputready:
# Handle sockets
message = self.conn.recv(1024)
if message:
print "Daniel: " + message + ' (' + datetime.datetime.now().strftime('%H:%M:%S') + ')'
else:
break
time.sleep(0)
def kill(self):
self.running = 0
class Chat_Client(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.host = None
self.sock = None
self.running = 1
def run(self):
PORT = 23647
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host, PORT))
# Select loop for listen
while self.running == True:
inputready,outputready,exceptready \
= select.select ([self.sock],[self.sock],[])
for input_item in inputready:
# Handle sockets
message = self.sock.recv(1024)
if message:
print "Daniel: " + message + ' (' + datetime.datetime.now().strftime('%H:%M:%S') + ')'
else:
break
time.sleep(0)
def kill(self):
self.running = 0
class Text_Input(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = 1
def run(self):
while self.running == True:
text = raw_input('')
try:
chat_client.sock.sendall(text)
except:
Exception
try:
chat_server.conn.sendall(text)
except:
Exception
time.sleep(0)
def kill(self):
self.running = 0
# Prompt, object instantiation, and threads start here.
ip_addr = raw_input('Type IP address or press enter: ')
if ip_addr == '':
chat_server = Chat_Server()
chat_client = Chat_Client()
chat_server.start()
text_input = Text_Input()
text_input.start()
else:
chat_server = Chat_Server()
chat_client = Chat_Client()
chat_client.host = ip_addr
text_input = Text_Input()
chat_client.start()
text_input.start()
if __name__ == "__main__":
main()
This script works absolutely fine across a home network, with 192.168... internal IP addresses.
On a school network, with 172... IP addresses, it doesn't seem to work. There is no connection error, but messages are not sent or received, with the exception of if there are two instances of the application being run on the same computer with the same internal IP address, in which case the program works flawlessly.
I am very new to sockets and this sort of networking, so I am wondering if anyone can point out to me why this is the case. Might it be the difference in IP addresses, for example?
Thank you in advance.
I see you're connecting on port 23647 - you may not have access to this on your school network. Check if this port has traffic enabled. see: Port Forwarding for details.
Something like: this site/tool may allow you to check quickly.
I'm trying to understand my code's behavior.
I'm using zeromq to create a server that sends a "ping" and waits for "pong" responses.
What i'm seeing is that when I send a ping, only one client receives it.
when I run this code and send "ping" for the first time i receive:
pong: A
and when i run it again, i get
pong: B
why is that? I want to send one "ping" and receive two pongs.
here's the code:
from threading import Thread
import zmq
class zmqdealer(object):
def __init__(self, port):
context = zmq.Context()
self.sock = context.socket(zmq.DEALER)
#self.sock.setsockopt(zmq.RCVTIMEO, 1000)
self.sock.bind("tcp://*:%s" % port)
thread = Thread(target=lambda: self.poll())
thread.daemon = True
thread.start()
def poll(self):
while True:
reply = self.sock.recv()
if reply != "":
print(reply)
def ping(self):
self.sock.send_multipart(['', 'ping'])
class zmqrep(object):
def __init__(self, ident,host, port):
context = zmq.Context()
self.sock = context.socket(zmq.REP)
self.sock.connect("tcp://%s:%s" % (host, port))
self.ident = ident
thread = Thread(target=lambda: self.pong())
thread.daemon = True
thread.start()
def pong(self):
while True:
request = self.sock.recv()
if request == "ping":
msg = "pong: %s" % self.ident
self.sock.send(msg)
if __name__ == "__main__":
port = 11112
host = "localhost"
server = zmqdealer(port)
client1 = zmqrep('A',host,port)
client2 = zmqrep('B',host,port)
answer = raw_input('press <ENTER> to exit or type \'ping\' to get a pong\n')
while True:
if answer == "":
break
if answer == "ping":
server.ping()
answer = raw_input()
EDIT
I found a way to make this work. I really hope there is another way because i genuinely hate this one! so it looks like dealer sends to the clients in a round robin fashion. so to make my ping work i had to send it to all the clients. how? i subscribed to the monitor socket and added every connected client to a list. every time i ping, i ping to every client. look:
import threading
import zmq
from zmq.utils import monitor
def threadify(func, daemon=True):
thread = threading.Thread(target=func)
thread.daemon = daemon
thread.start()
class zmqdealer(object):
def __init__(self, port):
context = zmq.Context()
self.sock = context.socket(zmq.DEALER)
self.monitor_sock = self.sock.get_monitor_socket()
self.sock.bind("tcp://*:%s" % port)
self.connected_clients = {}
threadify(func=self.poll)
threadify(func=self.monitor)
def poll(self):
while True:
reply = self.sock.recv()
if reply != "":
print reply
def add_client(self, event):
endpoint = event['endpoint']
value = event['value']
if endpoint in self.connected_clients:
self.connected_clients[endpoint].append(value)
else:
self.connected_clients[endpoint] = [value]
def remove_client(self, event):
endpoint = event['endpoint']
value = event['value']
if endpoint in self.connected_clients \
and value in self.connected_clients[endpoint]:
self.connected_clients[endpoint].remove(value)
def monitor(self):
options = {zmq.EVENT_ACCEPTED: lambda e: self.add_client(e),
zmq.EVENT_DISCONNECTED: lambda e: self.remove_client(e)}
while True:
event = monitor.recv_monitor_message(self.monitor_sock)
event_type = event['event']
if event_type in options:
options[event_type](event)
event['event'] = event_types[event_type]
print event
def ping(self):
connected_clients_amount = sum([len(clients) for clients in self.connected_clients.values()])
for i in xrange(connected_clients_amount):
self.sock.send_multipart(['', 'ping'])
if connected_clients_amount <= 0:
print "there are no connected clients!"
class zmqrep(object):
def __init__(self, ident, host, port):
context = zmq.Context()
self.sock = context.socket(zmq.REP)
self.sock.connect("tcp://%s:%s" % (host, port))
self.identity = ident
self.stopped = threading.Event()
threadify(self.pong)
def pong(self):
while not self.stopped.isSet():
request = self.sock.recv()
if request == "ping":
msg = "pong: %s" % self.identity
self.sock.send(msg)
self.sock.close()
def stop(self):
self.stopped.set()
if __name__ == "__main__":
port = 11112
host = "localhost"
num = 5
server = zmqdealer(port)
clients = [zmqrep(i.__str__(), host, port) for i in xrange(num)]
answer = raw_input('press <ENTER> to exit or type \'ping\' to get a pong\n')
while True:
if answer == "":
break
if answer == "ping":
server.ping()
if answer == "kill":
if len(clients) > 0:
die = clients[0]
clients.remove(die)
die.stop()
else:
print "there are no connected clients!\n"
answer = raw_input()
Router/Dealer sockets are best used for distributing tasks. Say you have 10 tasks and 2 workers, you do not care who does what. Dealer/Router will distribute in a round robin fashion.
Maybe Pub/Sub or Push/Pull sockets would fit your usecase better? They are both broadcast sockets.
Here's an example of Push/Pull used in a similar fashion as what you're doing.
You often end up doing pairs of sockets, one to transmit and one other to receive results. You could for example do a PUSH with a ping message + random identifier, and ask clients to answer on PUB/SUB where you subscribe to this random identifier. This way you can match requests and responses.