Browse files

now serving django as wsgi service, via twisted

  • Loading branch information...
aausch committed Nov 6, 2013
1 parent 2ecf247 commit 0cdbd7125b3340519af1e29b2c71d673d9b14b95
Showing with 62 additions and 5 deletions.
  1. +27 −4
  2. +20 −1 twisted_chat/
  3. +15 −0 twisted_chat/
@@ -1,11 +1,20 @@
from twisted.web.websockets import WebSocketsResource, WebSocketsProtocol, lookupProtocolForFactory
import os
from twisted.application import service, internet
from twisted.internet import reactor
from twisted.web import wsgi
from twisted.web.resource import Resource
from twisted.web.server import Site
from twisted.internet import protocol
from twisted.application import service, internet
from twisted.web.websockets import WebSocketsResource, lookupProtocolForFactory
from twisted_chat.factories import ChatFactory
from twisted_chat.resources import HttpChat
from twisted_chat.resources import HttpChat, StaticFileScanner
from twisted_chat.wsgi import WsgiRoot
from twisted.python.threadpool import ThreadPool
from django.core.handlers.wsgi import WSGIHandler
shared_messages = {}
@@ -20,3 +29,17 @@
application = service.Application("chatserver")
internet.TCPServer(1025, Site(root)).setServiceParent(application)
#serving django over wsgi
# Create and start a thread pool,
wsgiThreadPool = ThreadPool()
django_application = WSGIHandler()
django_resource = wsgi.WSGIResource(reactor, wsgiThreadPool, django_application)
django_root = WsgiRoot(django_resource)
project_dir = os.getcwd()
django_root.putChild('static', StaticFileScanner(project_dir + "/chat/static", project_dir + "/django_twisted_chat/static"))
internet.TCPServer(8080, Site(django_root)).setServiceParent(application)
@@ -1,8 +1,10 @@
import json
import time
from twisted.web.resource import Resource
from twisted.internet import task
from twisted.web.resource import Resource
from twisted.web.static import File
from twisted.web.server import NOT_DONE_YET
from twisted_chat.factories import ChatFactory
@@ -98,3 +100,20 @@ def __format_response(self, request, status, data, timestamp=float(time.time()))
return request.jsonpcallback+'('+response+')'
return response
class StaticFileScanner(Resource):
dirs = []
def __init__(self, *dirs):
if (len(dirs) < 1):
self.dirs = [File()]
self.dirs = [File(d) for d in dirs]
def getChild(self, *args):
for d in self.dirs:
if d.getChild(*args) != d.childNotFound:
return d.getChild(*args)
return self.dirs[0].childNotFound
@@ -0,0 +1,15 @@
import os
from twisted.web.resource import Resource
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_twisted_chat.settings'
class WsgiRoot(Resource):
def __init__(self, wsgi_resource):
self.wsgi_resource = wsgi_resource
def getChild(self, path, request):
path0 = request.prepath.pop(0)
request.postpath.insert(0, path0)
return self.wsgi_resource

0 comments on commit 0cdbd71

Please sign in to comment.