-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.py
42 lines (34 loc) · 1.42 KB
/
http_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from twisted.web import static, resource, server, wsgi, static
from twisted.python import threadpool
from twisted.application import internet, service
from twisted.internet import reactor
import os, sys
execfile('internal_config.py')
# set up django environment variables
sys.path.append('/home/james/ict617/candice')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.core.handlers.wsgi import WSGIHandler
class RootResource(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
def wsgi_resource():
pool = threadpool.ThreadPool()
pool.start()
reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
return wsgi.WSGIResource(reactor, pool, WSGIHandler())
def get_root_resource():
wsgi_root = wsgi_resource()
return RootResource(wsgi_root)
#Twisted Application setup:
application = service.Application('candice')
serviceCollection = service.IServiceCollection(application)
# Django and static file server:
root_resource = get_root_resource()
root_resource.putChild("static", static.File("static"))
http_factory = server.Site(root_resource)
internet.TCPServer(web_server_port, http_factory).setServiceParent(serviceCollection)