在Android上写Python:安装和自动配置完成后的应用

[复制链接]
查看1170 | 回复0 | 2022-11-21 08:05:50 | 显示全部楼层 |阅读模式
<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ pkg install root</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">repo</span></code></section></li></ol></pre></p>
执行更新,使所有安装的软件达到最新状态。

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ pkg update</span></code></section></li></ol></pre></p>
最后,安装 Python:

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ pkg install python</span></code></section></li></ol></pre></p>
安装和自动配置完成后,就可以构建你的应用了。

在 Android 上构建一个 Android 应用

现在你已经安装了一个终端,你可以在很大程度上像使用另一台 Linux 电脑一样使用你的 Android 手机。这很好地展示了终端到底有多强大。

首先创建一个项目目录:

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">mkdir</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Source</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">cd</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Source</span></code></section></li></ol></pre></p>
接下来,创建一个 Python 虚拟环境。这是 Python 开发者的常见做法,它有助于让你的 Python 项目独立于你的开发系统(在本例中是你的手机)。在你的虚拟环境中,你将能够安装特定于你应用的 Python 模块。

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ python </span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">m venv venv</span></code></section></li></ol></pre></p>
激活你的新虚拟环境(注意,开头的两个点用空格隔开)

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">.</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">./</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">venv</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">/</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">bin</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">/</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">activate</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">env</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$</span></code></section></li></ol></pre></p>
请注意你的 shell 提示符现在以(env)开头,表示你在虚拟环境中。

现在使用pip安装 Flask Python 模块。

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">env</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> $ pip install flask</span></code></section></li></ol></pre></p>
在 Android 上写 Python 代码

你已经准备好了。现在你需要为你的应用编写代码。

要做到这一点,你需要有经典文本编辑器的经验。我使用的是vi。如果你不熟悉vi,请安装并试用vimtutor,它(如其名称所暗示的)可以教你如何使用这个编辑器。如果你有其他你喜欢的编辑器,如jove、jed、joe或emacs,你可以安装并使用其中一个。



现在,由于这个演示程序非常简单,你也可以直接使用 shell 的 heredoc 功能,它允许你直接在提示符中输入文本。

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">env</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">cat</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"><<</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> EOF </span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">>></span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hello_world</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">.</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">py</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">></span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">from</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> flask </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">import</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Flask</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">></span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> app </span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">=</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Flask</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">__name__</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">></span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">></span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(51, 135, 204);">@app</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">.</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">route</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'/'</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">></span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">def</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hello_world</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">():</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">></span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">     </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">return</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'Hello, World!'</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">></span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> EOF</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">env</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$</span></code></section></li></ol></pre></p>
这只有六行代码,但有了它,你可以导入 Flask,创建一个应用,并将传入流量路由到名为hello_world的函数。

现在你已经准备好了网页服务器的代码。现在是时候设置一些环境变量,并在你的手机上启动一个网页服务器了。

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">env</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> $ </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">export</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> FLASK_APP</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">=</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">hello_world</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">.</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">py</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">env</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> $ </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">export</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> FLASK_ENV</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">=</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">development</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">evn</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> $ python hello_world</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">.</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">py</span></code></section></li></ol></pre></p>
启动应用后,你会看到这条消息:

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">serving </span><span style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Flask</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> app</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">…</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> running on http</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span style="overflow-wrap: break-word;color: rgb(174, 174, 174);font-style: italic;">//127.0.0.1:5000/</span></code></section></li></ol></pre></p>
这表明你现在在 localhost(也就是你的设备)上运行着一个微型网页服务器。该服务器正在监听来自 5000 端口的请求。

打开你的手机浏览器并进入到:5000,查看你的网页应用。

你并没有损害手机的安全性。你只运行了一个本地服务器,这意味着你的手机不接受来自外部世界的请求。只有你可以访问你的 Flask 服务器。

为了让别人看到你的服务器,你可以在run命令中加入--host=0.0.0.0来禁用 Flask 的调试模式。这会打开你的手机上的端口,所以要谨慎使用。

<p><pre style="overflow-wrap: break-word;background-color: rgb(22, 27, 32);background-image: none;font-size: 12px;box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 5px;text-shadow: rgb(0, 0, 0) 0px 1px 1px;border-radius: 6px;color: rgb(184, 255, 184);margin: 10px;padding-top: 1em;padding-right: 1em;padding-bottom: 1em;white-space: pre-wrap;border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);line-height: 1.2em;overflow: auto;letter-spacing: 0px !important;">    <ol class="list-paddingleft-2" style="overflow-wrap: break-word;margin: 1em 1em 1em 3em;padding: 1px;border-left: 2px solid rgb(0, 153, 0);background-image: none;color: rgb(174, 174, 174);line-height: 1.2em;"><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">env</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> $ </span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">export</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> FLASK_ENV</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">=””</span></code></section></li><li style="overflow-wrap: break-word;padding-left: 28px;text-indent: -28px;line-height: 1.2em;list-style-type: decimal;"><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span style="overflow-wrap: break-word;color: rgb(226, 137, 100);">env</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">)</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> $ flask run </span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">–</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">host</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">=</span><span style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span style="overflow-wrap: break-word;color: rgb(184, 255, 184);">.</span><span style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span></code></section></li></ol></pre></p>
按Ctrl+C停止服务器(使用特殊的Termux键来作为Ctrl键)。

决定下一步怎么做

你的手机可能不是一个严肃的网页应用的理想服务器平台,但这个例子证明了可能性是无限的。你可能会在 Android 手机上编程,只是因为这是一种方便的实践方式,或者因为你有一个令人兴奋的本地化网页应用的新想法,或者你只是碰巧使用 Flask 应用来完成自己的日常任务。正如爱因斯坦曾经说过的“想象力比知识更重要”,对于任何一个新手编码者,或者一个经验丰富的 Linux 或 Android 爱好者来说,这是一个有趣的小项目。它可以扩展到无穷的层次,所以让你的好奇心接手,并做出一些令人兴奋的东西!

via:

作者:Phani Adabala选题:lujun9972译者:geekpi校对:wxy

本文由LCTT原创编译,Linux中国荣誉推出
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则