gpt4 book ai didi

python - Twisted - 向选定的客户端发送数据

转载 作者:行者123 更新时间:2023-11-28 17:48:39 24 4
gpt4 key购买 nike

有一个我用 Twisted 制作的服务器,它从客户端接收字符串并将其发送到所有其他连接的客户端。但是有没有办法将字符串发送给发件人想要发送给的客户?如果是这样,我该如何在代码中做到这一点?这是我到目前为止所做的(注意我是 Python 的完全菜鸟。我只需要为我的 iOS 应用程序构建一个服务器,如果这个问题很愚蠢,我很抱歉):

from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor


class IphoneChat(Protocol):
def connectionMade(self):
#self.transport.write("""connected""")
self.factory.clients.append(self)
print "clients are ", self.factory.clients

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

def dataReceived(self, data):
#print "data is ", data
a = data.split(':')
if len(a) > 1:
command = a[0]
content = a[1]

msg = ""
if command == "iam":
self.name = content
msg = "iam" + self.name + " has joined"

elif command == "msg":
msg = self.name + ": " + content

elif command == "img":
msg = command + ":" + content + ":" + command

elif command == "img2":
msg = content

elif command == "img3":
msg = content

print msg

for c in self.factory.clients:
c.message(msg)

def message(self, message):
self.transport.write(message + '\n')


factory = Factory()
factory.protocol = IphoneChat
factory.clients = []

reactor.listenTCP(53080, factory)
print "Iphone Chat server started"
reactor.run()

感谢帮助

最佳答案

self.factory.clients[24].transport.write("You are client 24!")

我觉得应该可以

关于python - Twisted - 向选定的客户端发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14128455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com