
- 22 students
- 41 lessons
- 0 quizzes
- 10 week duration
-
Python Basics
- Installing Anaconda and running a sample Python Program
- How to use the Print function in Python?
- Arithmetic Operations in Python
- Running a Web Server using Python in Spyder
- Input Output in Python
- Starting Plots in Python
- For loop in Python
- Installing and running DJango
- Linear and Polynomial Regression using Python
- Some Problems on Lists
- Creating Classes in Python
- Inheritance in Python
- How to implement Multiple Inheritance in Python
- Using Flask-1
- Lists in Python
- Lists in Python — 1
- Sorting of Lists
- Lists in Python — Insertions
- How to make a list in python and save it in files ……
- Importing Excel Files in Python
- Mailer in Python
- How to add a simple view to a Django site?
- Python Print and I/O questions
- Print answers
- If then else in Python
- Summation of Series
- Python Print Assignment
- Loop Assignments
- Pickle in Python
- Dictionary in Python-1
- Dictionaries in Python-2
- Python OOPs Assignment
- Operator Overloading in Python
- Equation Solving using Numpy
- Starting Numpy
- Passing Data from a Python Controller to a HTML Page
- Downloading Data from a URL in Python
- Sending a Dictionary to a HTML Template and displaying it using Flask.
- Web Scraping Library-1
- Downloading data from a remote website and displaying data in an html page
- Arithmetic Operations in Python — Assignment
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