微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在此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 举报,一经查实,本站将立刻删除。