Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/home2/bilumspn/bin/python2.5
- #################################### IMPORTS ###################################
- #Std Libs
- import cgi
- import os
- import sys
- import re
- import functools
- from pprint import pformat
- # Fast CGI
- from flup.server.fcgi import WSGIServer
- # 3rd Parties
- import cherrypy
- ################################# RELATIVE URLS ################################
- url = functools.partial(cherrypy.url, relative='server')
- ################################# WSGI WRAPPER #################################
- class Wrapper(object):
- def __init__(self, app):
- self.app = app
- def __call__(self, environ, start_response):
- environ['SCRIPT_NAME'] = ''
- return self.app(environ, start_response)
- ################################### DEBUGGING ##################################
- def debug_request(multiplier=1):
- f = open('debugging', 'w')
- f.write(pformat(cherrypy.request.__dict__))
- f.close()
- cherrypy.tools.debug_request = cherrypy.Tool('on_start_resource', debug_request)
- ############################# STRING INTERPOLATION #############################
- def _(string):
- frame = sys._getframe(1)
- locals = frame.f_locals
- globals = frame.f_globals
- for item in re.findall(r'\$\{([^{]*)\}', string):
- string = string.replace('${%s}' % item,
- str(eval(item, globals, locals)))
- return string
- ################################## APPLICATION #################################
- class Admin:
- @cherrypy.expose
- def index(self):
- return _("""
- <a href=${url('login')}> Login? </a> """)
- @cherrypy.expose
- def login(self, username=None, password=None, **kw):
- if cherrypy.request.method == 'POST':
- return _('Hello ${username}')
- else:
- return _("""
- <form action='${url('login')}' method='POST'>
- User: <input type='text' name='username'/>
- Password: <input type='password' name='password'/>
- <input type='submit' name='submit' value='login'/>
- </form> """)
- ################################ FCGI BOOTSTRAP ################################
- cherrypy.server.unsubscribe()
- cherrypy.config.update({
- 'engine.autoreload_on': False,
- 'tools.debug_request.on': True,
- })
- cherrypy.tree.mount(Admin(), '/admin')
- app = Wrapper(cherrypy.tree)
- try:
- WSGIServer(app).run()
- finally:
- cherrypy.engine.stop()
- ################################################################################
Add Comment
Please, Sign In to add comment