O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating the Requester class

Make yourself a new file in the same folder as your friendslist.py file named requester.py:

import jsonimport requestsclass Requester:    def __init__(self):        self.url = "http://127.0.0.1:5000"

As the name implies, the Requester class will be making use of the requests module to communicate with our web service. We will also need to use the json module to read any data which is returned.

In our __init__, we just need to keep a reference to the URL at which our web service operates. Keeping it here means that, if we change it for any reason, we only have one place in this class to update.

Since our web service uses both GET and POST endpoints, we can generalize our requesting by extracting it to a method:

def request(self, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required