如何使用Raspberry Pi制作自己的Python开发服务器

by Karan Asher

由Karan Asher

如何使用Raspberry Pi制作自己的Python开发服务器 (How to make your own Python dev-server with Raspberry Pi)

In simple terms, Raspberry Pi is a super cheap ($40) Linux based computer. That’s it. Seriously.

简单来说,Raspberry Pi是基于Linux的超级便宜(40美元)的计算机。 而已。 说真的

It can do whatever you can imagine a normal Linux computer can do, such as browse the web, write code, edit documents, and connect to I/O devices such a thumb drive, mouse, keyboard, etc. This tutorial will be focused on learning how to make your own Python dev-server with Raspberry Pi.

它可以完成普通Linux计算机可以想象的一切,例如浏览Web,编写代码,编辑文档以及连接到I / O设备(例如拇指驱动器,鼠标,键盘等)。本教程将重点介绍学习如何使用Raspberry Pi制作自己的Python开发服务器。

步骤0。定义目标 (Step 0. Define the goal)

Before we begin, it is important to understand what is it that we are trying to build. By the end of the tutorial, you will be able to run a basic website (using Flask) off of a Raspberry Pi on your local home network.

在开始之前,重要的是要了解我们要构建的内容。 在教程结束之前,您将能够在本地家庭网络上的Raspberry Pi上运行基本网站(使用Flask )。

The goal of this tutorial is to demonstrate how a Pi can be used as a dev-server, more specifically, the example will be to host a simple website (using Flask).

本教程的目的是演示如何将Pi用作开发服务器,更具体地说,示例将是托管一个简单的网站(使用Flask )。

步骤1.陈述假设 (Step 1. State the assumptions)

Here are some assumptions that this tutorial will make:

这是本教程将做的一些假设:

  1. You already have a Raspberry Pi set up with Raspbian OS. Here is a useful setup guide if you need one.

    您已经具有使用Raspbian OS设置的Raspberry Pi。 如果需要的话, 是一个有用的设置指南。

  2. The Pi is connected to your home WiFi (and that you know the Pi’s IP address).

    Pi已连接到您的家庭WiFi(并且您知道Pi的IP地址)。
  3. You will not require a screen going forward. assuming points 1 and 2 are complete.

    您不需要前进的屏幕。 假设第1点和第2点是完整的。

We will use VS Code with the Remote VSCode extension to remotely create and edit files on the Pi. I definitely recommend that you use these two to follow along. Also, these will make working with remote files a lot easier, so that’s a plus.

我们将使用带有Remote VSCode扩展名的VS Code来在Pi上远程创建和编辑文件。 我绝对建议您使用这两个步骤。 而且,这些将使处理远程文件变得更加容易,这是一个加号。

步骤2.找到Pi的IP地址 (Step 2. Find the Pi’s IP address)

First, connect the Pi to a power supply, and ensure that it is correctly booted up and connected to the WiFi/Ethernet (basically, it needs to have an internet connection).

首先,将Pi连接到电源,并确保其已正确启动并连接到WiFi /以太网(基本上,它需要连接互联网)。

We will use ssh to connect to and communicate with the Pi. To do that remotely using a laptop, you need to know its IP address. This can be easily obtained using your ISP’s admin portal (usually available at http://192.168.0.1. Please note that this could be different for different ISPs.)

我们将使用ssh连接到Pi并与之通信。 要使用笔记本电脑进行远程操作,您需要知道其IP地址。 可以使用您的ISP的管理门户轻松获得它(通常可从http://192.168.0.1获得 。请注意,对于不同的ISP,这可能会有所不同。)

Usually, you should have your Pi connected to an address which may look similar to ‘192.168.0.12.’ Again, this will be different for different people. So please use the IP address that you found for your Pi in the admin portal. Going forward, this tutorial will use 192.168.0.12 as the Pi’s IP address.

通常,您应该将Pi连接到看起来类似于“ 192.168.0.12”的地址。 同样,这对于不同的人将是不同的。 因此,请使用您在管理门户中为您的Pi找到的IP地址。 展望未来,本教程将使用192.168.0.12作为Pi的IP地址。

步骤3.使用ssh连接到Pi (Step 3. Connect to the Pi using ssh)

Open VS Code and its built-in terminal window on your laptop. Connect to the Pi with an IP address of 192.188.0.12 using the following ssh command:

在笔记本电脑上打开“ VS Code”及其内置的终端窗口。 使用以下ssh命令以IP地址192.188.0.12连接到Pi:

ssh -R 52698:localhost:52698 pi@192.168.0.12

ssh -R 52698:localhost:52698 pi@192.168.0.12

The above command will set up a 2-way communication channel between your laptop and the Pi. If this is the first time you are connecting to the Pi, use raspberry as the password. You may be prompted to change your default password. It is highly recommended that you do so.

上面的命令将在您的笔记本电脑和Pi之间建立2通通讯通道。 如果这是您第一次连接到Pi,请使用树莓派作为密码。 系统可能会提示您更改默认密码。 强烈建议您这样做。

步骤4.创建一个项目目录 (Step 4. Create a project directory)

You should now be in the home directory of the Pi. Let’s create a directory for the website we wish to build. Use the following command to create the directory:

您现在应该在Pi的主目录中。 让我们为要构建的网站创建目录。 使用以下命令创建目录:

mkdir MyFlaskWebsite

mkdir MyFlask网站

Use the ‘ls’ command to verify that you can indeed see a new folder named MyFlaskWebsite.

使用“ ls”命令验证您确实可以看到名为MyFlaskWebsite的新文件夹。

步骤5.安装烧瓶 (Step 5. Install Flask)

We will use Flask to create a simple website. Flask is a Python based micro web framework. It uses Jinja (Python based template engine) as its template engine which makes it very usable and powerful. Use the following command to install flask on the Pi:

我们将使用Flask创建一个简单的网站。 Flask是一个基于Python的微型Web框架。 它使用Jinja (基于Python的模板引擎)作为其模板引擎,这使其非常实用且强大。 使用以下命令在Pi上安装flask:

sudo apt-get install python3-flask

须藤apt-get install python3-flask

步骤6.编写一些基本代码 (Step 6. Write some basic code)

Now that Flask is installed, we can start creating files and writing some code. First, navigate to your newly created project directory (from step 4) by using the following command:

现在已经安装了Flask,我们可以开始创建文件并编写一些代码。 首先,使用以下命令导航到新创建的项目目录(从步骤4开始):

cd MyFlaskWebsite

cd MyFlask网站

All project files and folders will reside inside this ‘MyFlaskWebsite’ directory. Now, create your first code file (app.py) using the following command:

所有项目文件和文件夹都将位于“ MyFlaskWebsite”目录中。 现在,使用以下命令创建您的第一个代码文件(app.py):

touch app.py

触摸app.py

On checking the directory using the ‘ls’ command, you should be able to see this newly created file.

使用“ ls”命令检查目录时,您应该能够看到这个新创建的文件。

Now, press F1 and choose ‘Remote Start Server.’ This should allow you to remotely edit files on the Pi using your laptop.

现在,按F1并选择“远程启动服务器”。 这应该可以让您使用笔记本电脑在Pi上远程编辑文件。

Next, use the following command to start editing the newly created app.py file. It might take a few seconds but the empty file should then be visible in the window right above.

接下来,使用以下命令开始编辑新创建的app.py文件。 可能需要花费几秒钟的时间,但是空文件应该在右上角的窗口中可见。

rmate app.py

好友app.py

Type out the code shown in the below picture. Here, we have defined a route to the home page of the website which should display ‘This is my flask website and it is so cool.’ Note that setting the host to 0.0.0.0 allows this website to be accessible by all devices connected to the same network.

输入下图所示的代码。 在这里,我们定义了一个指向网站主页的路径,该路径应显示“这是我的烧瓶网站,非常酷”。 请注意,将主机设置为0.0.0.0允许连接到同一网络的所有设备访问此网站。

Save the file and use the following command to run the website on the Pi server:

保存文件并使用以下命令在Pi服务器上运行网站:

python3 app.py

python3 app.py

On receiving the above success message, open a new browser window on any device within your network and type out the Pi’s IP address (in this case, it is 192.168.0.12) followed by the port the dev-server is running on (5000.) So the complete address will be http://192.168.0.12:5000/

收到上述成功消息后,请在网络中的任何设备上打开一个新的浏览器窗口,然后键入Pi的IP地址(在本例中为192.168.0.12),然后输入运行dev-server的端口(5000)。 ),因此完整地址为http://192.168.0.12:5000/

You should see the text ‘This is my flask website and it is so cool.’ on the webpage.

您应该看到文字“这是我的烧瓶网站,非常酷。” 在网页上。

This confirms that your dev-server is active and is running the website you just created.

这确认您的开发服务器处于活动状态并且正在运行您刚创建的网站。

步骤7.添加更多路线 (Step 7. Add more routes)

Currently, the code consists of only 1 route which is the home page of the website. Add another route by typing out the following code. Note that you can dynamically make changes while the dev-server is running. It will automatically capture the delta (code change) and run a revised version once you refresh your browser window.

当前,该代码仅包含1条路由,这是网站的主页。 通过键入以下代码来添加另一条路线。 请注意,您可以在开发服务器运行时动态进行更改。 刷新浏览器窗口后,它将自动捕获增量(代码更改)并运行修订版。

To check whether or not the new route works as expected, go to http://192.168.0.12:5000/meow and the webpage should ‘MEOW’ at you.

要检查新路线是否按预期工作,请访问http://192.168.0.12:5000/meow ,网页应该会显示“ MEOW”。

步骤8.在代码中添加结构 (Step 8. Add structure to your code)

Now, adding more routes is cool but having the all the code in just 1 app.py file is not how a website should be structured. Usually, we would have a folder with HTML templates, a folder with static CSS files and another one for JS files. Let’s add these folders and move the code in appropriate folders to structure out code better. Use the following commands to create these directories:

现在,添加更多路由很酷,但是将所有代码仅包含在一个app.py文件中,这不是网站的结构。 通常,我们会有一个带有HTML模板的文件夹,一个带有静态CSS文件的文件夹,以及一个用于JS文件的文件夹。 让我们添加这些文件夹并将代码移到适当的文件夹中,以更好地组织代码。 使用以下命令创建这些目录:

mkdir templates

mkdir模板

mkdir static

mkdir静态

Use the ‘ls’ command to verify that these folders have been created.

使用“ ls”命令来验证是否已创建这些文件夹。

Now, let’s create an HTML file for the homepage. Use the following commands to navigate to the templates directory. Then, create a new file named index.html and use rmate to edit the same:

现在,让我们为主页创建一个HTML文件。 使用以下命令导航到模板目录。 然后,创建一个名为index.html的新文件,并使用rmate进行编辑:

cd templates

cd模板

touch index.html

触摸index.html

rmate index.html

好友index.html

Write some basic HTML code for the homepage inside index.html.

在index.html中为主页编写一些基本HTML代码。

Make the following changes in app.py to use the index.html file. The below code will search for a file named index.html in the templates directory by default.

在app.py中进行以下更改以使用index.html文件。 以下代码默认情况下将在模板目录中搜索名为index.html的文件。

Navigate back to the project directory and run the website again.

导航回到项目目录,然后再次运行网站。

Go back to the home page and you should see the content you put inside of index.html.

返回首页,您应该看到放在index.html中的内容。

Now add some styling by creating ‘main.css’ inside the static directory. As always, use the ‘cd’ command to change the directory, ‘touch’ command to create a new file, and ‘rmate’ command to edit the same file.

现在,通过在静态目录内创建“ main.css”来添加一些样式。 与往常一样,使用“ cd”命令更改目录,使用“ touch”命令创建新文件,并使用“ rmate”命令编辑同一文件。

Add some styling to the h4 tag. Note that we currently have 1 h4 tag in index.html which the css is supposed to modify.

在h4标签上添加一些样式。 请注意,我们目前在index.html中有1个h4标签,css应该对此标签进行修改。

As always, test your changes by using the following command:

与往常一样,使用以下命令测试更改:

python3 app.py

python3 app.py

Notice how the text within the h4 tag gets colored according to the CSS.

注意h4标记中的文本如何根据CSS着色。

步骤9.利用Jinja的优势 (Step 9. Take advantage of Jinja)

Jinja in a Python based template engine which adds a lot of powerful features to webpages. Although this tutorial is not focused on learning Jinja, let’s just look at a simple example of how Jinja can be useful.

基于Python的模板引擎中的Jinja ,它为网页添加了许多强大的功能。 尽管本教程的重点不是学习Jinja,但让我们看一个简单的示例,了解Jinja如何有用。

Let’s just create a list of fruits in app.py and pass it as a parameter to index.html. We will then have index.html display that list on the webpage. Make the following changes in app.py and index.html.

让我们在app.py中创建一个水果列表,并将其作为参数传递给index.html。 然后,我们将使用index.html在网页上显示该列表。 在app.py和index.html中进行以下更改。

Refresh your webpage and you should see the list of fruits on screen.

刷新网页,您应该在屏幕上看到水果列表。

This speaks to how powerful and useful Jinja can be. For more info on Jinja, please refer to this.

这说明了Jinja有多么强大和有用。 有关Jinja的更多信息,请参阅

步骤10.后续步骤 (Step 10. Next steps)

Now that you have a fully functional Python dev-server, the possibilities going forward are practically infinite. Here are a few useful next steps that you may consider for your project:

既然您已经拥有了功能齐全的Python开发服务器,那么前进的可能性实际上是无限的。 以下是您可以为项目考虑的一些有用的后续步骤:

  1. Currently, the Pi is accessible only through the devices within your personal network. To expose the Pi to the outside world (access it through any device outside your personal network), you need something known as port forwarding. Basically, you need a domain name and a static IP address which is permanently assigned to the Pi. More info here and here.

    目前,只能通过您个人网络中的设备访问Pi。 要将Pi暴露给外界(通过您的个人网络之外的任何设备进行访问),您需要某种称为端口转发的东西。 基本上,您需要一个永久分配给Pi的域名和一个静态IP地址。 更多信息在这里这里

  2. Most applications will require a database for basic CRUD operations. Python supports SQlite right out of the box. Learn how to use SQlite with Flask here and here.

    大多数应用程序将需要一个数据库来进行基本的CRUD操作。 Python开箱即用地支持SQlite。 在此处此处了解如何将SQlite与Flask一起使用。

  3. Here’s a cool Raspberry Pi starter kit on Amazon. The neat thing about this is that it has everything you need to get started and saves you the effort of searching individual items yourself.

    这是亚马逊上很酷的Raspberry Pi入门套件 。 这样做的好处是,它具有入门所需的一切,并且省去了您自己搜索单个项目的工作。

  4. Since you are not using a screen, it is important that you use the shutdown command for the Pi using the terminal. This ensures that the Pi and the SD card are not damaged:

    由于您没有使用屏幕,因此使用终端对Pi使用shutdown命令非常重要。 这样可以确保Pi和SD卡没有损坏:

sudo shutdown -h now

sudo shutdown -h现在

#UntilNextTime.

#直到下一次。

翻译自: https://www.freecodecamp.org/news/how-to-make-your-own-python-dev-server-with-raspberry-pi-37651156379f/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值