twisted 3 webserver
静态文件的web服务器
from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File resource = File(‘C:\\‘) factory = Site(resource) reactor.listenTCP(8000, factory) reactor.run()
from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File root = File("D:\\downloaed_images") root.putChild("logs", File("D:\\work_journal")) factory = Site(root) reactor.listenTCP(8000, factory) reactor.run()
呈现动态内容
from twisted.internet import reactor from twisted.web.resource import Resource from twisted.web.server import Site import time class 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 reactor from twisted.web.resource import Resource,NoResource from twisted.web.server import Site from calendar import calendar class 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()
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。