python – Twisted – 将数据从服务器发送到客户端
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Twisted – 将数据从服务器发送到客户端,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3122字,纯文字阅读大概需要5分钟。
内容图文
![python – Twisted – 将数据从服务器发送到客户端](/upload/InfoBanner/zyjiaocheng/799/a5034b4a87f0424db57d7e74180873d3.jpg)
我是Twisted的新手,并且阅读了许多与我遇到的问题类似的相关帖子.但是,我无法推断以前的答案来解决我的简单问题.我确实参考了Twisted的FAQ部分 – 我仍然无法弄清楚.
我的问题是我有一个服务器在一个端口监听,当我收到一个“START”命令时,我想和几个客户交谈.作为一个例子,我使用了一个客户端,它为我提供了一个幸运饼干.但是,我无法在服务器代码中与客户端通信.你能告诉我哪里出错吗?这是代码:
from twisted.internet import reactor, protocol
from twisted.internet.protocol import Protocol, Factory
class FakeTelnet(Protocol):
def connectionMade(self):
print 'local connection made'
self.otherFact = protocol.ClientFactory()
self.otherFact.protocol = EchoClient
self.factory.clients.append(self.otherFact.protocol)
reactor.connectTCP('psrfb6',10999, self.otherFact)
def dataReceived(self, data):
if 'START' in data:
# send a command to cookie server.
for client in self.factory.clients:
client.transport.write('START\r\n')
def connectionLost(self):
print 'connection lost'
class EchoClient(Protocol):
"""Once connected, send a message, then print the result."""
def connectionMade(self):
print "Connection to cookie server"
def dataReceived(self, data):
"As soon as any data is received, write it back."
print "Fortune Server said:", data
def connectionLost(self):
print "connection lost"
def send_stuff(data):
self.transport.write(data);
class MyFactory(Factory):
protocol = FakeTelnet
def __init__(self, EchoClient):
self.clients = []
self.otherCli = EchoClient
reactor.listenTCP(5823, MyFactory(EchoClient))
reactor.run()
解决方法:
class FakeTelnet(Protocol):
def connectionMade(self):
print 'local connection made'
self.otherFact = protocol.ClientFactory()
self.otherFact.protocol = EchoClient
在这里,您将self.otherFact.protocol设置为EchoClient类.
self.factory.clients.append(self.otherFact.protocol)
在这里,您将EchoClient类添加到self.factory.clients列表中.这只会导致self.factory.clients一遍又一遍地成为EchoClient类的列表.
def dataReceived(self, data):
if 'START' in data:
# send a command to cookie server.
for client in self.factory.clients:
client.transport.write('START\r\n')
在这里,您尝试写入EchoClient.transport,它通常是None,因为它只是连接到传输的协议类的实例,而不是类本身.
尝试将连接的实例添加到self.factory.clients中.我想你正在创建这样的实例,因为你在FakeTelnet.connectionMade中也有这一行:
reactor.connectTCP('psrfb6',10999, self.otherFact)
但是,您没有做任何可以让连接协议进入self.factory.clients列表的操作.也许你可以这样定义self.otherFact:
class OtherFactory(ClientFactory):
protocol = EchoClient
def __init__(self, originalFactory):
self.originalFactory = originalFactory
def buildProtocol(self, addr):
proto = ClientFactory.buildProtocol(self, addr)
self.originalFactory.clients.append(proto)
return proto
您还需要在某些时候从客户端列表中删除内容.也许在connectionLost回调协议中:
class EchoClient(Protocol):
...
def connectionLost(self, reason):
self.factory.originalFactory.clients.remove(self)
最后,如果您正在处理面向行的数据,您可能希望使用twisted.protocols.basic.LineOnlyReceiver作为FakeTelnet的基类,并将您的逻辑放在其lineReceived回调中. dataReceived回调不保证数据边界,因此您可能永远不会看到包含“START”的单个字符串.例如,您可能会收到两次对dataReceived的调用,一次调用“ST”,另一次调用“ART”.
内容总结
以上是互联网集市为您收集整理的python – Twisted – 将数据从服务器发送到客户端全部内容,希望文章能够帮你解决python – Twisted – 将数据从服务器发送到客户端所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。