LinkIt Smart Duo: Installing and testing the Requests library for Python

The objective of this post is to explain how to install the Python Requests library on the LinkIt Smart 7688 Duo and perform a simple test.


Introduction

The objective of this post is to explain how to install the Python Requests library on the LinkIt Smart 7688 Duo and perform a simple test.

The Requests library allows us to make and process HTTP requests very easily. You can check the API description for this library here.

To install the Requests library on the LinkIt Smart 7688 Duo, just open Putty and connect to the device. Then, type the command bellow and hit enter. The installation may take a while.

 pip install requests 

The code

To test the installation of the library, we will do a simple GET HTTP request on a website that has a fake online REST API for testing and prototyping [1].

The code we will be using is very simple and has already been explained in detail in this previous post. I will assume the use of the tool WinSCP to upload the file with the Python script to the LinkIt Smart 7688 Duo. You can check here a detailed guide on how to use this tool with the device.

We can create a new directory for the file or use an existing one. I will be using the /IoT/examples default directory, which comes with the installation of the operating system. Just name the file where the code will be developed as testRequests.py.

So, for the actual code, we start by importing the requests module.

 
import requests

Now, we simply call the get method defined in this module and pass the URL as argument. We will store the result in a variable.

 
response = requests.get('http://jsonplaceholder.typicode.com/users')

The returning value of this invocation will be a Response object. To print the response text of our request we just call the text property on the Response object.

 
print response.text

The final complete code can be seen bellow.

 
import requests
response = requests.get('http://jsonplaceholder.typicode.com/users')
print response.text

Testing the code

To test the code, just go back to Putty and navigate to the directory where the Python script file was saved. There, just type the following command and hit enter:

 
python testRequests.py

The code may take a while to execute. You should get something similar to figure 1.

linkit-smart-test-requests-library

Figure 1 – Output of the test program.

We can test the output obtained in the LinkIt Smart against the result obtained by accessing the URL in a web browser: http://jsonplaceholder.typicode.com/users. Since this is a fake API, we should always get the same user’s dummy data.

 

References

[1] http://jsonplaceholder.typicode.com/

Advertisements
This entry was posted in LinkIt Smart and tagged , , , , , , , , , . Bookmark the permalink.

One Response to LinkIt Smart Duo: Installing and testing the Requests library for Python

  1. Pingback: LinkIt Smart Duo: Sending HTTPS requests | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s