Use Python + flask to develop blog projects and achieve intranet penetration

Time:2022-5-3

preface

Flask is a lightweight web framework written in Python. Compared with other frameworks of the same type, this framework is more flexible and lightweight. And it has strong customization, users can add functions according to their own needs, and has a powerful plug-in library, which is why this framework has been hot in the field of Python. In this article, we will use this framework to write our own blog website! And teach you how to handle projects by using intranet penetration tools, so that local projects can be accessed on the public network!

First, let’s take a look at some demonstrations of our blog web project

Use Python + flask to develop blog projects and achieve intranet penetration

Use Python + flask to develop blog projects and achieve intranet penetration

The project mainly includes the following functions: personal registration in login, home page article display, article details display, article publishing module, article addition classification module, article classification management module, user information management module and program startup module.

Let’s write the code of each module

 

1. Personal registration and login module

The main purpose of this module is to allow users to register users, then judge whether the entered passwords are consistent, and write the results to the database.

Use Python + flask to develop blog projects and achieve intranet penetration

from flask import render_ template, redirect, url_ for, request, flash, session

 

2. Homepage article display module

This module combines the paging function. When the number of published articles cannot be displayed completely on the current page, it will be displayed by adding paging, and written through the paging navigation bar provided by bootstrap. Using bootstrap to develop front-end pages can enable us to develop exquisite pages without good front-end knowledge.

Use Python + flask to develop blog projects and achieve intranet penetration

@front. route('/index')

 

3. Article details display module

This module is mainly to get the full time of the article model and display it, and accurately display it through the ID of the article

Use Python + flask to develop blog projects and achieve intranet penetration

@front. route('/article_detail/<int:article_id>')

 

4. Article publishing module

This module introduces the MD format article writing module. Users can write and publish articles according to the MD syntax. At the same time, the syntax preview box on the right is added to facilitate users to see the display of articles

Use Python + flask to develop blog projects and achieve intranet penetration

@front. route('/add_article',methods=['GET','POST'])

 

5. Add a classification module to the article

In this module, the user can input the name of the group to be added, then select the color to be displayed, and store the relevant data in the database

Use Python + flask to develop blog projects and achieve intranet penetration

@front. route("/add_group",methods=['GET','POST'])

 

6. Article classification management module

This module is mainly used to edit and delete the groups and colors edited by users

Use Python + flask to develop blog projects and achieve intranet penetration

@front. route("/article_group_manage")

 

7. Article management module

This module provides the management function of articles. If users have ideas for continuous improvement of published articles, they can edit them in this module and delete or edit and republish them.

Use Python + flask to develop blog projects and achieve intranet penetration

@front. route('/edit_article/<int:article_id>',methods=['GET','POST'])

 

8. User personal information logout module

Through this module, users can log off the current login account with one click and return to the login page

#Log off, log in and write in the base

 

9. Information management module

The information of the above modules needs to be stored in the MySQL database. First create a Blog Library in the local MySQL database, and then execute the web project. This module will map and create the database table information with the database table, and assign the corresponding type. The implementation can avoid the complicated operation of manually creating data tables.

login_ manager = LoginManager()

 

10. Program startup module

In this module, in order to make the startup entry of flash more concise, I encapsulate the code of the startup module.

def create_ app():

Application main class

Use Python + flask to develop blog projects and achieve intranet penetration

from current import create_app

app=create_app()
if __name__ == '__main__':
  app.run(host='0.0.0.0',debug=True,port=83)

Here, the relevant modules of our blog project have been written. In fact, there are still many functions that can be added by small partners according to their own needs. This is also a very characteristic place of flask. The point to be explained here is that there are too many codes in the front-end part, so they are not displayed here. The partners in need can chat privately and I get the overall code.

 

11. Intranet penetration module

This module may be unfamiliar to many small partners. Here is an example to facilitate their understanding.

If we want to deploy the project written above and make it available to all people, it is easy to think of using the server. However, the cost of the server is high and the deployment process is complicated. Is there any good way to help us map the local service ID to the public network access? The answer is intranet penetration

Here is a quick intranet penetration tool, peanut shell. You can search and download directly.

After downloading, we just need to input our local IP, and he will help us generate a domain name to make the public network accessible, as shown in the figure below.

Use Python + flask to develop blog projects and achieve intranet penetration

Then go to the browser to test whether it can be accessed. If it can be accessed successfully, it means that the mapping is successful:

Use Python + flask to develop blog projects and achieve intranet penetration

 

12. Summary

We hope that you can search more articles related to the development of flapper on the python intranet and continue to use flapper to search for more articles related to the development of flapper in the future!