In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to start web service on python". In daily operation, I believe many people have doubts about how to start web service on python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to start web service on python". Next, please follow the editor to study!
Start the web service
Initialize the web service object app = Flask (_ _ name__) using Flask after importing the Flask module in flask
This app is the object of the web service. Then you can start the web server by calling the app.run () method. It's as simple as that.
Start log as follows
/ home/yukiti2007/anaconda3/envs/tf2/bin/python / home/yukiti2007/IdeaProjects/sample/python/flask/router.py * Serving Flask app "router" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
You can see that flask starts the web service on port 5000 by default
As can be seen from the warnings in log, this startup mode is only used in the development phase and is not recommended in a production environment.
If it is a production environment, it is recommended to use WSGI server to start the service
The app.run () method has five parameters
Parameter name meaning default value: host ip address 127.0.0.1 (localhost) portweb service startup port 5000debug whether to start Noneload_dotenv in debug mode whether to load environment variables, other options for files ending in .env and .flaskenv are loaded by default (because the underlying flask is implemented through Werkzeug, these options are passed to Werkzeug. For more information, you need to see the werkzeug.serving.run_simple method)-
If you do not want to start the service on port 5000, just add the parameter prot=8080 to start the service on port 8080
In addition, since the default listening ip address is 127.0.0.1, the started service can only be accessed locally.
If you need to be accessible on other machines, you need to set the listening port to 0.0.0.0
Start the service app.run (host= "0.0.0.0", port=8080,debug=True) with the following parameters, and start log as follows
/ home/yukiti2007/anaconda3/envs/tf2/bin/python / home/yukiti2007/IdeaProjects/sample/python/flask/router.py * Serving Flask app "router" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 249,050,533 add routes
There are two ways to add a route to flask
Use the decorator route ()
Use the function add_url_rule ()
Let's try adding two routes in two ways.
# add decorator @ app.route ("/ hello1") def hello_world1 (): return "hello_world1" # add def hello_world2 (): return "hello world2" app.add_url_rule ("/ hello2", view_func=hello_world2) using the function
The access was successful, and the access log was automatically printed.
192.168.140.1-[08/Aug/2019 16:23:30] "GET / hello2 HTTP/1.1" 200-192.168.140.1-[08/Aug/2019 16:23:39] "GET / hello1 HTTP/1.1" 200-
However, the route added in this way only supports requests in GET mode, and the response will be rejected if accessed through POST mode.
192.168.140.1-[08/Aug/2019 16:33:26] "POST / hello1 HTTP/1.1" 405-192.168.140.1-[08/Aug/2019 16:33:44] "POST / hello2 HTTP/1.1" 405-
You can specify the Method that responds to the request by adding method.
# decorator mode @ app.route ("/ hello1", methods= ["GET", "POST"]) # use function mode app.add_url_rule ("/ hello2", view_func=hello_world2, methods= ["GET", "POST"])
After restarting the service, try to access it in POST mode again, and the response is successful.
192.168.140.1-[08/Aug/2019 16:40:29] "POST / hello1 HTTP/1.1" 200-192.168.140.1-[08/Aug/2019 16:40:50] "POST / hello2 HTTP/1.1" 200-
The complete code has been uploaded to github
At this point, the study on "how to start web services on python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.