Introducing REST in Python

REST stands for Representational State Transfer Technology and can be considered a successor to Web Services. RESTful APIs can be used to GET, POST, PUT and DELETE data. This post provides very simple code for this purpose.
To start we will first run a simple server in Python and access it in a Web Browser.
The following code will create a web server.

 

from flask import Flask
from flask import jsonify
app = Flask(__name__)
@app.route("/",)
def f():
   
    d1="From a Python Web Server"
    return d1
if __name__ == "__main__":
    app.run()

 

 

 

 

 

from flask import Flask means importing Flask from the package flask.
in the next line we create an instance of a web application.
app = Flask(__name__)
__name__ is a special variable in Python. It will be equal to __main__ if the Python file is being executed as a main program.
@app.route(“/”) sets the url to the base. Then we have created an function returning a value. Python will run the web server on port 5000.

To change the port no we can use the following command
app.run(host=”localhost”, port=int(“80”))

The complete program now.

from flask import Flask
from flask import jsonify
app = Flask(__name__)
@app.route("/",)
def f():
   
    d1="From a Python Web Server"
    return d1
if __name__ == "__main__":
    app.run(host="localhost", port=int("80"))

 

Now, we will create a dictionary to store a list of cities and provide a options for adding, deleting, changing and searching.

Let create a dictionary and return it in JSON format now.

from flask import Flask
from flask import jsonify
app = Flask(__name__)
@app.route("/",)
def f():
   
    d1=jsonify({2:"Mumbai",1:"Jaipur",3:"Varanasi"})
    return d1
if __name__ == "__main__":
    app.run(host="localhost", port=int("80"))

 

 

The search option

from flask import Flask
from flask import jsonify
app = Flask(__name__)
@app.route("/search/<cityname>/",)
def search(cityname):
   
    d1={2:"Mumbai",1:"Jaipur",3:"Varanasi"}
    return d1.get(int(cityname))
    if __name__ == "__main__":
     app.run(host="localhost", port=int("80"))

@app.route(“/search/<cityname>/”,)
search
defines a url and <cityname> defines a parameter.
Searching for Mumbai will require a url http://localhost/search/2/, Jaipur http://localhost/search/1/

 

 

 

from flask import Flask
from flask import jsonify
app = Flask(__name__)
d1={2:"Mumbai",1:"Jaipur",3:"Varanasi"}
@app.route("/search/<cityname>/",)

def search(cityname):


result=d1.get(int(cityname),"Not Found")
return result

@app.route("/insert/<citynumber>/<cityname>/",)

def insert(citynumber,cityname):


d1[int(citynumber)]=cityname
return "Inserted"

@app.route("/update/<citynumber>/<cityname>/",)

def update(citynumber,cityname):


d1[int(citynumber)]=cityname
return "Updated"

@app.route("/delete/<citynumber>/",)

def delete(citynumber):

try:
del( d1[int(citynumber)])
return "Deleted"
except:
return "Not Found"
if __name__ == "__main__":
app.run(host="localhost", port=int("80"))

Leave a Reply