2011-07-22 168 views
4

我是扭曲的新手,並閱讀了許多與我所遇到的問題類似的相關文章。但是,我無法推斷以前的答案來解決我的簡單問題。我確實參考了扭曲的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() 
+0

一個簡短的更新:我檢查了這個FAQ:http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother - 但仍然無法擺脫它。我想要的是使用在FakeTelnet中建立的客戶端連接在dataReceived中可用。任何幫助將非常感激。謝謝。 – user858302

回答

5
class FakeTelnet(Protocol): 
    def connectionMade(self): 
     print 'local connection made' 
     self.otherFact = protocol.ClientFactory() 
     self.otherFact.protocol = EchoClient 

在這裏,你設置self.otherFact.protocolEchoClient

 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 

你也想clients列表中的某個時間取出事情。也許在connectionLost回調協議:

class EchoClient(Protocol): 
    ... 
    def connectionLost(self, reason): 
     self.factory.originalFactory.clients.remove(self) 

最後,你可能需要使用twisted.protocols.basic.LineOnlyReceiver作爲基類FakeTelnet,並把你的邏輯在其lineReceived回調,如果你正在處理面向行的數據。 dataReceived回調不保證數據邊界,因此您可能永遠不會看到包含"START"的單個字符串。例如,您可能會收到兩個電話dataReceived,一個是​​,另一個是"ART"

相關問題