私はTwistedの新人です。私が持っている問題と同様の多くの関連記事を読んでいます。しかし、私は単純な問題を解決するために以前の答えを推定することができません。私はTwistedのFAQセクションを参照しました - 私はまだ理解できません。ツイスト - サーバーからクライアントにデータを送信
私の問題は、1つのポートでリッスンしているサーバーがあることです。「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()
短い更新:私はこのよくある質問をチェックしました:http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Howdo他の人との間でコネクションを確立しようとしています。私は、FakeTelnetで確立されたクライアント接続をdataReceivedで利用できるようにしたい。どんな助けも非常に高く評価されます。ありがとうございました。 – user858302