【问题标题】:How to configure static serving in twisted with django如何在 django 中配置静态服务
【发布时间】:2011-07-14 08:47:11
【问题描述】:

我有一个 django 应用程序在扭曲下运行,并提供以下服务:

class JungoHttpService(internet.TCPServer):

    def __init__(self, port):
        self.__port = port
        pool = threadpool.ThreadPool()
        wsgi_resource = TwoStepResource(reactor, pool, WSGIHandler())
        internet.TCPServer.__init__(self, port, Site(wsgi_resource))
        self.setName("WSGI/HttpJungo")
        self.pool = pool

    def startService(self):
        internet.TCPServer.startService(self)
        self.pool.start()

    def stopService(self):
        self.pool.stop()
        return internet.TCPServer.stopService(self)

    def getServerPort(self):
        """ returns the port number the server is listening on"""
        return self.__port

这是我的 TwoStepResource:

class TwoStepResource(WSGIResource):

    def render (self, request):
        if request.postpath:
            pathInfo = '/' + '/'.join(request.postpath)
        else:
            pathInfo = ''
        try:
            callback, callback_args, 
            callback_kwargs = urlresolvers.resolve(pathInfo)

            if hasattr(callback, "async"):
                # Patch the request
                _patch_request(request, callback, callback_args, 
                               callback_kwargs)
        except Exception, e:
            logging.getLogger('jungo.request').error("%s : %s\n%s" % (
                      e.__class__.__name__, e, traceback.format_exc()))

            raise
        finally:
            return super(TwoStepResource, self).render(request)

如何将服务媒体文件(“/media”)添加到同一个端口?

【问题讨论】:

  • 不完整的代码示例很难理解。什么是TwoStepResource?它不是 Twisted Web 本身提供的任何类。
  • 在问题中添加了 TwoStepResource。

标签: django static twisted


【解决方案1】:

只需在wsgi_resource 赋值后添加wsgi_resource.putChild('media', File("/path/to/media"))。你当然需要from twisted.web.static import File

更新 1:

原来WSGIResource 拒绝 putChild() 尝试。这里有一个解决方案:http://blog.vrplumber.com/index.php?/archives/2426-Making-your-Twisted-resources-a-url-sub-tree-of-your-WSGI-resource....html

更新 2:

jungo.py

from twisted.application import internet
from twisted.web import resource, wsgi, static, server
from twisted.python import threadpool
from twisted.internet import reactor

def wsgiApplication(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/plain')])
    return ['Hello, world!']

class SharedRoot(resource.Resource):
    """Root resource that combines the two sites/entry points"""
    WSGI = None

    def getChild(self, child, request):
        request.prepath.pop()
        request.postpath.insert(0, child)
        return self.WSGI

    def render(self, request):
        return self.WSGI.render(request)

class JungoHttpService(internet.TCPServer):

    def __init__(self, port):
        self.__port = port
        pool = threadpool.ThreadPool()
        sharedRoot = SharedRoot()

                          # substitute with your custom WSGIResource
        sharedRoot.WSGI = wsgi.WSGIResource(reactor, pool, wsgiApplication)
        sharedRoot.putChild('media', static.File("/path/to/media"))
        internet.TCPServer.__init__(self, port, server.Site(sharedRoot))
        self.setName("WSGI/HttpJungo")
        self.pool = pool

    def startService(self):
        internet.TCPServer.startService(self)
        self.pool.start()

    def stopService(self):
        self.pool.stop()
        return internet.TCPServer.stopService(self)

    def getServerPort(self):
        """ returns the port number the server is listening on"""
        return self.__port

jungo.tac

from twisted.application import internet, service
from jungo import JungoHttpService

application = service.Application("jungo")
jungoService = JungoHttpService(8080)
jungoService.setServiceParent(application)

$ twistd -n -y jungo.tac

【讨论】:

  • 感谢您的回复。这样做之后,我收到以下错误:“无法将 IResource 子项放在 WSGIResource 下”
  • 糟糕,我以为我可以侥幸逃脱,但我从未真正使用过 WSGIResource - 假设它就像任何其他 IResource 一样。用解决方案的链接更新了我的答案。
  • 谢谢。但是我在哪里将它插入到我的代码中?尝试将其添加到我的 TwoStepResource,但得到:
  • "不能将 IResource 子级放在 WSGIResource 下"
  • 您不需要更改 TwoStepResource。您需要创建一个占位符根资源,将未更改的请求路径直接传递给 WSGI 资源 - 有效地将 WSGI 资源提升到根状态。我在我的答案中添加了一个工作示例。为简洁起见,我使用的是简单的 WSGIResource 而不是您的 TwoStepResource。
猜你喜欢
  • 2015-06-06
  • 2012-09-02
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
相关资源
最近更新 更多