日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

twsited快速基础

發(fā)布時(shí)間:2025/3/12 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 twsited快速基础 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

協(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è)例子:

from twisted.internet.protocol import Protocol class Echo(Protocol):def __init__(self, factory):self.factory = factory def connectionMade(self):self.factory.numProtocols = self.factory.numProtocols + 1self.transport.write("歡迎,這是第%d個(gè)運(yùn)行中的連接\n" % (self.factory.numProtocols,)) def connectionLost(self, reason):self.factory.numProtocols = self.factory.numProtocols - 1 def dataReceived(self, data): self.transport.write(data)

工廠用于共享存在于超過任何給定連接的生命周期的狀態(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()endpoint = TCP4ServerEndpoint(reactor, 21200) endpoint.listen(QOTDFactory()) reactor.run()

============================

#!/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.
]==>連接丟失!

#!/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 import timeclass QOTD(Protocol):#協(xié)議類def connectionMade(self):print "接受連接"self.transport.write("您好!歡迎您的登錄,再見!\r\n") def dataReceived(self, data):if data:print "客戶端發(fā)來消息:%s"%data self.transport.write("[%s]:%s"%(time.ctime(),data))self.transport.loseConnection() class QOTDFactory(Factory):def buildProtocol(self, addr):return QOTD()if __name__=="__main__":endpoint = TCP4ServerEndpoint(reactor, 21210)endpoint.listen(QOTDFactory())reactor.run()

接受連接
客戶端發(fā)來消息:hello,1

#!/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("請輸入消息:")self.transport.write(data)def dataReceived(self, data):print "服務(wù)器回應(yīng):%s"%data 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)連接.
請輸入消息: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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。