如何在twisted python中将列表从服务器发送到客户端?

发布于 2024-12-18 20:14:04 字数 1404 浏览 1 评论 0原文

我正在用twisted python 做一个多客户端聊天服务器程序。在我的程序中,如果我们从一个客户端发送“列表”,服务器必须将已连接客户端的列表发送到该客户端。此外,当我们从一个客户端发送“talk clientname 消息”时,服务器必须将该消息发送到“clientname”中指定的目标客户端。但我的代码不起作用。服务器有错误。不显示列表并且通话也不起作用。

我的服务器代码如下:

class MultiEcho(Protocol):

    def __init__(self, factory):
        self.factory = factory

    def connectionMade(self):
        self.factory.clients.append(self)

    def dataReceived(self,data):
        data = data.strip()

        if (data == "list"):
            for client in self.factory.clients:
                print self.factory.clients
                self.transport.write(self.factory.clients)
        else:
            data = data.split()
                    if (len(data) > 1):
                l = data[1]
                m = data[2]
                l.transport.write(m)

    def connectionLost(self,reason):
        self.factory.clients.remove(self)       

class MultiEchoFactory(Factory):

    def __init__(self):
        self.clients = []
    def buildProtocol(self, addr):
        return MultiEcho(self)
if __name__ == '__main__':

    import sys
    if len(sys.argv) != 4:
        print "Sorry.. not correct.. Try Again!"
        sys.exit(1)
    else:
        if (sys.argv[1] == "chatserver") and (sys.argv[2] == "-p"):
            PORT = sys.argv[3]
    reactor.listenTCP(8000, MultiEchoFactory())
    reactor.run()   

任何人都可以给我一个解决方案吗?

I am doing a multiclient chat server program in twisted python. In my program, if we send 'list' from one client, server has to send the list of connected clients to that client. Also, when we send 'talk clientname message' from one client, server has to send that message to the destination client specified in the 'clientname'. But my code is not working. there are mistakes in server. Not displays the list and also talk is not working.

My server code is given below:

class MultiEcho(Protocol):

    def __init__(self, factory):
        self.factory = factory

    def connectionMade(self):
        self.factory.clients.append(self)

    def dataReceived(self,data):
        data = data.strip()

        if (data == "list"):
            for client in self.factory.clients:
                print self.factory.clients
                self.transport.write(self.factory.clients)
        else:
            data = data.split()
                    if (len(data) > 1):
                l = data[1]
                m = data[2]
                l.transport.write(m)

    def connectionLost(self,reason):
        self.factory.clients.remove(self)       

class MultiEchoFactory(Factory):

    def __init__(self):
        self.clients = []
    def buildProtocol(self, addr):
        return MultiEcho(self)
if __name__ == '__main__':

    import sys
    if len(sys.argv) != 4:
        print "Sorry.. not correct.. Try Again!"
        sys.exit(1)
    else:
        if (sys.argv[1] == "chatserver") and (sys.argv[2] == "-p"):
            PORT = sys.argv[3]
    reactor.listenTCP(8000, MultiEchoFactory())
    reactor.run()   

Can anybody give me a solution, please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2024-12-25 20:14:04

您需要为客户端定义clientname,例如,工厂应该能够根据其名称找到客户端。然后你可以在 LineReceived() 中:

command, _, rest = line.partition(command_separator)
if command == "talk":
   clientname, _, message = rest.partition(arg_separator)
   self.factory.getClient(clientname).sendLine(message)
  • you can't rely how much data dataReceived() receives. You might need to subclass from LineReceiver and use lineReceived() to process input line by line.
  • transport.write() accepts a string, not a list.

You need to define clientname for a client e.g., a factory should be able to find a client given its name. Then you could in LineReceived():

command, _, rest = line.partition(command_separator)
if command == "talk":
   clientname, _, message = rest.partition(arg_separator)
   self.factory.getClient(clientname).sendLine(message)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文