How does one incorporate a Django application into an existing twisted server?

孤街醉人 提交于 2019-12-08 04:50:21

问题


I'm looking to add/serve a complicated django application, to an existing twisted server (the existing twisted server doesn't serve any http services, at least not on the standard ports, so I can use port 80 for this work).

All of the examples I can find to date are for earlier versions of twisted, and do not seem to work, out of the box, with the latest version.

Where can I find an up to date tutorial, set of examples or recipe, showing the correct wiring for serving a complex Django application through the latest version of twisted?


More specifically, I have gotten close with this little blurb:

from twisted.web.resource import Resource
from twisted.web import wsgi
from twisted.internet import reactor
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings'
django_application = WSGIHandler()
django_resource = wsgi.WSGIResource(reactor, reactor.getThreadPool(), django_application)

root = Resource()
root.putChild("",django_resource)
from twisted.application import service
application = service.Application("app")

internet.TCPServer(8080, Site(root)).setServiceParent(application)

(it will serve the basic contents of my django project, but seems to fail to properly serve my static content, and doesn't seem to automatically handle django applications served at subdirectories within my project)


回答1:


Found my bug:

# All this work is un-necessary overkill
#root = Resource()
#root.putChild("",django_resource)
#from twisted.application import service
#application = service.Application("app")

internet.TCPServer(8080, Site(django_resource)).setServiceParent(application)


来源:https://stackoverflow.com/questions/19720766/how-does-one-incorporate-a-django-application-into-an-existing-twisted-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!