How to send contents to a list from a server in twisted python?

I have a list related to multi-client chat problem. This is the list ['talk', client_n, message]. How can I send the "client_n" message from the server in twisted python using transport.write ()?

I wrote the code in two ways. But both of them do not work.

First way:

data = data.strip() dat1 = data.split() dat2 = ' '.join(dat1) l = dat2[5:12] m = dat2[13:] l.transport.write(m) 

The second way:

  data = data.strip() dat1 = data.split() l1 = dat1[1] m1 = dat1[2] if l1 in self.factory.clients: l1.transport.write(m1) 

But in both cases, I got an attribute error.

 exceptions.AttributeError: 'str' object has no attribute 'transport' 

Can someone please give me a solution for this, please?

+4
source share
2 answers

What is self.factory.clients? Perhaps this is a dictionary? You really want to do something like:

 client = self.factory.clients.get(client_n) if client: client.transport.write(message) 
+6
source

I'm not an expert, but maybe you should consider serializing and deserializing the list via JSON ?

0
source

Source: https://habr.com/ru/post/1383869/


All Articles