Last active
December 11, 2015 22:39
-
-
Save allieus/4671532 to your computer and use it in GitHub Desktop.
Django 프로젝트를 Twisted 를 통해 실행
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from twisted.web import server, resource, wsgi, static | |
CURRENT_PATH = os.path.abspath(os.path.dirname(__file__)) | |
sys.path.insert(0, os.path.join(CURRENT_PATH, 'observer')) | |
class DjangoResource(resource.Resource): | |
def __init__(self, wsgi_resource): | |
resource.Resource.__init__(self) | |
self.wsgi_resource = wsgi_resource | |
def getChild(self, path, request): | |
path0 = request.prepath.pop(0) | |
request.postpath.insert(0, path0) | |
return self.wsgi_resource | |
@classmethod | |
def get_site(cls, reactor): | |
os.environ['DJANGO_SETTINGS_MODULE'] = 'observer.settings.prod_winapp' | |
from django.core.wsgi import get_wsgi_application | |
_wsgi_resource = wsgi.WSGIResource(reactor, reactor.getThreadPool(), get_wsgi_application()) | |
root = cls(_wsgi_resource) | |
media_src = static.File(os.path.join(CURRENT_PATH, 'media')) | |
root.putChild('media', media_src) | |
static_src = static.File(os.path.join(CURRENT_PATH, 'static')) | |
root.putChild('static', static_src) | |
return server.Site(root) | |
from twisted.internet import reactor | |
site = DjangoResource.get_site(reactor) | |
reactor.listenTCP(8080, site) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment