2011-07-14 79 views
1

我正在與下列服務扭曲運行Django應用程序:如何配置靜態的,扭曲的服務和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) 

如何添加服務媒體文件(「/媒體」)到同一個端口?

+0

很難理解不完整的代碼示例。什麼是「TwoStepResource」? Twisted Web本身不提供任何類。 –

+0

將TwoStepResource添加到問題中。 – alexarsh

回答

4

只需在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

+0

感謝您的回覆。這樣做後,我得到以下錯誤:「不能把IResource兒童下WSGIResource」 – alexarsh

+0

糟糕,我認爲我可以擺脫它,我從來沒有實際使用WSGIResource - 認爲它就像任何其他IResource。通過指向解決方案的鏈接更新我的答案。 –

+0

謝謝。但我在哪裏插入它在我的代碼?試圖將它添加到我的TwoStepResource,但得到: – alexarsh