Python Web Services Learning--flask

Source: Internet
Author: User

Tag: Start LAN PoC effect pip install PIP highlight Program Res

Flask is a lightweight WEB application framework written using Python. is also one of the most common frameworks for developing Python Web services.

The first step, of course, is to install flask, which is still PIP installation:

Learning flask can start with the quick start of the website (http://flask.pocoo.org/docs/0.10/quickstart/#hooking-in-wsgi-middlewares), there are many small examples, Let's look at one of the simplest

From flask Import Flaskapp = Flask (__name__) @app. Route ('/') def hello_world ():    return ' Hello world! ' if __name__ = = ' __main__ ':    app.run (' 172.31.11.141 ', 8080)

This is an adorner implementation of the route function, the table name URL path to "/" Request by Hello_world () processing, and the Hello_world () function simply returned a string. To run the program:

You can also use angle brackets to define variables in the routing path, for example:

@app. Route ('/user/<username> ') def show_user_profile (username):    # Show the user profiles for that user    Return ' User%s '% username

The effect is as follows:

Of course, flask can also support the processing of static pages, using the first set of static pages of the directory, and then use the App.send_static_file () function to return the static page, the code is as follows:

#-*-Coding:utf-8-*-

From flask import Flaskimport Osapp = Flask (__name__) #设置静态页面的绝对路径app. _static_folder = Os.path.abspath ("/home/yu/flask/ ") @app. Route ('/') def hello_world (): #返回静态页面 return app.send_static_file (' hello.py ')

Python Web Services Learning--flask

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.