我是扭曲的新手,並閱讀了許多與我所遇到的問題類似的相關文章。但是,我無法推斷以前的答案來解決我的簡單問題。我確實參考了扭曲的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()
一個簡短的更新:我檢查了這個FAQ:http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother - 但仍然無法擺脫它。我想要的是使用在FakeTelnet中建立的客戶端連接在dataReceived中可用。任何幫助將非常感激。謝謝。 – user858302