A Python client for our flask REST/CRUD servers

1   Introduction

A previous post provided bash scripts to drive and test our Flask REST servers. See: http://www.davekuhlman.org/python-flask-restful-rest-crud.html#testing

In this post we provide a Python client that can submit requests to our REST server in order to perform get, add, update, and delete operations.

This script uses the requests package to make HTTP requests.

You can find the code for this Python HTTP REST client here: flask-rest-client.py.

2   Notes and explanation

  1. We use a dictionary to map each command (a string) to the corresponding function that implements that command. See the Function_map dictionary.
  2. The argparse module from the standard Python library processes command line options and arguments for us.
  3. The urllib module in the Python standard library has changed from Python 2 to Python 3. Furthermore, the Python 2 version of that module does not seem to support PUT requests. By using the requests package we can use the same code for Python 2 and Python 3 and we are able to make the PUT requests that we need in order to do an update to an existing task.
  4. Note that the requests module converts a dictionary to URL-encoded form data/parameters for us. In our case, we need to create that dictionary, and we use the helper function convertparams to do so.
  5. Our Flask REST server returns JSON. requests makes it easy to access that returned JSON content with response.json().

links