Guest User

Untitled

a guest
May 10th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #!/home2/bilumspn/bin/python2.5
  2.  
  3. #################################### IMPORTS ###################################
  4.  
  5. #Std Libs
  6. import cgi
  7. import os
  8. import sys
  9. import re
  10. import functools
  11.  
  12. from pprint import pformat
  13.  
  14. # Fast CGI
  15. from flup.server.fcgi import WSGIServer
  16.  
  17. # 3rd Parties
  18. import cherrypy
  19.  
  20. ################################# RELATIVE URLS ################################
  21.  
  22. url = functools.partial(cherrypy.url, relative='server')
  23.  
  24. ################################# WSGI WRAPPER #################################
  25.  
  26. class Wrapper(object):
  27. def __init__(self, app):
  28. self.app = app
  29. def __call__(self, environ, start_response):
  30. environ['SCRIPT_NAME'] = ''
  31. return self.app(environ, start_response)
  32.  
  33. ################################### DEBUGGING ##################################
  34.  
  35. def debug_request(multiplier=1):
  36. f = open('debugging', 'w')
  37. f.write(pformat(cherrypy.request.__dict__))
  38. f.close()
  39.  
  40. cherrypy.tools.debug_request = cherrypy.Tool('on_start_resource', debug_request)
  41.  
  42. ############################# STRING INTERPOLATION #############################
  43.  
  44. def _(string):
  45. frame = sys._getframe(1)
  46. locals = frame.f_locals
  47. globals = frame.f_globals
  48. for item in re.findall(r'\$\{([^{]*)\}', string):
  49. string = string.replace('${%s}' % item,
  50. str(eval(item, globals, locals)))
  51. return string
  52.  
  53. ################################## APPLICATION #################################
  54.  
  55. class Admin:
  56. @cherrypy.expose
  57. def index(self):
  58. return _("""
  59. <a href=${url('login')}> Login? </a> """)
  60.  
  61. @cherrypy.expose
  62. def login(self, username=None, password=None, **kw):
  63. if cherrypy.request.method == 'POST':
  64. return _('Hello ${username}')
  65. else:
  66. return _("""
  67. <form action='${url('login')}' method='POST'>
  68. User: <input type='text' name='username'/>
  69. Password: <input type='password' name='password'/>
  70. <input type='submit' name='submit' value='login'/>
  71. </form> """)
  72.  
  73. ################################ FCGI BOOTSTRAP ################################
  74.  
  75. cherrypy.server.unsubscribe()
  76. cherrypy.config.update({
  77. 'engine.autoreload_on': False,
  78. 'tools.debug_request.on': True,
  79. })
  80.  
  81. cherrypy.tree.mount(Admin(), '/admin')
  82. app = Wrapper(cherrypy.tree)
  83.  
  84. try:
  85. WSGIServer(app).run()
  86. finally:
  87. cherrypy.engine.stop()
  88.  
  89. ################################################################################
Add Comment
Please, Sign In to add comment