MP7
aiohttp lite
Due dates may be found on the schedule.

In this MP you’ll implement key components of the web framework we’ll use for several MPs later in the semester. Some of the design decisions might seem a little odd at first, but make sense in the larger context of aiohttp, the library we’ll use for building web services later in the term.

A major part of this MP is parsing and creating HTTP messages. HTTP messages are defined in the following standards:

1 Initial Files

mp7.zip contains a simple web server to use in testing once your code is fully complete, consisting of demoserver.py and the microbe/ and chat/ folders. It also contains a starter file, web.py, which you will modify and automated tests in test_webserver.py.

2 MP Structure

This MP has more imposed structure than some others because of the desire to have it mimic how aiohttp is organized.

You will implement

We have implemented for you

We also provide an example server you can us to test in the browser and a set of automated tests.

3 class Response

A Response needs to be creatable and convertible to bytes.

The constructor has only optional keyword arguments:

We also suggest making a function named self __bytes__(self) -> byte that produces the encoded bytes of an HTTP message representing the request object.

Going between bytes and str

To decode bytes into a string, use bytesobject.decode('utf-8'); to encode them again, use stringobject.encode('utf-8'). Any other character set can be used instead of 'utf-8' if you have a reason to do so.

Methods with double-underscores in Python

Python allows classes to overload many different aspects of the language. Classes do this by defining specific methods, all of which use two underscores at the beginning and end of the name. For example,

There are many more such methods; see the special method names section of the Python documentation for more.

4 class Request

Requests are created by your code (particularly in Application.serve), and can have any constructor you wish. The objects are passed into functions that implement web services, so they have several member methods and properties that you need to implement. These methods return various values extracted from an HTTP request.

Methods vs Properties

If a member method is preceded by @property, it is invoked without parentheses.

For the most part you can just assume that @property works. If you are curious, under the hood it works by changing what the . operator does; you can read more about it in the official documentation of the property class.

The docstrings of the methods and properties explain what each one should return. These often use terminology found in the HTTP specification.

5 class Application

The application class has two important methods. You might find it helpful to add other methods and a constructor as well.

6 Testing your code

6.1 Development testing

You probably want to write your own small programs to test your code during development. For example,

6.2 Nearly-there Testing

Once your code is mostly complete, you should be able to run python3 demoserver.py and then

The browser sends more complicated HTTP requests with many more headers than curl, but should generally work the same.

6.3 Final Testing

python3 -m pytest

will run a set of automated tests. These should all pass. Adding -v will output more verbose results.

make test

will run the same automated tests but also give you your projected score on the MP.