我在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”)添加到同一端口?
答案 0 :(得分:4)
只需在wsgi_resource.putChild('media', File("/path/to/media"))
分配后添加wsgi_resource
即可。当然,你需要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