0

$USD

$USD
TUTORIALS Raspberry Pi

Raspberry Pi 3 Raspbian: Exposing a Flask server to the local network

DFRobot Jun 08 2018 1123

Introduction

In this tutorial we will check how to expose a Flask server to the local network, so it can be reached from clients connected to that same network.

We have already covered how to create a simple “Hello World” Flask server on this previous tutorial. It also covers how to install Flask on the Raspberry Pi 3, in case you haven’t done it yet.

In this tutorial, instead of returning a “Hello World” message, we will return the current date and time on the Raspberry Pi and make the server accessible so clients running on other machines can reach it.

In order for a client to be able to reach the Flask server, it will need to know the Raspbery Pi’s local address on the network. You can check a detailed guide on how to obtain the local IP of the Raspberry Pi on this previous post.

This tutorial was tested on a Raspberry Pi 3 model B+, running version 4.9 of Raspbian, installed using NOOBS.

 

The Python code

As usual, the first thing we need to do is importing the Flask class from the flask module, so we can create an object of this class and configure the server.

We will also import the datetime class from the datetime module, so we can obtain the current time and date on the Raspberry Pi.

from flask import Flask
from datetime import datetime

After that we need to create an object of class Flask. The constructor of this class receives as input the name of the module or package of our application. For this simple application we can pass the value __name__, which contains the name of the current module.

app = Flask(__name__)

Next we will define the route of our server that will return the current date and time of the Raspberry Pi. We will call the route “/time”.

@app.route('/time')
def getTime():
# Route handling code

The implementation of the handling function will be very simple. We will just get the current date and time by calling the now class method on the datetime class. Then, we will convert the returned value to a string and return it to the client.

time = datetime.now()
return "RPI3 date and time: " + str(time)

Finally, we need to call the run method on our Flask object, so the server starts listening to incoming HTTP requests.

As first input, this method receives the IP where the server should be listening, and has second input the port. The IP is passed as a string and the port as a number.

Since we want to expose our server to the local network, we use the 0.0.0.0 IP address, which indicates that the server should listen on all available IPs. As port, we will use the value 8090, although you can test with other values.

The final complete code can be seen below.

from flask import Flask
from datetime import datetime

app = Flask(__name__)

@app.route('/time')
def getTime():
    time = datetime.now()
    return "RPI3 date and time: " + str(time)

app.run(host='0.0.0.0', port= 8090)

 

Testing the code

To test the code, simply run the previous program on the Python environment of your choice. In my case, I’ve used IDLE.

Next, as mentioned before, you need to get the IP of your Raspberry Pi on the network to which it is connected. The quickest way is by opening a command line and use the ifconfig command, as detailed in this previous post.

Then, open a web browser in another computer connected to the same network as the Raspberry Pi. On the address bar type the URL below, replacing #yourRaspberryIp# by the IP you have obtained.

http://#yourRaspberryIp#:8090/time

You should get an output similar to figure 1, which shows the current date and time of the Raspberry Pi.

Contacting Raspberry Pi Flask server on local network

Figure 1 – Answer to the HTTP request performed to the Flask server.