Introduction to Python Rest Server
In this article, we are discussing Python REST API. Usually, developers create APIs on the server to communicate with the client, whereas REST is abbreviated as representational state transfer, which the developers use to create APIs using a certain set of rules that REST provides. In Python, REST API can be created using a flask framework which is very simple and easy. REST contains a set of rules that takes HTTP protocol to provide create, update, read, and delete behavior on things or a collection of things. This REST API is used to build modern web development. In this, we will see the REST server in Python.
Working of REST API Server in Python
To create a REST API server using Python, we use a flask. Flask is a microframework for the creation of web applications, and it is very easy to use; hence REST API using flask is very simple.
Let’s get started by creating a REST server using a flask and Python. First, you need to install Python of the latest version or 3.7, and above then open the Python terminal to install flask; to do this, you need to write the command as below:
pip install flask
py –m pip install flask
After the installation of the flask, we need to set up the environment so that we can run our applications.
from flask import ( Flask, render_template ) app = Flask(__name__, template_folder="templates") @app.route('/') def home(): return render_template('home.html') if __name__ == '__main__': app.run(debug=True)
The above Python code is to run a web server, and it will run the application with the request page, which has HTML code which response with “Hello World” for the request page, and the HTML file is named as home.html. The python code is named as server.py, and the application can be run by this command line as mentioned below.
The terminal in which we run the command is a shell, and the command is as written below:
Once the application starts running, the webserver will port on the default port used by the flask and the port on which the application runs is 5000, such as “localhost:5000”. After the application is executed, you will see “Hello World” is displayed, and you can later add the extra code to develop the REST API
In the Python program, first, we need to import the flask module, which is necessary because we need to provide access to the application to the flask functionalities. Then we need to create a flask instance as in the above Python code, we can see the flask application instance created is the “app” variable, and we need to connect it to the URL route to home () ‘ / ’ such as @app.route (‘ / ’). This function is created so that we can call another flask function known as the render_template() function. This render_template() function is used to fetch the HTML program named home.html file from the template directories, and then it will return to the browser. This is how the web server works that are used to create a REST API server.
By default, the route answers to only GET request, and this can be done using a request that the flask must first import, and this can be seen as below:
From flask import Flask, request app = Flask(__name__) @app.route('/login', methods=['GET','POST']) def login(): if request.method == 'POST': #check user details from db login_user() elif request.method == 'GET': #serve login page serve_login_page()
The data can be passed via the POST method, which will explore the URL so that we can access the data via POST or PUT. To access the URL parameters (?key = value), we have to use the args attribute.
searchkeyword = request.args.get('key': '')
If we have no username or password, then a special “KeyError” has occurred, and it can be raised using this error; else, an HTTP 400 error page is displayed.
We can also upload files using flask and python. To do this, we need to set certain known as “enctype” attribute in the form and can use the save() method to store the file in the server system. This can be done using below sample code below.
From flask import Flask, request app = Flask(__name__) @app.route('/upload', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': static_file = request.files['the_file'] static_file.save('/var/www/uploads/filename.fileextension
This file uploaded can be accessed using the hostname of the server and the directory file after saving the file. This can be done using https:// “here mention the URL where the file is uploaded”.
This article creates the REST API server, and the REST is representational state transmission. In this article, we use Python for creating the REST API server using a flask. In this article, we have designed the web server using a flask that we need to install in the python package using a flask library that needs to be imported into the python program using the pip command.
This is a guide to Python Rest Server. Here we also discuss the Introduction and working of the rest API server in python and an example and its code implementation. You may also have a look at the following articles to learn more –