from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource, NoResource
from twisted.web.static import File
from calendar import calendar
class Calendar(Resource):
def __init__(self, year):
assert(isinstance(year, (int, long)))
self.year = year
def 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 resouce
elif 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->:',name
y = 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()
twisted08 分发动态处理的http服务器
最新推荐文章于 2022-03-06 17:34:19 发布