標籤:twisted
靜態檔案的web伺服器
from twisted.internet import reactorfrom twisted.web.server import Sitefrom twisted.web.static import Fileresource = File(‘C:\\‘)factory = Site(resource)reactor.listenTCP(8000, factory)reactor.run()
from twisted.internet import reactorfrom twisted.web.server import Sitefrom twisted.web.static import Fileroot = File("D:\\downloaed_images")root.putChild("logs", File("D:\\work_journal"))factory = Site(root)reactor.listenTCP(8000, factory)reactor.run()
呈現動態內容
from twisted.internet import reactorfrom twisted.web.resource import Resourcefrom twisted.web.server import Siteimport timeclass ClockPage(Resource): isLeaf = True def render_GET(self, request): return "The local time is %s" % (time.ctime(),)resource = ClockPage()factory = Site(resource)reactor.listenTCP(8000, factory)reactor.run()
日曆伺服器
from twisted.internet import reactorfrom twisted.web.resource import Resource,NoResourcefrom twisted.web.server import Sitefrom calendar import calendarclass YearPage(Resource): def __init__(self, year): Resource.__init__(self) self.year = year def render_GET(self, request): return "<html><body><pre>%s</pre></body></html>" % (calendar(self.year),)class CalendarHome(Resource): def getChild(self, path, request): if path == ‘‘: return self if path.isdigit(): return YearPage(int(path)) else: return NoResource()root = CalendarHome()factory = Site(root)reactor.listenTCP(8000, factory)reactor.run()
twisted 3 webserver