Profile
Pastes: 75988
Members: 1482

Paste

Plain view | Edit code: here. | Add this to your website. | Report abuse.

Short URL: http://goo.gl/sI8uq

Pasted as Plain Text by count12 on Thursday, November 1st, 2012 7:13am ( 2 years ago )

  1. #!/usr/bin/python
  2.  
  3. import os
  4.  
  5.  
  6. import web
  7.  
  8. urls = (
  9.  '/', 'index',
  10.  '/login', 'Login',
  11.  '/logout', 'Logout',
  12.  '/add', 'add',
  13.  '/secure','Secure',
  14.      )
  15.  
  16. render=web.template.render('/var/www/pvperformance/test/templates/', base='layout')
  17. mrender=web.template.render('/var/www/pvperformance/test/templates/' )
  18. db=web.database(dbn='sqlite',db='/var/www/pvperformance/test/users.db')
  19.  
  20.  
  21.  
  22. app = web.application(urls, globals())
  23. application = app.wsgifunc()
  24. session = web.session.Session(app,
  25.                web.session.DiskStore('/var/www/pvperformance/test/sessions'),
  26.                initializer={'count': 0})
  27.  
  28.  
  29.  
  30. class index:
  31.  def GET(self):
  32.   return render.index()
  33.  
  34.  
  35. class Secure:
  36.  def GET(self):
  37.   return """
  38.    <html>Hello %s
  39.    <hr>
  40.    <a href="/logout">Log me out</a></html>
  41.    """ % web.ctx.username  
  42.  
  43.  
  44. class Login:
  45.  
  46.  def GET(self):
  47.   return """
  48.   <html>
  49.   <form acti method="post">
  50.   <input type="text" name="username">
  51.   <input type="submit" value="Login">
  52.   </form>
  53.   </html>
  54.   """
  55.  
  56.  def POST(self):
  57.   # only set cookie if user login succeeds
  58.   name = web.input(username=None).username
  59.   if name:
  60.    web.setcookie('username', name)
  61.   raise web.seeother('/secure')
  62.  
  63.  
  64. class Logout:
  65.  
  66.  def GET(self):
  67.   web.setcookie('username', '', expires=-1)
  68.   raise web.seeother('/login')
  69.  
  70.  
  71. # Auth Processor
  72. def auth_app_processor(handle):
  73.  path = web.ctx.path
  74.  web.ctx.username = name = web.cookies(username=None).username
  75.  if not name and path != '/login':
  76.   raise web.seeother('/login')
  77.  return handle()
  78.  
  79.  
  80. app = app.wsgifunc()
  81. app.add_processor(auth_app_processor)

Revise this Paste
Child(s): 56457
Your Name:
Code Language:
 
Security Image:
Text seen in Image:
Comments

Nothing has been added as yet. Post a comment.