Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. # This is the ajaxlogin.py file,
  2. # which creates the sessions userid
  3. # and password, which is md5-encoded.
  4.  
  5. #!/usr/bin/env python
  6.  
  7. import sys, md5
  8. import cgitb; cgitb.enable()
  9. sys.path.append("/home/toxic_elegant/python")
  10. from wsgiref.handlers import CGIHandler
  11. from beaker.middleware import SessionMiddleware
  12. from weberror.errormiddleware import ErrorMiddleware
  13. from webob import Request, Response
  14. import elixir
  15. from sqlalchemy import create_engine, and_
  16.  
  17. from model import * # My functions
  18.  
  19. # Elixir and SQLAlchemy stuff
  20. elixir.metadata.bind = create_engine('mysql://username:password@mysql.host.com/db_name')
  21. elixir.setup_all()
  22.  
  23. def app(environ, start_response):
  24.     session = environ['beaker.session']
  25.     request = Request(environ)
  26.     response = Response()
  27.     headers = [('Content-type', 'text/html')]
  28.     username = request.POST['username']
  29.     password = request.POST['password']
  30.     password = md5.new(password).hexdigest()
  31.     myquery = User.query.filter(and_(User.user==username, User.password==password))
  32.     result = myquery.count()
  33.     if myquery.count() == 1:
  34.         myquery = myquery.one()
  35.         session['userid'] = myquery.id
  36.         session['password'] = myquery.password
  37.         result =  "You've been successfully logged in!"
  38.         session.save()
  39.     else:
  40.         result = "Sorry, but the combination you entered seems to be nonexistent. Either try again or contact the admin."
  41.     start_response('200 OK', headers)
  42.     return [str(result)]
  43.  
  44. session_opts = {'session.cookie_expires': True}
  45. app = ErrorMiddleware(app, debug=True)
  46. app = SessionMiddleware(app, session_opts)
  47. CGIHandler().run(app)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement