09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 11 ■ WEB APPLICATIONS<br />

Listing 11–2 also requires an accompanying template file, which is shown in Listing 11–3.<br />

Listing 11–2. Rewriting the WSGI Application With a Framework<br />

#!/usr/bin/env python<br />

# <strong>Foundations</strong> <strong>of</strong> <strong>Python</strong> <strong>Network</strong> <strong>Programming</strong> - Chapter 11 - wsgi_app.py<br />

# A simple web application built using the Bottle micro-framework.<br />

import base64, bottle<br />

bottle.debug(True)<br />

app = bottle.Bottle()<br />

@app.route('/encode')<br />

@bottle.view('bottle_template.html')<br />

def encode():<br />

» mystring = bottle.request.GET.get('mystring')<br />

» if mystring is None:<br />

» » bottle.abort(400, 'This form requires a "mystring" parameter')<br />

» return dict(mystring=mystring, myb=base64.b64encode(mystring))<br />

@app.route('/')<br />

@bottle.view('bottle_template.html')<br />

def index():<br />

» return dict(mystring=None)<br />

bottle.run(app=app, host='localhost', port=8080)<br />

In Listing 11–1, the attention was on the single incoming HTTP request, and the branches in our<br />

logic explored all <strong>of</strong> the possible lifespans for that particular protocol request. Listing 11–2 changes the<br />

focus to the pages that actually exist on the site and giving each <strong>of</strong> these pages reasonable behaviors. The<br />

same tree <strong>of</strong> possibilities exists, but the tree exists implicitly thanks to the possible URLs defined in the<br />

code, not because the programmer has written a large if statement.<br />

Listing 11–3. The Template That Goes With Listing 11–2<br />

%#!/usr/bin/env python<br />

%# <strong>Foundations</strong> <strong>of</strong> <strong>Python</strong> <strong>Network</strong> <strong>Programming</strong> - Chapter 11 - bottle_template.py<br />

%# The page template that goes with bottle_app.py.<br />

%#<br />

bottle_app.py<br />

<br />

%if mystring is None:<br />

» Welcome! Enter a string:<br />

» <br />

%else:<br />

» {{mystring}} base64 encoded is: {{myb}}<br />

» Return to the home page<br />

%end<br />

<br />

It might seem merely a pleasant convenience that we can use the Bottle SimpleTemplate to insert our<br />

variables into a web page and know that they will be escaped correctly. But the truth is that templates<br />

serve, just like schemes for URL dispatch, to re-orient our attention: instead <strong>of</strong> the resulting web page<br />

existing in our minds as what will result when the strings in our program listing are finally concatenated,<br />

we get to lay out its HTML intact, in order, and in a file that can actually take an .html extension and be<br />

188

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!