Homework 5ΒΆ

Due by noon on Thursday, Feb 13th.

  1. Merge hw4 into your master. Please don’t delete the ‘hw4’ branch :)

    Hand in this homework on branch ‘hw5’ on your github account, and set up a pull request between hw4 and your master branch. Don’t merge it, just set up the PR.

  2. Move all of your content-creation code (anything in server.py after reading in the request) into a new file, ‘app.py’, and refactor it to look like a WSGI application.

    More specifically, follow the WSGI app specification.

    Your app should work in the ref-server.py available here – you should be able to merge this into your repo by doing:

    git pull https://github.com/ctb/cse491-serverz.git hw5-wsgi

    This will give you a basic ‘app’ file and a demonstration of how to run it in ref-server.py.

    Once you’ve refactored your app.py code, you should get the same Web page results as from hw4, but through the WSGI server running in ref-server.py instead of with your own socket handling code.

  3. Refactor the handle_connection function to run WSGI apps; see the WSGI server info for an example, although you’ll need to ignore some of the CGI-specific details...

    Basically, what you need to do is separate out the actual HTTP request parsing from the code that generates a response (which, in any case, is now a WSGI app in app.py, right?). A few tips:

    • ‘environ’ is a dictionary that you create from the HTTP request; see environ variables.
    • you should fill in ‘REQUEST_METHOD’, ‘PATH_INFO’ (leave SCRIPT_NAME blank), ‘QUERY_STRING’, ‘CONTENT_TYPE’, ‘CONTENT_LENGTH’, and ‘wsgi.input’ in the environ dictionary;
    • wsgi.input is that StringIO object you used in hw4 – this contains the POST data, if any; empty otherwise.
    • ‘start_response’ should probably be defined within your handle connection_function (although there are other ways to do it). It is responsible for storing the status code and headers returned by the app, until the time comes to create the HTTP response.

    Your logic could look something like this:

    1. read in entire request
    2. parse request headers, build environ dictionary
    3. define start_response
    4. call WSGI app with start_response and environ
    5. build HTTP response from results of start_response and return value of WSGI app.

    The ‘simple_app’ in app.py on my hw5-wsgi branch (see app.py) is potentially a useful debugging tool; your server should work with it, as well as with the app in your app.py

  4. Template inheritance and proper HTML.

    Create a file base.html and use it as a “base template” to return proper-ish HTML, as in the jinja2 example from last year.

    (Each of your HTML files should inherit from this base.html and fill in only their specific content.)

    Please do specify an HTML title (<title> tag) for each page.

  5. Your tests still pass, right?

Previous topic

Day 13: Tuesday, Feb 18th, 2014

Next topic

Day 11: Tuesday, Feb 11th, 2014

This Page

Edit this document!

This file can be edited directly through the Web. Anyone can update and fix errors in this document with few clicks -- no downloads needed.

  1. Go to Homework 5 on GitHub.
  2. Edit files using GitHub's text editor in your web browser (see the 'Edit' tab on the top right of the file)
  3. Fill in the Commit message text box at the bottom of the page describing why you made the changes. Press the Propose file change button next to it when done.
  4. Then click Send a pull request.
  5. Your changes are now queued for review under the project's Pull requests tab on GitHub!

For an introduction to the documentation format please see the reST primer.