我正在尝试使用gzip编码来提供部分扭曲的网站:
from twisted.internet import reactor
from twisted.web.server import Site, GzipEncoderFactory
from twisted.web.resource import Resource, EncodingResourceWrapper
from twisted.web import server, resource, static
class SharedRoot(resource.Resource):
"""Root resource that combines the two sites/entry points"""
def getChild(self, child, request):
request.prepath.pop()
request.postpath.insert(0, child)
return self
def render(self, request):
return ""
def render_POST(self, request):
return ''
class GzipFileResource(static.File):
def getChild(self, path, request):
child = super(GzipFileResource, self).getChild(path, request)
return EncodingResourceWrapper(child, [GzipEncoderFactory()])
def main():
shared=SharedRoot()
shared.putChild("compressed",GzipFileResource("static"))
shared.putChild("plain", static.File("static"))
thisSite=server.Site(shared)
reactor.listenTCP(8000, thisSite)
reactor.run()
if __name__=="__main__":
main()
服务器短文件在普通模式和压缩模式下工作。提供更长的文件(> 32 KB)仅适用于普通模式。浏览器挂起。