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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Twisted中的putChild和getChild

發(fā)布時(shí)間:2025/3/15 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Twisted中的putChild和getChild 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? ? ? ? Twisted的官方文檔上對(duì)于putChild的解釋是“Register a static child”(點(diǎn)擊打開鏈接),即為當(dāng)前資源節(jié)點(diǎn)注冊(cè)一個(gè)靜態(tài)子資源節(jié)點(diǎn)。實(shí)際上,Resource類中的putChild實(shí)現(xiàn)的是IResource接口中的putChild方法(點(diǎn)擊打開鏈接)。

? ? ? ? Resource類中還有一個(gè)getChild方法,官方文檔的解釋是“Retrieve a 'child' resource from me. Implement this to create dynamic resource generation -- resources which are always available may be registered with self.putChild()”(點(diǎn)擊打開鏈接)。這兩句話說明getChild的作用是從當(dāng)前資源節(jié)點(diǎn)獲取子資源節(jié)點(diǎn)。但是,與putChild不同,getChild方法能夠創(chuàng)建動(dòng)態(tài)資源。

? ? ? ? 理解putChild和getChild的關(guān)鍵在于理解“靜態(tài)資源”和“動(dòng)態(tài)資源”兩個(gè)概念。下面先看一個(gè)書上(Twisted Network Programming Essentials)的例子:

from twisted.internet import reactor from twisted.web.resource import Resource, NoResource from twisted.web.server import Sitefrom calendar import calendarclass YearPage(Resource):def __init__(self, year):Resource.__init__(self)self.year = yeardef render_GET(self, request):return "<html><body><pre>%s</pre></body></html>" % (calendar(self.year), )class CalendarHome(Resource):def getChild(self, name, request):if name == "":return selfif name.isdigit():return YearPage(int(name))else:return NoResource()def render_GET(self, request):return "<html><body>Welcome to the calendar server!</body></html>"root = CalendarHome() factory = Site(root) reactor.listenTCP(8000, factory) reactor.run()

? ? ? ? 假設(shè)上面代碼在本地運(yùn)行,如果我們?cè)L問localhost:8000,CalendarHome的實(shí)例root的getChild會(huì)被調(diào)用,此時(shí)傳給getChild的name實(shí)參為“”,getChild返回實(shí)例自身(self);同時(shí),該實(shí)例調(diào)用render_GET函數(shù)渲染自己。如果訪問localhost:8000/2016,此時(shí)傳給getChild的name實(shí)參為"2016",這時(shí),getChild負(fù)責(zé)新建一個(gè)YearPage實(shí)例,該實(shí)例調(diào)用render_GET函數(shù)渲染自己。如果在瀏覽器中打開localhost:8000/2016頁面,每次刷新都將調(diào)用root的getChild函數(shù),而每次調(diào)用都會(huì)創(chuàng)建一個(gè)新的YearPage實(shí)例。因此,那些像YearPage一樣、訪問時(shí)才被創(chuàng)建的資源,就是“動(dòng)態(tài)資源”。

現(xiàn)在把上面的代碼稍加改寫,如下所示:

from twisted.internet import reactor from twisted.web.resource import Resource, NoResource from twisted.web.server import Sitefrom calendar import calendarclass HelpPage(Resource):isLeaf = Truedef render_GET(self, request):return "<html><body>help</body></html>"class YearPage(Resource):def __init__(self, year):Resource.__init__(self)self.year = yearself.putChild("help", HelpPage())def render_GET(self, request):return "<html><body><pre>%s</pre></body></html>" % (calendar(self.year), )class RoomPage(Resource):isLeaf = Truedef render_GET(self, request):return "<html><body>room</body></html>"class HomePage(Resource):def __init__(self):Resource.__init__(self)self.putChild("room", RoomPage())def render_GET(self, request):return "<html><body>home</body></html>"class CalendarHome(Resource):def __init__(self):Resource.__init__(self)self.putChild("home", HomePage())self.putChild("year", YearPage(2016))def getChild(self, name, request):if name == "":return selfreturn NoResource()def render_GET(self, request):return "<html><body>Welcome to the calendar server!</body></html>"root = CalendarHome() factory = Site(root) reactor.listenTCP(8000, factory) reactor.run()

? ? ? ? 與上一段代碼不同,這段代碼在CalendarHome實(shí)例構(gòu)造階段就用putChild函數(shù)為該實(shí)例添加了兩個(gè)子資源節(jié)點(diǎn):home對(duì)應(yīng)一個(gè)HomePage實(shí)例,“year”對(duì)應(yīng)一個(gè)YearPage實(shí)例。如果用瀏覽器打開localhost:8000/year,無論反復(fù)刷新多少次,得到的都是來自同一個(gè)YearPage實(shí)例的響應(yīng)。同理,用瀏覽器分別打開localhost:8000/year/help,localhost:8000/home和localhost:8000/home/room,反復(fù)刷新,相應(yīng)的實(shí)例不會(huì)反復(fù)重新生成,響應(yīng)瀏覽器GET請(qǐng)求的都是相應(yīng)的父資源節(jié)點(diǎn)調(diào)用putChild函數(shù)時(shí)注冊(cè)生成的那一個(gè)。因此,這些實(shí)例一旦被創(chuàng)建就會(huì)駐留在內(nèi)存中,被反復(fù)使用。這就是“靜態(tài)資源”的含義。

再對(duì)上面的代碼中CalendarHome的構(gòu)造函數(shù)稍作修改:

class CalendarHome(Resource):def __init__(self):Resource.__init__(self)self.putChild("", HomePage())self.putChild("year", YearPage(2016))def getChild(self, name, request):if name == "":return selfreturn NoResource()def render_GET(self, request):return "<html><body>Welcome to the calendar server!</body></html>"

? ? ? ? 如上面代碼所示,將HomePage實(shí)例注冊(cè)在CalendarHome“根目錄”下。這時(shí)就會(huì)出現(xiàn)一個(gè)問題:getChild函數(shù)要求當(dāng)name為“”,即訪問localhost:8000時(shí),返回CalendarHome實(shí)例自身,然后調(diào)用自身的render_GET函數(shù)渲染;而putChild函數(shù)似乎要求訪問localhost:8000時(shí)返回一個(gè)HomePage實(shí)例。到底聽誰的?實(shí)驗(yàn)證明,聽putChild的,即訪問localhost:8000時(shí),將調(diào)用HomePage實(shí)例的render_GET函數(shù)進(jìn)行渲染。事實(shí)上,只有當(dāng)某個(gè)資源節(jié)點(diǎn)沒有被putChild函數(shù)注冊(cè)生成時(shí),getChild函數(shù)才會(huì)被調(diào)用,用于查找并返回相應(yīng)的資源。

? ? ? ? 需要注意的是,此時(shí)訪問localhost:8000/room會(huì)出現(xiàn)404錯(cuò)誤。實(shí)際上,這是CalendarHome的getChild函數(shù)在作怪。因?yàn)間etChild函數(shù)中寫得清清楚楚,當(dāng)name不為“”時(shí),一律返回NoResource。如果把getChild代碼刪掉,再訪問localhost:8000/room呢?答案依然是404。這是因?yàn)镃alendarHome的getChild方法繼承自Resource類,而Resource類中的getChild源碼如下:

161 def getChild(self, path, request): 162 """ 163 Retrieve a 'child' resource from me. 164 165 Implement this to create dynamic resource generation -- resources which 166 are always available may be registered with self.putChild(). 167 168 This will not be called if the class-level variable 'isLeaf' is set in 169 your subclass; instead, the 'postpath' attribute of the request will be 170 left as a list of the remaining path elements. 171 172 For example, the URL /foo/bar/baz will normally be:: 173 174 | site.resource.getChild('foo').getChild('bar').getChild('baz'). 175 176 However, if the resource returned by 'bar' has isLeaf set to true, then 177 the getChild call will never be made on it. 178 179 Parameters and return value have the same meaning and requirements as 180 those defined by L{IResource.getChildWithDefault}. 181 """ 182 return NoResource("No such child resource.")

? ? ? ? 可見,如果CalendarHome不重寫父類中的getChild方法,一旦CalendarHome的getChild被調(diào)用,直接返回NoResource。

總結(jié)

以上是生活随笔為你收集整理的Twisted中的putChild和getChild的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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