简体   繁体   English

如何在扭曲的python中从服务器发送列表中的内容?

[英]how to send the content in a list from server in twisted python?

I have a list related to multiclient chat server problem. 我有一个与多客户聊天服务器问题相关的列表。 This is the list ['talk', client_n, message]. 这是列表['talk',client_n,message]。 How can I send 'message' to 'client_n' from the server in twisted python using transport.write()? 如何使用transport.write()在twisted python中从服务器向'client_n'发送'message'?

I wrote the code in two ways. 我用两种方式编写代码。 But both are not working. 但两者都不起作用。

1st way: 第一种方式:

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

2nd 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 I got attribute error. 但在两者中我都有属性错误。

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

Can anybody give me a solution for this, please? 请问有人能为我解决这个问题吗?

What is self.factory.clients? 什么是self.factory.clients? Is it perhaps a dictionary? 它可能是字典吗? Do you really want to do something like: 你真的想做这样的事情:

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

我不是专家,但也许您应该考虑通过JSON序列化和反序列化您的列表?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM