App = Flask (__name__) What's a thing?

Source: Internet
Author: User

#!/usr/local/bin/python
# Coding=utf-8
 
From flask Import Flask
app = Flask (__name__)
 
@app. Route ('/')
def hello_world ():
return ' Hello world! '
 
if __name__ = = ' __main__ ':
app.run (host= ' 0.0.0.0 ', port=9000)
" "
Line 4th, introducing the Flask class, flask class implements a WSGI application
Line 5th, the app is an instance of flask, which receives the name of the package or module as a parameter, but is generally passed __name__.
let the Flask.helpers.get_root_path function determine the root of the program by passing in the name to obtain a directory of static files and template files.
Line 7th to 9th, using the App.route adorner saves the relationship of the URL and the executing view function to the App.url_map property.
The program that handles the relationship between the URL and the view function is the route, where the view function is Hello_world.
line 11th, use this to ensure that when other files refer to this file (e.g. "From Hello Import app") will not execute the code within this judgment, that is, do not execute the App.run function.
in line 12th, the service can be started by executing app.run. The default flask only listens to the local 127.0.0.1 of the virtual machine, and the port is 5000.
and we do port to the virtual machine is 9000, so we need to set the host and port parameters, 0.0.0.0 means to listen to all addresses, so that you can access the local.
when the server starts, it calls Werkzeug.serving.run_simple to poll, which uses a single-process single-threaded werkzeug.serving.BaseWSGIServer to process the request .
in fact, using the standard library basehttpserver.httpserver, the "while TRUE" event polling is done by Select.select for 0.5 seconds.
when we visit "http://127.0.0.1:9000/", through App.url_map find the registered "/" This URL pattern, found the corresponding Hello_world function execution, return "Hello world!", the status code is 200.
if access to a nonexistent path, such as access to "http://127.0.0.1:9000/a", flask cannot find the corresponding mode, will return to the browser "not Found", the status code is 404
" "

App = Flask (__name__) What's a thing?

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.