-
Notifications
You must be signed in to change notification settings - Fork 4
/
cherrypy_static_server.py
executable file
·38 lines (30 loc) · 1.17 KB
/
cherrypy_static_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
#!/usr/bin/env python
"""Static file server, using Python's CherryPy. Should be used when Django's static development server just doesn't cut."""
import cherrypy
import settings
from cherrypy.lib.static import serve_file
import os.path
from cherrypy.process.plugins import Daemonizer, PIDFile
class Root:
@cherrypy.expose
def index(self, name):
return serve_file(os.path.join(media_dir, name))
if __name__=='__main__':
media_dir = settings.MEDIA_ROOT
print "\nmedia_dir: %s\n" % media_dir
cherrypy.config.update( { # I prefer configuring the server here, instead of in an external file.
'server.socket_host': settings.MEDIA_SERVER['HOST'],
'server.socket_port': settings.MEDIA_SERVER['PORT'],
} )
conf = {
'/': { # Root folder.
'tools.staticdir.on': True, # Enable or disable this rule.
'tools.staticdir.root': media_dir,
'tools.staticdir.dir': '',
}
}
# daemonize
d = Daemonizer(cherrypy.engine)
d.subscribe()
PIDFile(cherrypy.engine, '/tmp/flink-cherrypy.pid').subscribe()
cherrypy.quickstart(Root(), '/', config=conf) # ..and LAUNCH ! :)