Web Services

A web service is a computer program that listens to a network connection to serve results over that network on request. A basic web page is provided by a web server returning documents when a specific URL is requested. More complicated pages, like Google search results, have some computation done on the server before an ad-hoc document is returned over the network.

REST

A REST (representational state transfer) API refers to a system of communication that is used to exchange information over the world-wide web. There are two important ideas behind a REST API. First, there are resources identified by a URL. The URL https://www.wikipedia.org/ is a resource for the home page of Wikipedia, while the URL https://en.wikipedia.org/wiki/Representational_state_transfer is a resource for the page on REST. The second are verbs that can be used to interact with a resource. These verbs include GET, PUT, POST, and DELETE. The GET verb is used to retrieve information from a resource; this is what happens when you click on either of the links above. PUT and POST are used to send information to a resource; this might be used to send search terms to a search engine, or to send user information to a sign-up page. What happens when certain verbs are used on certain resources is entirely up to the web service providing them, although there are conventions that should be followed.

RESTful APIs

A RESTful API is simply an API that follows the REST architecture. Such an API can respond to requests with any type of information; it can return an HTML web page, a JSON document, or an image. We will create web services that return both statically and dynamically generated documents.

Creating a web service

Unsurprisingly, there are a large number of Python libraries that make it very easy to create a web service. We will use flask, which is simple enough to get us started quickly and powerful enough to help you do more or less anything you’d like in the future.

From the Flask homepage, the following is all you need to write to create a simple web service that responds to GET requests to the root URL with the string Hello World!.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

Saving this to hello.py and executing python3 hello.py will cause a web service to start listening at the IP address 127.0.0.1:5000, which is accessible only from your local machine. A slightly more interesting example would be something like the following.

from flask import Flask
app = Flask(__name__)

@app.route("/assignment/<name>")
def return_assignment(name):
    if name == "git":
        return work_for_git_assignment()
    elif name == "anscombe":
        return work_for_anscombe_assignment()
    else:
        return "Unknown assignment"

if __name__ == "__main__":
    app.run()

This code would listen at 127.0.0.1:5000/assignment/SOMETHING to serve up an assignment named SOMETHING, and then will dynamically return the appropriate result. A GET request to 127.0.0.1:5000/assignment/git would return your work on the Git assignment, a request to 127.0.0.1:5000/assignment/anscombe would return your work on Anscombe’s Quartet, and any other URl would return an error message.