Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # gsr-zeit.py
  4. #
  5. # MyCherry application to test session handling
  6. #
  7.  
  8. import cherrypy
  9.  
  10. class GsrZeit:
  11.     """
  12.    The main application class
  13.    """
  14.  
  15.     # some page footers common to most html files produced
  16.     htmlfooter = '<hr><a href="index">Index</a>|<a href="testpage">Testpage</a>'
  17.     htmllogout = '<br><a href="logout">Logout</a>'
  18.  
  19.     def index(self):
  20.         """
  21.        The entry point when browser is pointed to this apps' root
  22.        It returns a simple login form and sends its POST data to the login function
  23.        """
  24.  
  25.         return """
  26.            <h3>Session-Test-Form</h3>
  27.            <form action="login" method="post">
  28.                <p>Username</p>
  29.                <input type="text" name="username" value=""
  30.                    size="15" maxlength="40"/>
  31.                <p>Password</p>
  32.                <input type="password" name="password" value=""
  33.                    size="10" maxlength="40"/>
  34.                <p><input type="submit" value="Login"/></p>
  35.                <p><input type="reset" value="Clear"/></p>
  36.            </form>
  37.            """ + self.htmlfooter
  38.  
  39.     index.exposed = True
  40.  
  41.     @cherrypy.expose
  42.     def login(self, username=None, password=None):
  43.         """
  44.        Function reads POST data from index and sets up session parameters
  45.        """
  46.  
  47.         if self.loggedin():
  48.             return 'Why login? You are already logged in' + self.htmlfooter + self.htmllogout
  49.  
  50.         if username:
  51.             cherrypy.session['username'] = username
  52.             cherrypy.session['password'] = password
  53.             return 'You are now logged in.' + self.htmlfooter + self.htmllogout
  54.         else:
  55.             return self.testpage()
  56.  
  57.     @cherrypy.expose
  58.     def logout(self):
  59.         """
  60.        Clears/deletes session when somebody is logged in
  61.        """
  62.  
  63.         if self.loggedin():
  64.             cherrypy.session.clear()    # not sure about this
  65.             cherrypy.session.delete()   # dito
  66.             return 'You are now logged out. ' + self.htmlfooter + self.htmllogout
  67.         else:
  68.             return 'Why logout? You were not logged in.' + self.htmlfooter
  69.  
  70.     @cherrypy.expose
  71.     def testpage(self):
  72.         """
  73.        Just a page function to test session results
  74.        """
  75.  
  76.         if self.loggedin():
  77.             return 'Logged in as ' + cherrypy.session['username'] + \
  78.                 self.htmlfooter + self.htmllogout
  79.         else:
  80.             return 'Not logged in' + self.htmlfooter
  81.  
  82.     def loggedin(self):
  83.         """
  84.        Check if a session user has logged in
  85.        """
  86.  
  87.         if cherrypy.session.has_key('username'):
  88.             return True
  89.         else:
  90.             return False
  91.  
  92.  
  93. if __name__ == '__main__':
  94.     root = GsrZeit()
  95.     cherrypy.config.update({'tools.sessions.on': True})
  96.     cherrypy.config.update({'server.socket_host': '0.0.0.0'})  # listen on all interfaces
  97.     cherrypy.quickstart(root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement