Lightweight web development framework: Flask local deployment and implementation of public network access interface

Lightweight web development framework: Flask local deployment and implementation of public network access interface

Preface

This article explains how to install Flask locally, and how to publish its web interface to the public network and access it remotely.

Flask is a very popular web framework that uses the Python programming language to implement related functions. It is more flexible, lighter, safer and easier to use than other frameworks of the same type. It can be well combined with the MVC model for development. Developers work together, and small teams can complete the implementation of feature-rich small and medium-sized websites or Web services in a short time.

In addition, Flask is highly customizable. Users can add corresponding functions according to their own needs, enriching and expanding functions while keeping the core functions simple. Its powerful plug-in library allows users to implement personalized websites. Customize and develop powerful websites.

1. Install and deploy Flask

The code in this article is run using Python3

Installation environment: Python3 and pip3 need to be installed on the computer. It is recommended to install the latest version

Check if there is available Python with pip installed, view the Python and pip versions:

C:> py --version
Python 3.N.N
C:> py -m pip --version
pip X.Y.Z from ... (python 3.N.N)

Install and update using pip:

$ pip install -U Flask

Create a new file under FLASK in VS Code with the nameapp.py

Paste the following command and save

# save this as app.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"

image-20231120164830537

Enter in TERMINAL belowlsView directory location

image-20231120165319864

Open a new command

Enter cd space plus the directory of vs code just now, for example

cd C:\Users\wang\dev\python\flask

EnterlsView directory

image-20231120165938767

Start Flask

flask run

Open a new browser and enter http://127.0.0.1:5000/ to enter the HelloWorld web interface.

image-20231120170348720

2. Install Cpolar intranet penetration

The above has successfully deployed Flask locally and successfully accessed the LAN. Next, we install the Cpolar intranet penetration tool and forward the http public network address mapped by the local port through cpolar. We can easily achieve remote access without having to register a domain name and purchase a cloud. Server. Here are the installation steps:

cpolar official website address: https://www.cpolar.com

  • Use one-click script installation command
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
  • Add services to the system
sudo systemctl enable cpolar
  • Start cpolar service
sudo systemctl start cpolar

After cpolar is successfully installed, access port 9200 on an external browser: [http://LAN ip:9200], log in with your cpolar account (if you don’t have an account, you can click below to register for free), and you can see cpolar web after logging in Configuration interface, just configure it in the web management interface

image-20230831171159175

3. Configure the public network access address of Flask’s web interface

Click Tunnel Management - Create Tunnel on the left dashboard to create a Flask cpolar public network address tunnel:

  • Tunnel name: The name can be customized. Be careful not to duplicate the existing tunnel name.
  • Protocol: Select http
  • Local address: 5000 (port for LAN access)
  • Domain name type: Choose a random domain name for free
  • Region: Select China Top
  • Click创建

image-20231120171035750

After the tunnel is successfully created, click the status on the left - online tunnel list to view the generated public network access address. There are two access methods, one is http and https

image-20231120171152780

4. Remotely access Flask’s web interface from the public network

Use the above cpolar https public network address to access it in the browser of any device, and you can successfully see the Flask web interface. In this way, a public network address that can be accessed remotely is created. There is no need to purchase a cloud server yourself, and it can be published to Public network access.

image-20231120171325137

Since the tunnel created using cpolar above uses a random public network address, it will change randomly within 24 hours, which is not conducive to long-term remote access.

I usually use a fixed second-level subdomain name because I hope to send the URL to developers for division of labor, so that small teams can use fixed public addresses to complete the implementation of feature-rich small and medium-sized websites or web services in a short period of time. It is a fixed and easy-to-remember public network address (for example: Flask.cpolar.cn), so we can configure a second-level subdomain name for it. This address is a fixed address and will not change randomly [ps: cpolar.cn has been filed]

Note that you need to upgrade the cpolar package to a basic package or above, and the bandwidth corresponding to each package is different. [cpolar.cn has been registered]

Log in to the cpolar official website, click Reserve on the left, select to reserve the second-level subdomain name, set a second-level subdomain name, click Reserve, and copy the reserved second-level subdomain name after the reservation is successful. Level subdomain name

image-20231120171616938

After the reservation is successful, copy the name of the second-level subdomain name that was successfully reserved.

image-20231120171639329

Return to the cpolar web UI management interface, click Tunnel Management - Tunnel List on the left dashboard, find the tunnel you want to configure, and click Edit on the right

image-20231120171707486

Modify the tunnel information and configure the successfully reserved second-level subdomain name into the tunnel.

  • Domain name type: Select a second-level subdomain name
  • Sub Domain: Fill in the successfully reserved second-level subdomain name

Click更新(Note, click once to update, no need to submit again)

image-20231120171747421

After the update is completed, open the online tunnel list. At this time, you can see that the public network address has changed and the address name has become a fixed second-level subdomain name.

image-20231120174238360

Finally, we use a fixed public network address to access, and we can see that the access is successful. In this way, a fixed and permanent public network address is set.

image-20231120174301657

Guess you like

Origin blog.csdn.net/xz2935117143/article/details/134582538