Flask lets you do web development in Python. Python can run the web server that returns HTTP responses.
So what is Flask? Flask is a micro framework. A Python module that assists you in building your web app.
The Code
First install Flask with pip,
pip install flask
We'll import the module in our Python script:
#!/usr/bin/python3
# -*- coding: utf-8 -*
from flask import Flask
from flask import request
from flask import make_response
Then run the program below
#!/usr/bin/python3
# -*- coding: utf-8 -*
from flask import Flask
from flask import request
from flask import make_response
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/browser')
def index():
user_agent = request.headers.get('User-Agent')
return '<p>Your browser is %s</p>' % user_agent
if __name__ == '__main__':
app.run()
In your browser you'll be able to open http://127.0.0.1:5000/ and http://127.0.0.1:5000/browser.
The method @app.route() links a web browser url to a Python function. The Python function can do anything you want.
The response can return both plain text and html responses. The call app.run() starts the server.
Related links:
Top comments (0)