A document from MCS 260 Fall 2021, instructor David Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Homework 13 Solutions

  • Course instructor: David Dumas
  • Solutions prepared by: Kylash Viswanathan


  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)


This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, November 23, 2021.


This homework assignment focuses on networks in general and using HTTP in Python (servers and clients).


Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

The course materials and other resources you are allowed to refer to for this homework are:

(Lecture videos are not linked on course documents, but video links can be found in the course course Blackboard site.)

Point distribution

This homework assignment has 2 problems, numbered 2 and 3. Thus the grading breakdown is:

Points Item
2 Autograder
4 Problem 2
4 Problem 3
10 Total

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

IMPORTANT: Make sure you can load URLS

You won't be able to complete problem 2 on this homework unless you are able to load HTTP URLs in Python. That should be the case anywhere Python is installed and you have internet access, but it is worth checking to be sure. Try these two commands in the Python REPL:

import urllib.request

If this doesn't work, contact your TA or instructor right away and copy the error message into your email.

Here's what it looks like when these commands succeed:

>>> import urllib.request
>>> urllib.request.urlopen("http://example.com/")
<http.client.HTTPResponse object at 0x7fdfd72ef6d0>

( 1. There's no problem 1 )

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

2. Is TVMaze working?

Here is a URL that retrieves information about a specific television show episode from the TVMaze API:


Write a program hwk13prob2.py that attempts to do the following:

  • Load the URL given above
  • Interpret the response as JSON

If those steps work, it should simply print

TVMaze API seems to be working

But if one of the steps listed above fails, the program should exit with some kind of exception before printing the success message. It's OK if the exception message is ugly and complicated; you don't need to catch it.

Note that this program doesn't take any command line arguments.


  • Make a program that just attempts the operations and prints the success message. Then temporarily change the URL it loads to simulate various failure modes, using e.g. a URL like https://neverssl.com/asdfhjkahsdjkfbs (returns 403 Forbidden HTTP error) and http://example.com/ (returns HTML, which can't be interpreted as JSON).

  • Use the solution of worksheet 13, problem 1 as a starting point.

Restricted methods note

  • The only module(s) you are allowed to import are: urllib.request, json
  • You can only use Python syntax and techniques we've covered in MCS 260


In [2]:
"""Performs a url request to a given website and renders the object as a json object. If successful, prints
a success message. Otherwise catches the exception thrown and prints an "invalid url" message"""
import json
import urllib.request

    res_obj = urllib.request.urlopen("http://api.tvmaze.com/episodes/209354")
    print("TVMaze API is not working: Failed to load a URL")

    json_data = json.load(res_obj)
    print("TVMaze API is not working: Response could not be parsed as JSON")

print("TVMaze API seems to be working")
TVMaze API seems to be working

3. Heartbeat API

Sometimes an API is used for something as simple as checking whether a computer is still running.

Write a Flask API where accessing the document / (e.g. as http://localhost:3000/) will return a JSON object that simply contains

{ "up": true }

which is the JSON way to encode the Python dictionary

{ "up": True }

Put your Flask API in a file hwk13prob3.py.

Note that this program doesn't take any command line arguments.


  • Remember, flask has a function flask.jsonify to turn a Python object into a suitable return type so that a URL will send back JSON data. Check the worksheet 13 solutions for an example.

  • Use the solution of worksheet 13, problem 2 or the sample program apielement.py as a starting point.

Restricted methods note

  • The only module(s) you are allowed to import are: flask
  • You can only use Python syntax and techniques we've covered in MCS 260


In [1]:
"""Offers API that replies to requests at / with a fixed JSON object"""
import flask
app =  flask.Flask("Heartbeat")

def heartbeat_api(): 
    heartbeat_dict = {"up":True}
    return flask.jsonify(heartbeat_dict)

 * Serving Flask app "Heartbeat" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on (Press CTRL+C to quit) - - [30/Nov/2021 13:24:23] "GET / HTTP/1.1" 200 - - - [30/Nov/2021 13:24:24] "GET /favicon.ico HTTP/1.1" 404 -

Revision history

  • 2021-11-30 Initial release of solutions