在此Twisted聊天服务器示例中,正确的格式是什么:适用于Python37的protocol.sendLinemessage?

问题描述

在这里使用Twisted documentation

中的Twisted聊天服务器示例

由于我使用的是Python 37,因此我需要进行一些修改

其中一些更改包括

if self.users.has_key(name):

 if name in self.users:

虽然让我有些难过的是

 protocol.sendLine(message)

(基本,我知道)。 为了进行快速而肮脏的测试,我将该行更改为:

protocol.sendLine(b"hello")

是的,是的,它每次在另一个打开的telnet会话中以“ hello”响应聊天用户。 但是,如何更改以使消息回显给其他用户,因为原始示例给出了一个错误

enter image description here

该示例中的函数为(已注释掉我的更改):

    def handle_CHAT(self,message):
    message = "<%s> %s" % (self.name,message)
    #for name,protocol in self.users.iteritems():
    for name,protocol in iter(self.users.items()):
        if protocol != self:
            #protocol.sendLine(b"hello")
            protocol.sendLine(message)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)