Python

champak
₹2,000.00 ₹1,500.00
The Python Programming Language at Champak's World
  • 15 students
  • 22 lessons
  • 0 quizzes
  • 10 week duration

Python

Running a Web Server using Python in Spyder

This is one of the more important lessons in Python. In this lesson we shall learn how to create and run a Web Server using Python on Spyder. In addition we will implement REST API for insert, update, delete and search.

To begin with let us create a server.

Flask is a Python web framework for developing web applications. There is another famous web framework for Python namely DJango. Flask is lightweight as compared to DJango and therefore we will start with Flask

To create a server you begin by importing Flask into your Python program. Then you create an app.


from flask import Flask
app = Flask(__name__)

@app.route("/",)

def index():
return "Welcome to Python Server"
app.run(host="localhost", port=int("777"))

We are importing  Flask in the first line
from flask import Flask
The second line creates the app by calling its constructor.
app = Flask(__name__)

__name__ is the name of the current module and will be __main__
You can actually check this by writing
print(__name__)

in a program.

@app.route(“/”,) defines the default web path and then we have  a return statement

return “Welcome to Python Server”

This will show up in the browser.
Finally we use app.run to run the application.

app.run() will run the server on the default port =5000.

app.run(host=”localhost”, port=int(“80”))

Check up the console and get the portno.

runfile(‘C:/Users/Champak Roy/WebServices.py’, wdir=’C:/Users/Champak Roy’)
__main__
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 – – [02/May/2019 12:59:57] “GET / HTTP/1.1” 200 –
127.0.0.1 – – [02/May/2019 12:59:57] “GET /favicon.ico HTTP/1.1” 404 –

Run the given code and open the url in the browser.

 

 

Finally we have a complete code for insert, delete update and search as promised earlier.


from flask import Flask
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("/")

def index():



return "Welcome to Python Server"

@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__":
#print(__name__)
app.run(host="localhost", port=int("777"))


Let us analyze the Insert function

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

def insert(citynumber,cityname):

d1[int(citynumber)]=cityname
return “Inserted”

The route starts with insert and contains two parameters citynumber and cityname. The cityname will be inserted with the key citynumber.

http://localhost:777/insert/1/Varanasi/

The output shows Inserted.

Try out the other URLs. That is an assignment.

end

Leave a Reply

Your email address will not be published. Required fields are marked *