.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.
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.)
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 |
Ask your instructor or TA a question by email, in office hours, or on discord.
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
urllib.request.urlopen("http://example.com/")
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>
>>>
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
Here is a URL that retrieves information about a specific television show episode from the TVMaze API:
http://api.tvmaze.com/episodes/209354
Write a program hwk13prob2.py
that attempts to do the following:
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.
urllib.request
, json
"""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
try:
res_obj = urllib.request.urlopen("http://api.tvmaze.com/episodes/209354")
except:
print("TVMaze API is not working: Failed to load a URL")
exit()
try:
json_data = json.load(res_obj)
except:
print("TVMaze API is not working: Response could not be parsed as JSON")
exit()
print("TVMaze API seems to be working")
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.
flask
"""Offers API that replies to requests at / with a fixed JSON object"""
import flask
app = flask.Flask("Heartbeat")
@app.route("/")
def heartbeat_api():
heartbeat_dict = {"up":True}
return flask.jsonify(heartbeat_dict)
app.run(port=3000)