twsited快速基础
協(xié)議處理類通常會(huì)子類化twisted.com .internet.protocol. protocol。大多數(shù)協(xié)議處理程序從這個(gè)類繼承,或者從它的一個(gè)方便子類繼承。協(xié)議類的實(shí)例按連接實(shí)例化,按需實(shí)例化,并在連接完成時(shí)消失。這意味著持久化配置不會(huì)保存在協(xié)議中。
持久化配置保存在工廠類中,工廠類通常繼承自twisted.com .internet.protocol.Factory。工廠的buildProtocol方法用于為每個(gè)新連接創(chuàng)建Protocol。
能夠在多個(gè)端口或網(wǎng)絡(luò)地址上提供相同的服務(wù)通常很有用。這就是為什么工廠不監(jiān)聽連接,實(shí)際上不知道任何關(guān)于網(wǎng)絡(luò)的信息。
Twisted協(xié)議以異步方式處理數(shù)據(jù)。當(dāng)事件從網(wǎng)絡(luò)到達(dá)時(shí),協(xié)議protocol響應(yīng)它們,而事件到達(dá)將調(diào)用協(xié)議上的方法。
這是最簡單的協(xié)議之一。它只是簡單地回寫所寫的內(nèi)容,而不響應(yīng)所有事件。
#!/usr/bin/env python2 # -*- coding: utf-8 -*-from twisted.internet.protocol import Protocol class Echo(Protocol):def dataReceived(self, data): self.transport.write(data)下面是一個(gè)協(xié)議響應(yīng)另一個(gè)事件的例子:
from twisted.internet.protocol import Protocol class QOTD(Protocol):def connectionMade(self):self.transport.write("一天一蘋果,不用請醫(yī)生\r\n") self.transport.loseConnection()這個(gè)協(xié)議用一個(gè)名言響應(yīng)初始連接,然后終止連接。
connectionMade事件通常相應(yīng)連接對(duì)象啟動(dòng)發(fā)生,以及任何初始的問候語(如上面的QOTD協(xié)議)。connectionLost事件應(yīng)用于任何特定于連接的對(duì)象消失。下面是一個(gè)例子:
工廠用于共享存在于超過任何給定連接的生命周期的狀態(tài)。
在Echo協(xié)議代碼中,loseConnection是在寫入傳輸之后立即調(diào)用的。只有當(dāng)Twisted將所有數(shù)據(jù)寫完入操作系統(tǒng)時(shí),loseConnection調(diào)用才會(huì)關(guān)閉連接,因此在這種情況下使用它是安全的,無需擔(dān)心傳輸寫入丟失。如果生產(chǎn)者與傳輸一起使用,loseConnection只會(huì)在生產(chǎn)者解除注冊時(shí)關(guān)閉連接。在某些情況下,等待所有數(shù)據(jù)被寫出來并不是我們想要的。由于網(wǎng)絡(luò)故障、bug或另一端連接者的惡意,寫入到傳輸端的數(shù)據(jù)可能無法交付,因此即使調(diào)用loseConnection,連接也不會(huì)丟失。在這些情況下,可以使用abortConnection:它立即關(guān)閉連接,而不管傳輸中仍然未寫入的緩沖數(shù)據(jù)或仍然有注冊的生產(chǎn)者。
在本例中,我創(chuàng)建了一個(gè)協(xié)議工廠。我想告訴這個(gè)工廠它的任務(wù)是構(gòu)建QOTD協(xié)議實(shí)例,所以我設(shè)置了它的buildProtocol方法來返回QOTD類的實(shí)例。然后,我想在一個(gè)TCP端口上偵聽,因此我創(chuàng)建了一個(gè)TCP4ServerEndpoint來標(biāo)識(shí)我想綁定到的端口,然后將我剛剛創(chuàng)建的工廠傳遞到它的偵聽方法。
listen()告訴反應(yīng)器使用特定的協(xié)議來處理到端點(diǎn)地址的連接,但是反應(yīng)器需要運(yùn)行才能執(zhí)行任何操作。run()啟動(dòng)反應(yīng)器,然后永遠(yuǎn)等待連接到達(dá)指定的端口。您可以通過在終端中按Control-C或調(diào)用reactor.stop()來停止反應(yīng)器。
============================
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sun Sep 16 15:27:20 2018@author: myhaspl @email:myhaspl@myhaspl.com server """ from twisted.internet.protocol import Factory from twisted.internet.endpoints import TCP4ServerEndpoint from twisted.internet import reactor from twisted.internet.protocol import Protocol class QOTD(Protocol):#協(xié)議類def connectionMade(self):self.transport.write("您好!歡迎您的登錄,再見!\r\n") self.transport.loseConnection()class QOTDFactory(Factory):def buildProtocol(self, addr):return QOTD()if __name__=="__main__":endpoint = TCP4ServerEndpoint(reactor, 21210)endpoint.listen(QOTDFactory())reactor.run()=============================
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sun Sep 16 15:28:44 2018@author: myhaspl @email:myhaspl@myhaspl.com client """from twisted.internet.protocol import Protocol from twisted.internet.protocol import ClientFactory from twisted.internet import reactorHOST="localhost" PORT=21210 class ClientMessage(Protocol):#協(xié)議類def connectionMade(self):data=raw_input("請輸入要發(fā)送給服務(wù)器的消息")self.transport.write(data)def dataReceived(self, data):print data self.transport.loseConnection()class ClientMessageFactory(ClientFactory):#工廠類def startedConnecting(self, connector):print '開始連接'def buildProtocol(self, addr):print '已經(jīng)連接.'return ClientMessage()def clientConnectionLost(self, connector, reason):print '%s==>連接丟失!'%reasondef clientConnectionFailed(self, connector, reason):print '%s==>連接錯(cuò)誤!'%reasonif __name__=="__main__":reactor.connectTCP(HOST, PORT, ClientMessageFactory())reactor.run()客戶端
開始連接
已經(jīng)連接.
請輸入要發(fā)送給服務(wù)器的消息hi
您好!歡迎您的登錄,再見!
[Failure instance: Traceback (failure with no frames): <class ‘twisted.internet.error.ConnectionDone’>: Connection was closed cleanly.
]==>連接丟失!
接受連接
客戶端發(fā)來消息:hello,1
開始連接
已經(jīng)連接.
請輸入消息:hello,1
服務(wù)器回應(yīng):您好!歡迎您的登錄,再見!
服務(wù)器回應(yīng):[Sun Sep 16 16:37:08 2018]:hello,1
[Failure instance: Traceback (failure with no frames): <class ‘twisted.internet.error.ConnectionDone’>: Connection was closed cleanly.
]==>連接丟失!
總結(jié)
以上是生活随笔為你收集整理的twsited快速基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Intellij Idea的使用之svn
- 下一篇: ajax 405报错,使用ajax请求时