如何使用django在twisted中配置静态服务
我有一个django应用程序在twisted下运行,具有以下服务:如何使用django在twisted中配置静态服务,django,static,twisted,Django,Static,Twisted,我有一个django应用程序在twisted下运行,具有以下服务: class JungoHttpService(internet.TCPServer): def __init__(self, port): self.__port = port pool = threadpool.ThreadPool() wsgi_resource = TwoStepResource(reactor, pool, WSGIHandler())
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
以下是我的两步资源:
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”)添加到同一端口?只需在
wsgi\u资源分配后添加wsgi\u资源.putChild('media',文件(“/path/to/media”)
。当然,您需要从twisted.web.static导入文件
更新1:
结果是拒绝putChild()尝试。这里有一个解决方案:
更新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
只需在wsgi_资源分配后添加wsgi_资源.putChild('media',文件(“/path/to/media”)
。当然,您需要从twisted.web.static导入文件
更新1:
结果是拒绝putChild()尝试。这里有一个解决方案:
更新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
不完整的代码示例很难理解。什么是两步资源?Twisted Web本身并没有提供任何类。在问题中添加了TwoStepResource。很难理解不完整的代码示例。什么是两步资源?这不是Twisted Web本身提供的任何类。在问题中添加了TwoStepResource。感谢您的回答。在这样做之后,我得到了以下错误:“无法将IResource子项置于WSGIResource下”哦,我以为我可以侥幸逃脱,我从来没有实际使用过WSGIResource-假设它与任何其他IResource一样。更新了我的答案,并添加了解决方案的链接。谢谢。但是我应该在代码中的什么地方插入它呢?试图将其添加到我的TwoStepResource中,但得到:“无法将IResource子项置于WSGIResource下”您不需要更改TwoStepResource。您需要创建一个占位符根资源,将未更改的请求路径直接传递给WSGI资源—有效地将WSGI资源提升到根状态。我在回答中添加了一个有效的例子。为了简单起见,我使用了一个简单的WSGIResource,而不是您的TwoStepResource。谢谢您的回复。在这样做之后,我得到了以下错误:“无法将IResource子项置于WSGIResource下”哦,我以为我可以侥幸逃脱,我从来没有实际使用过WSGIResource-假设它与任何其他IResource一样。更新了我的答案,并添加了解决方案的链接。谢谢。但是我应该在代码中的什么地方插入它呢?试图将其添加到我的TwoStepResource中,但得到:“无法将IResource子项置于WSGIResource下”您不需要更改TwoStepResource。您需要创建一个占位符根资源,将未更改的请求路径直接传递给WSGI资源—有效地将WSGI资源提升到根状态。我在回答中添加了一个有效的例子。为了简单起见,我使用了一个简单的WSGIResource,而不是TwoStepResource。