In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about raspberry pie how to use Flask to create a basic Python Web server, the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Create a basic Python Web server using Flask
Flask is a Python-based micro-framework for creating web pages. It can be used to render a Web-based interface on Pi and is relatively easy to set up. Creating a dashboard is useful, and I first encountered it when looking for a way to create my paddle pool control panel.
This tutorial will show you how to create a basic site for you to get started. Once you have an example, the official documentation of the work can be used to move to a more advanced topic.
I use Python 3 in this tutorial. It should be the same as Python 2, but you will need to replace all references to "python3" with "python".
Create a new SD card
To avoid conflicts with other software, you may have installed that I will start with a new SD card by writing the latest Raspbian image. I wrote Raspberry Pi images in Etcher, and for the initial experiment with Flask, I used Jessie Lite images from the official download page.
Enable SSH
By default, SSH is disabled. If you want to configure Pi from other computers over the network, you can enable it in the following ways:
Create a blank file named "ssh" in the boot partition (in Windows, this is the only accessible partition)
Use the raspi-config utility to enable SSH with monitors and keyboards connected to the Pi
"for more information, see enable SSH on the Pi guide."
Find the IP address
Find the IP address of your Pi. If you are using a monitor and keyboard, you can run:
Use ifconfig
This is probably in the form of 192.168. At the time of this tutorial, my Pi is using 192.168.1.19.
If you connect remotely through SSH, you can use the IP scanner to find it, otherwise it will be listed somewhere in the router settings.
Update and change password
When SSH is enabled, I strongly recommend that you change the default password from "raspberry"!
Use:
Passwdfile
Set the new password and run:
Sudoraspi-config
Select Advanced, and then select extend File system.
To ensure that we will install the latest package, run the following two commands:
Sudoapt-getupdate
This process may take 5-10 minutes.
Installation point
Before we can install Flask, we need to install the pip,Python package manager:
Sudoapt-get-yinstallpython3-pip
Install flask
Now is the time to install Flask
Sudopip3installflask
I received some errors in the output, but finally reported a "successfully installed flask".
Create a test Flask application
Now that Flask is installed, we need to create a small test site to check that everything is fine. In this tutorial, I will assume that the test site is called "testSite". You can use any name you like, but you will need to exchange all references to "testSite" with your name. Create a new folder:
Cd~
Navigate to the new folder and create a new Python script using the following command:
CdtestSite
Then paste the following code:
Press "CTRL-X", "Y" and "Enter" to save and return to the command prompt.
This script defines a simple one-page site.
You can now run the script using the following command:
Python3testSite.py
If you access the IP address of your Pi in a browser, the test site should be visible:
Note that Flask defaults to port 5000, and you need to replace 192.168.1.19 with the actual IP address of your Pi.
Add additional pages
You can modify the script to add additional "pages". Look at the following example:
It adds an additional "route" called "hello". This page is displayed when you access the helo subdirectory:
More routes.
You can also introduce information from URL into scripts to create finer page combinations. In this example, we added / user / and / post / routes.
.r
Enter: 5000 / user / john or: 5000 / post / 42 and display a page with its name or post ID as part of the content.
Use the template page
Instead of defining HTML pages in a script, you can use template files to accommodate most of the HTML. This makes the script easier to handle when your page is more complex.
Flask looks for templates in the "templates" directory. Create a new directory for the template:
Mkdir/home/pi/testSite/templates
Then paste it in this sample template:
TestSite.py can then update:
NanotestSite.py
Replace the content with:
When two routes are activated, the same template is used, but the values passed to it are different. So visitors see a slightly different page.
You can use HTML and CSS enhanced templates. The advantage of templates is that they focus the main Python scripts on functionality and leave layout and aesthetics to the template files.
Debug mode
In these examples, the "debug" flag is set to True. This runs Flask in debug mode and automatically reloads Flask when you update the script. If the page cannot be loaded, it also provides an error message. If the site is exposed to the Internet, the debug flag should be set to False.
Run the script automatically at startup
If you want Python scripts to run automatically when Pi starts, you can use this technique:
Crontab-e
If prompted to select the editor to use. I prefer to use "nanometer". Insert the following line at the bottom of the comment block:
@ reboot/usr/bin/python3/home/pi/testSite/testSite.py&
Press "CTRL-X", "Y" and "Enter" to save and return to the command prompt.
When you restart, "testSite.py" will be run. "&" make sure it runs in the background.
Sudo raspi-config
Select Boot options and Desktop / CLI. Select console automatic logon. This means that when Pi starts, it will automatically log in as the Pi user.
Restart the use:
Sudo reboot
Your web page should be available on the IP address of Pi on your network.
Download script
The sample scripts and templates in this tutorial are available in my BitBucket repository. You can download them using the following link: testSite1.py testSite2.py testSite3.py testSite4.py template1.py
You can download directly to your Pi to use:
Wget
This is one of the above script URL. Remember to download the file to the correct directory. Templates should be placed in the "templates" directory.
Official documents and other resources
More information about the official page of the Flask documentation.
Also take a look at the Raspberry Pi Foundation-use the Flask tutorial to build a Python Web server.
This is how raspberry pie uses Flask to create a basic Python Web server. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.