twisted08 分发动态处理的http服务器

来源:互联网 发布:免费b2b群发软件 编辑:程序博客网 时间:2024/06/10 06:35
from twisted.internet import reactorfrom twisted.web.server import Sitefrom twisted.web.resource import Resource, NoResourcefrom twisted.web.static import Filefrom calendar import calendarclass Calendar(Resource):def __init__(self, year):assert(isinstance(year, (int, long)))self.year = yeardef render_GET(self, request):return '<html><h1><pre>%s</pre></h1></html>' % calendar(self.year)class URL_Dispatcher(Resource):def getChild(self, name, request):if name == '/' or not name:return self#return ico resouceelif name == 'favicon.ico':return File('d:/down.png')#Only process http://localhost:8000/1995#Can't process http://localhost:8000/1995/1995 or http://localhost:8000/1995/1995/../../..#if return a resource object (derived from Resource)#will continue accesss by level (sperator by '/')#until no '/'elif name.isdigit() and int(name) > 1990:print 'name->:',namey = int(name)return Calendar(y)else:return NoResource()def render_GET(self, request):return '<html><h1>Calendar</h1></html>'if __name__ == '__main__':s = Site(URL_Dispatcher())reactor.listenTCP(8000,s)reactor.run()

0 0
原创粉丝点击