def test_sessions(webapp): Sessions().register(webapp) cj = CookieJar() opener = build_opener(HTTPCookieProcessor(cj)) f = opener.open("%s/test_sessions" % webapp.server.http.base) data = f.read() data = data.decode("utf-8") d = loads(data) assert d["success"] assert d["message"] == "Hello World!" f = opener.open("%s/test_sessions/test" % webapp.server.http.base) data = f.read() data = data.decode("utf-8") d = loads(data) assert d["success"] assert d["message"] == "Hello test" f = opener.open("%s/test_sessions" % webapp.server.http.base) data = f.read() data = data.decode("utf-8") d = loads(data) assert d["success"] assert d["message"] == "Hello test"
def test_expire(webapp): Sessions().register(webapp) cj = CookieJar() opener = build_opener(HTTPCookieProcessor(cj)) f = opener.open(webapp.server.http.base) s = f.read() assert s == b"Hello World!" f = opener.open(webapp.server.http.base + "/test") s = f.read() assert s == b"Hello test" f = opener.open(webapp.server.http.base) s = f.read() assert s == b"Hello test" f = opener.open(webapp.server.http.base + "/logout") s = f.read() assert s == b"OK" f = opener.open(webapp.server.http.base) s = f.read() assert s == b"Hello World!"
#!/usr/bin/env python from circuits.web import Controller, Server, Sessions class Root(Controller): def index(self, name="World"): if "name" in self.session: name = self.session["name"] self.session["name"] = name return "Hello %s!" % name app = Server(("0.0.0.0", 8000)) Sessions().register(app) Root().register(app) app.run()
def done(self): if self.session('buyer', False): email_a = self.session['buyer'] return 'success! email has been sent to %s' % (email_a) else: return 'error page' def admin(self, **kwarg): # show admin page if user is logged in if self.session('logged_in', False): return 'todo' # show login page if user is not logged in else: if self.request.method == 'POST': # password for admin page set here correctpass = '******' # check if the password submitted is correct if kwarg['password'] == correctpass: # authenticate the user and redirect to admin page self.session['logged_in'] = True return self.redirect('/admin') # display error page if password is not correct else: return 'error page' elif self.request.method == 'GET': return 'login page' (Server('0.0.0.0:8000') + Root() + Sessions()).run()
def main(): config = Config() manager = Manager() if config.get("debug"): manager += Debugger( events=config.get("verbose"), file=config.get("errorlog"), ) environ = Environment(config) SignalHandler(environ).register(environ) manager += environ if config.get("sock") is not None: bind = config.get("sock") elif ":" in config.get("bind"): address, port = config.get("bind").split(":") bind = ( address, int(port), ) else: bind = ( config.get("bind"), config.get("port"), ) server = (Server(bind) + Sessions() + Root(environ) + CacheControl(environ) + ErrorHandler(environ)) if MemoryMonitor is not None: MemoryMonitor(channel="/memory").register(server) if not config.get("disable-logging"): server += Logger(file=config.get("accesslog", sys.stdout)) if not config.get("disable-static"): server += Static(docroot=os.path.join(config.get("theme"), "htdocs")) if not config.get("disable-hgweb"): baseui = ui() baseui.setconfig("web", "prefix", "/+hg") baseui.setconfig("web", "style", "gitweb") baseui.setconfig("web", "allow_push", "*") baseui.setconfig("web", "push_ssl", False) baseui.setconfig("web", "allow_archive", ["bz2", "gz", "zip"]) baseui.setconfig("web", "description", config.get("description")) server += Gateway({ "/+hg": hgweb(environ.storage.repo_path, config.get("name"), baseui) }) if not config.get("disable-compression"): server += Compression(environ) if config.get("daemon"): manager += Daemon(config.get("pidfile")) server.register(manager) manager.run()