Growler Example in Jupyter


In [1]:
import growler

In [2]:
growler.__meta__.version_info


Out[2]:
(0, 6, 5)

Create growler application with name NotebookServer


In [3]:
app = growler.App("NotebookServer")

Add a general purpose method which prints ip address and the USER-AGENT header


In [4]:
@app.use
def print_client_info(req, res):
    ip = req.ip
    reqpath = req.path
    print("[{ip}] {path}".format(ip=ip, path=reqpath))
    print("  >", req.headers['USER-AGENT'])
    print(flush=True)

Next, add a route matching any GET requests for the root (/) of the site. This uses a simple global variable to count the number times this page has been accessed, and return text to the client


In [5]:
i = 0
@app.get("/")
def index(req, res):
    global i
    res.send_text("It Works! (%d)" % i)
    i += 1

We can see the tree of middleware all requests will pass through - Notice the router object that was implicitly created which will match all requests.


In [6]:
app.print_middleware_tree()


NotebookServer
├── ALL \/ <function print_client_info at 0x10b5b9950>
├── ALL \/ <growler.Router object at 0x10b5ce748>
│   └── GET \/ <function index at 0x10b5b9e18>
┴

Use the helper method to create the asyncio server listening on port 9000.


In [7]:
app.create_server_and_run_forever(host='127.0.0.1', port=9000)


[127.0.0.1] /
  > Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:44.0) Gecko/20100101 Firefox/44.0

[127.0.0.1] /
  > Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: