Flask Service setup

The project address

The project address

The code will be displayed here, if you like, you can help point a star, thank you! If you like this tutorial, you can share it with others.

About the selection of

After a long time of thought, I was going to use ** “Gin” as the back-end service, or as the gateway layer, but on second thought, it seems not necessary. The platform will have a limited user base and some “service unbundling” will take place. Flask, as some of you might say, why don’t you use Django?

I’ve never had a chance to use Django. I’ve been using Flask since I started Python Web development. The good news is that “Flask” is relatively simple, and Django users can get started quickly.

Environment to prepare

I am actually curious, do not know whether you want to see a very complete process, or a general, so it may be more casual ha, try to complete complex places, after all, writing articles and writing code, or more trouble. Early may speak more carefully, later may be based on code. So if you have any questions, please comment below or contact me.

Preliminary knowledge

  1. Familiar with PIP usage

  2. Familiar with Python syntax

  3. Be familiar with the use of Pycharm

Tools or software are available

  • IDE: Pycharm

  • Python3.4 above

    The best is above 3.4, the author here is more casual, using version 3.7, there is no big difference.

The simplest example

The author’s current directory is J:\projects\github.com\wuranxu

The future code will be based on this directory, for reference only.

Create the project and open it through Pycharm

Install the Flask package

Open the terminal in the current directory (pity) and type:

pip3 install flask

Copy the code

If the installation process is slow, you can add douban source:

pip3 install flask -i https://pypi.douban.com/simple

Copy the code

Since the author has installed, so there is no detailed installation process.

Initialize the app

Establish a pity/app/set py

! [images] (https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e302ea85cdbd426eb468d9023d534d38~tplv-k3u1fbpfcp-zoom-1.image)

Edit set py

from flask import Flask

pity = Flask(__name__)

Copy the code

This is the ** “convention” for flask **, which introduces the Flask class and instantiates a flask object, where __name__ is written in plain English.

At this point, we have one such Flask** “instance” ** called “pity”.

Write a Web service file

Write a pity/run. Py

from app import pity @pity.route('/') def hello_world(): return 'Hello World! 'if __name__ = = "__main__" : pity. Run (" 0.0.0.0 ", implementing = True, the port = "7777")Copy the code

Where @pity. Route (“/”) is a ** “decorator” **, which means that hello_world is bound to route /, meaning that hello_world is executed automatically when/is accessed.

Thread.run (“0.0.0.0″, threaded=True, port=”7777”) 0.0.0.0 indicates whether the port is threaded. If the port is threaded, the port is not blocked. Port represents the port on which the service is mounted. Here we use ** “clearlove” ** as the port number: 7777.

Give it a try!

Run run.py, either from Pycharm or from the terminal by typing python3 run.py.

You can see Running on http://0.0.0.0:7777, indicating that the service is successfully started.

verify

We all know that there are many methods for “HTTP”, so we default to “GET” if no method is specified.

Open your browser and type http://localhost:7777/

If you see this hello World, you are successful!

That’s all for today. See you next time. To tell the truth, write a little tired, the code did not write a few lines, wrote a lot of bullshit, behind may want to speed up!

Full code address: github.com/wuranxu/pit…

“Click Star QAQ if you think it will help.”

This article uses the article synchronization assistant to synchronize