2010-10-12 72 views
2

我已經決定動用我的腳趾與扭曲的幫助異步蟒蛇的世界。我已經實現了文檔中的一些示例,但我很難找到一個非常簡單的客戶端示例,我試圖編寫它。扭曲的客戶端發送一個唯一的協議,是寬容斷開

總之我想的客戶機,其建立與服務器的TCP連接,然後發送簡單的「\ n」個終止的字符串的消息關閉一個隊列對象到服務器的。服務器不會迴應任何消息,因此我的客戶端完全是單向的。我/想/我想要的是this example和twisted.internet.protocols.basic.LineReceiver便捷協議的組合。這感覺應該是人們可以做的最簡單的事情,但沒有任何我在網上看到的文檔或例子似乎很合適。

回答

1

我所做的不使用隊列,但是我將舉例說明發送一條線,一旦建立連接的代碼。有大量的打印內容可以幫助你理解正在發生的事情。

常住進口的東西:

from twisted.web import proxy 
from twisted.internet import reactor 
from twisted.internet import protocol 
from twisted.internet.protocol import ReconnectingClientFactory 
from twisted.protocols import basic 
from twisted.python import log 
import sys 
log.startLogging(sys.stdout) 

您創建線路接收器獲得的協議,設置分隔符。 在這種情況下,一旦建立連接,我只需寫一個字符串「www」。 關鍵的一點是看協議接口在twisted.internet.interface.py並理解協議的各種方法和他們做的,當他們叫什麼。

class MyProtocol(basic.LineReceiver): 

    #def makeConnection(self, transport): 
    # print transport  

    def connectionLost(self, reason): 
     print reason 
     self.sendData = False 

    def connectionMade(self): 
     print "connection made" 
     self.delimiter = "\n" 
     self.sendData = True 
     print self.transport 
     self.sendFromQueue() 

    def sendFromQueue(self): 
     while self.sendData: 
      msg = dataQueue.get() 
      self.sendLine(msg) 
      # you need to handle empty queue 
      # Have another function to resume 

最後,協議工廠將爲每個連接創建一個協議實例。 查看方法:buildProtcol。

class myProtocolFactory(): 
    protocol = MyProtocol 

    def doStart(self): 
     pass 

    def startedConnecting(self, connectorInstance): 
     print connectorInstance 

    def buildProtocol(self, address): 
     print address 
     return self.protocol() 

    def clientConnectionLost(self, connection, reason): 
     print reason 
     print connection 

    def clientConnectionFailed(self, connection, reason): 
     print connection 
     print reason 

    def doStop(self): 
     pass 

現在,您使用的連接器進行連接:

reactor.connectTCP('localhost', 50000, myProtocolFactory()) 
reactor.run() 

我跑了這一點,它連接到簡單的打印它接收和發送,因此沒有ACK回一個服務器。這裏是輸出:

1286906080.08 82  INFO 140735087148064 __main__ conn_made: client_address=127.0.0.1:50277 
1286906080.08 83 DEBUG 140735087148064 __main__ created handler; waiting for loop 
1286906080.08 83 DEBUG 140735087148064 __main__ handle_read 
1286906080.08 83 DEBUG 140735087148064 __main__ after recv 
'www\n' 

Recieved: 4 

上面的例子如果不是容錯的話。要重新連接,當連接丟失時,可以從現有的扭曲類派生協議工廠 - 重新連接客戶端工廠。 扭曲幾乎所有你需要:)

class myProtocolFactory(ReconnectingClientFactory): 
    protocol = MyProtocol 

    def buildProtocol(self, address): 
     print address 
     return self.protocol() 

更多參考

我建議你閱讀工具:http://krondo.com/?page_id=1327

[編輯:按照下面的評論]

+2

'basic.LineReceiver.delimiter = 「\ n」'?真?真可怕。你有什麼對'self.delimiter =「\ n」'?它更短*和*更少瘋狂。 – 2010-10-12 18:42:29

+1

@ Jean-Paul Calderone:+1我也在學習。謝謝。我在瞬間寫這一點,因此錯誤 – pyfunc 2010-10-12 18:45:27

+0

這是關於我在我的代碼在多大程度上做到了,但我不明白的是在connectionMade方法你有一個函數調用的MyProtocol類。我想有一個異步循環從隊列中提取這些值。如果我在連接建立的時候正確理解了你的代碼,並且在我完成發送所有排隊的消息之前立即斷開連接,那麼循環將會阻塞或者它會繼續,但是會由於連接而丟棄消息走了。我讀的是對的嗎? – 2010-10-12 19:04:06