from os import path import asyncio from growler import (App, create_http_server) from growler.middleware import (Logger, Renderer) app = App('GrowlerServer') this_dir = path.dirname(__file__) app.use(Logger()) app.use(Renderer(path.join(this_dir, "views"), "jade")) @app.get('/') def index(req, res): res.render("home") @app.get('/hello') def hello_world(req, res): res.send_text("Hello World!!") @app.use def error_handler(req, res, err): res.send_text("404 : Hello World!!")
#!/usr/bin/env python3 # # Example1 - DefaultSession # from growler import (App) from growler.middleware import (Logger, CookieParser, DefaultSessionStorage) app = App('Example1_Server') app.use(Logger()) app.use(CookieParser()) app.use(DefaultSessionStorage()) @app.get('/') def index(req, res): """ Return root page of website. """ number = req.session.get('counter', -1) req.session['counter'] = int(number) + 1 print(" -- Session '{id}' returned {counter} times".format(**req.session)) msg = "Hello!! You've been here [[%s]] times" % (req.session['counter']) res.send_text(msg) req.session.save() app.create_server_and_run_forever(host='127.0.0.1', port=8000)
def post_name(self, req, res): """ /name Submit your name to the server """ name = req.get_body() if name in self.name_dict: txt = "Already Created %s (%d)" % (name, self.name_dict[name]) else: self.name_dict[name] = 0 txt = "Created name %s." % (name) res.render_text(txt) def get_name(self, req, res): """ /name/:name Return the number of times this name has been returned """ name = req.params['name'] try: self.name_dict[name] += 1 txt = "%s : %d" % (name, self.name_dict[name]) except KeyError: txt = "No name %s." % (name) res.render_text(txt) app = App("Example3") app.use(QuickRoute('Helloo'), '/') app.create_server_and_run_forever(port=8000, host='127.0.0.1')
""" /name Submit your name to the server """ name = req.get_body() if name in self.name_dict: txt = "Already Created %s (%d)" % (name, self.name_dict[name]) else: self.name_dict[name] = 0 txt = "Created name %s." % (name) res.render_text(txt) def get_name(self, req, res): """ /name/:name Return the number of times this name has been returned """ name = req.params['name'] try: self.name_dict[name] += 1 txt = "%s : %d" % (name, self.name_dict[name]) except KeyError: txt = "No name %s." % (name) res.render_text(txt) app = App("Example3") app.use(QuickRoute('Helloo'), '/') app.print_middleware_tree() app.create_server_and_run_forever(port=8000, host='127.0.0.1')
#!/usr/bin/env python3 # # Example1 - DefaultSession # from growler import App from growler.middleware import Logger, CookieParser, DefaultSessionStorage app = App("Example1_Server") app.use(Logger()) app.use(CookieParser()) app.use(DefaultSessionStorage()) @app.get("/") def index(req, res): """ Return root page of website. """ req.session["counter"] = int(req.session.get("counter", -1)) + 1 print(" -- Session '{}' returned {} times".format(req.session["id"], req.session["counter"])) msg = "Hello!! You've been here [[%s]] times" % (req.session["counter"]) res.send_text(msg) req.session.save() app.create_server_and_run_forever(host="127.0.0.1", port=8000)
Example script to demonstrate route decorating, string-template rendering and low-level server. """ from os import path import asyncio from growler import (App) from growler.middleware import ( Logger, StringRenderer, ) app = App('GrowlerServer') view_dir = path.join(path.dirname(__file__), "views") app.use(Logger()) app.use(StringRenderer(view_dir)) @app.get('/') def index(req, res): obj = {'title': "FooBar"} res.render("home", obj) @app.get('/hello') def hello_world(req, res):