Introduction to Flask
A web service is a method for communicating between two devices over a network. So let’s say we have our web service and a user who wants to make a request. So the user sends the request with some information. The request has some parameters, then the user gets back result with the answer of this request.
We use Flask for implementing the web service and it takes care of all the internals.
Writing a simple ping/pong function
In a simple sample implementation, we want to create a ping-pong service. This means we send a “ping” request to a web service, and it responds with “pong.” To achieve this, we create a file named ping.py with the content of the next snippet:
def ping():
return "PONG"
To easily test whether it is working or not, you can follow these steps:
- Open a console.
- Launch an interactive Python session with
ipython. - Import the ping module by typing
import ping. - Call the
ping.ping()function.
When you execute these steps, it should return “PONG.”
Installing Flask
The next step is to turn this simple function into a web service. To achieve this, you need to install Flask, a Python web framework. You can install Flask using pip. Here are the steps to install Flask:
- Open your command prompt or terminal.
- Run the following command to install Flask:
pip install flask
This will download and install Flask along with its dependencies.
Writing a simple ping/pong app
Once Flask is installed, we can proceed to create our web service using Flask and integrate our “ping-pong” functionality into it.
We employ a decorator for our definition. A decorator is a mechanism for adding additional functionality to our functions. This added functionality will enable us to transform this function into a web service.
In the snippet below, we begin by importing Flask and creating an app with the name ‘ping’. We then use the @app.route('/ping') decorator to designate the ping function as a web service accessible at the “/ping” route. In this context, ‘route’ specifies the address at which the function will be accessible.
The ‘method’ specifies how we can access this address. When we visit the website in a browser, the browser sends a GET request. In this case, we want to access the function using the GET method, and it will be located at the ‘/ping’ address.
Finally, we start the Flask application with app.run(). It’s important to note that app.run() should be placed inside the main block (if __name__ == '__main__') to ensure it is executed only when we run the script using “python ping.py” in the top-level script environment.
from flask import Flask
app = Flask('ping')
@app.route('/ping', methods=['GET'])
def ping():
return "PONG"
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=9696)
Querying the simple app with ‘curl’ and browser
Now, when you run the ‘ping.py’ script that starts the web service and open a web browser or use a tool like ‘curl’ to access http://localhost:9696/ping, you should receive “PONG” as the response. This effectively transforms your ‘ping’ function into a simple web service.
‘Curl’ is a specialized command-line utility for communicating with web services. To use it, simply open a new console and type the following command:
curl http://0.0.0.0:9696/ping
Alternatively, you can access it via a web browser by opening a browser and typing the following URL:
http://localhost:9696/ping
In both cases, the web service will respond with “PONG”.