如何在twisted python中将列表从服务器发送到客户端?
我正在用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dataReceived()
收到的数据量。您可能需要从LineReceiver
< /a> 并使用lineReceived()
逐行处理输入。transport.write()
接受字符串,而不是列表。您需要为客户端定义
clientname
,例如,工厂应该能够根据其名称找到客户端。然后你可以在LineReceived()
中:dataReceived()
receives. You might need to subclass fromLineReceiver
and uselineReceived()
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 inLineReceived()
: