Flask

Flask is a simple web framework based on Werkzeug. It's easy to build HTTP GET/POST requests. If you have a scenario where you want to develop a RESTful server, Flask can solve your problem fast. The official website of Flask is http://flask.pocoo.org. You can install Flask using pip. Type this command:

    $ pip install Flask

For testing, we can create a simple-to-handle HTTP GET request. Create a Python file, called ch04_flask.py. Write the following program:

from flask import Flask 
app = Flask(__name__) 
 
@app.route("/") 
def hello(): 
    return "Hello,Flask!" 
 
@app.route('/ads/<int:ads_id>') 
def show_post(ads_id):     
    return 'Adversiting id %d' % ads_id 

This program will handle HTTP GET requests: / and /ads/<id>.

To run the program, you can type this command:

    $ FLASK_APP=ch04_flask.py flask run

It shows a running web server on a specific port, by default 5000. Now you can open a browser and navigate to http://localhost:5000 and try again to navigate to http://localhost:5000/ads/10. You can see the sample browser and program output here:

For further information about Flask development, I recommend you read the Flask documentation at http://flask.pocoo.org/docs/. Some program samples are provided to help you get started.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.102.254