<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ngrok &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/tag/ngrok/feed/" rel="self" type="application/rss+xml" />
	<link>https://greyli.com</link>
	<description>一个编程和写作爱好者的在线记事本</description>
	<lastBuildDate>Thu, 06 Nov 2025 11:36:11 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>

<image>
	<url>https://greyli.com/wp-content/uploads/2025/03/avatar-500-compressed-144x144.jpg</url>
	<title>ngrok &#8211; 李辉 / Grey Li</title>
	<link>https://greyli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>如果你在 macOS 上无法访问 Flask 程序</title>
		<link>https://greyli.com/thank-you-apple/</link>
		<comments>https://greyli.com/thank-you-apple/#respond</comments>
		<pubDate>Sun, 03 Apr 2022 09:37:51 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[Monterey]]></category>
		<category><![CDATA[ngrok]]></category>

		<guid isPermaLink="false">https://greyli.com/?p=4298</guid>
		<description><![CDATA[如果你用 macOS 开发 Flask 时无法正常访问程序，或是使用 ngrok 等内网穿透工具时映射的公网地 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>如果你用 macOS 开发 Flask 时无法正常访问程序，或是使用 <a href="https://greyli.com/use-ngrok-to-expose-your-local-application/">ngrok</a> 等内网穿透工具时映射的公网地址无法访问，大概率是因为 macOS 新版本（Monterey）的变动导致。你会在页面上看到类似下面的错误信息：</p>
<pre>Access to 127.0.0.1 was denied.

You don't have authorization to view this page.

HTTP ERROR 403</pre>
<p>或是在执行 flask run 命令时看到类似下面的报错：</p>
<pre>OSError: [Errno 48] Address already in use</pre>
<p>首先确认你的 macOS 版本是不是 Monterey（左上角 Apple 图标 &#8211; About This Mac）或是之后的版本，如果不是那么问题应该和本文无关。</p>
<p>简单来说，新版本的 macOS 上 localhost 5000 端口被一个叫 AirPlay Receiver 的服务占用了。而 Flask 内置服务器默认就运行在 5000 端口，所以会造成端口冲突。当你通过将 host 设为 0.0.0.0 指定 Flask 的内置服务器对外可见，或是使用内网穿透工具时，会发现程序无法访问（有时未设置对外可见也会遇到这个问题）。</p>
<p>最简单的解决方法是关掉这个服务：</p>
<ul>
<li>系统设置（System Preferences） &gt; 分享（Sharing） &gt; AirPlay Receiver &gt; 取消勾选</li>
</ul>
<p>或是更改 Flask 开发服务器默认的端口（比如改成 8000）。在执行 flask run 命令时使用 -p/&#8211;port 选项可以自定义端口：</p>
<pre>$ flask run -p 8000  # 或 flask run --port 8000</pre>
<p>或是在执行 flask run 之前通过环境变量 FLASK_RUN_PORT 设置：</p>
<pre>$ export FLASK_RUN_PORT=8000 # macOS/Linux
&gt; set FLASK_RUN_PORT=8000 # Windows CMD
&gt; $env:FLASK_RUN_PORT=8000 # Powershell</pre>
<p>在 Werkzueg 2.1.0（2022/3/28 发布）版本，如果你执行 flask run &#8211;host=0.0.0.0 时检测到了端口被占用，会直接显示相关的错误提示：</p>
<pre>$ flask run --host=0.0.0.0
Address already in use
Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port.
On macOS, try disabling the 'AirPlay Receiver' service from System Preferences -&gt; Sharing.</pre>
<p>本文源于去年十一月发的<a href="https://twitter.com/greylihui/status/1455392110618857474">一条推文</a>，最近经常碰见有人遇到这个问题，所以转成这篇文章发出来。</p>
<p>画了一幅涂鸦，送给苹果公司：</p>
<p><a href="https://greyli.com/wp-content/uploads/2022/04/apple-eat-port5000.png"><img class="aligncenter size-full wp-image-4310" src="https://greyli.com/wp-content/uploads/2022/04/apple-eat-port5000.png" alt="" width="816" height="566" srcset="https://greyli.com/wp-content/uploads/2022/04/apple-eat-port5000.png 816w, https://greyli.com/wp-content/uploads/2022/04/apple-eat-port5000-150x104.png 150w, https://greyli.com/wp-content/uploads/2022/04/apple-eat-port5000-300x208.png 300w, https://greyli.com/wp-content/uploads/2022/04/apple-eat-port5000-624x433.png 624w" sizes="(max-width: 816px) 100vw, 816px" /></a></p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/thank-you-apple/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用ngrok让你的本地Flask程序外网可访问</title>
		<link>https://greyli.com/ngrok-flask/</link>
		<comments>https://greyli.com/ngrok-flask/#respond</comments>
		<pubDate>Thu, 27 Sep 2018 05:28:19 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[ngrok]]></category>
		<category><![CDATA[服务器]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1947</guid>
		<description><![CDATA[2022/4/3 Update：如果你在 macOS 上映射的 Flask 程序无法访问，可以参考这篇文章解决 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>2022/4/3 Update：如果你在 macOS 上映射的 Flask 程序无法访问，可以参考<a href="https://greyli.com/thank-you-apple/">这篇文章</a>解决。</p>
<p><em>注：本文隶属于<a href="http://helloflask.com/book">《Flask Web开发实战》</a>番外文章系列，文章列表见<a href="http://greyli.com/flask-web-appendix/">《Flask Web开发实战番外文章索引》</a></em><em>。</em></p>
<p>在开发Web程序时，有时候会有这样的需求：</p>
<ul>
<li>让朋友可以访问到你本地运行的程序</li>
<li>在本地测试各类服务（比如Telegram机器人、微信公众号等）</li>
<li>有一台废弃的电脑，你想让它作为一台服务器来运行你的博客程序，或是运行其他简单的程序</li>
</ul>
<h2>外网可见</h2>
<p>要实现上面的需求，我们就需要程序能够“外网可见”。在<a href="http://helloflask.com/book">《Flask Web开发实战》</a>第一章介绍启动程序时，我们提及了“外网可见”的内容。简单来说，当你将Flask服务器监听的主机地址设置为0.0.0.0时，就可以让服务器外网可见。不过，这里有一个前提，那就是你的服务器需要运行在拥有公网IP的主机上，因为我们开发用的电脑通常不会有公网IP，所以这里的外网只能是你的电脑所在的局域网，比如在客厅的电脑可以访问你的笔记本上运行的程序（通过你的笔记本的内网IP）。</p>
<p>事实上，借助内网穿透/映射工具，我们也可以让外网上的朋友访问到运行在你的笔记本上的程序。这些工具会为我们分配一个域名A，你只需要在本地运行程序，并建立映射，那么当其他用户（不仅是你客厅的电脑，还可能是北京的毛毛，或是美国的Peter）访问A网址时，内网穿透工具就会把请求转发到你的笔记本中，取回响应后再返回给用户。具体流程如下所示：</p>
<div id="attachment_1949" style="width: 609px" class="wp-caption alignnone"><a href="http://greyli.com/wp-content/uploads/2018/09/how-ngrok-work.png"><img class="wp-image-1949 size-full" src="http://greyli.com/wp-content/uploads/2018/09/how-ngrok-work.png" alt="" width="599" height="346" srcset="https://greyli.com/wp-content/uploads/2018/09/how-ngrok-work.png 599w, https://greyli.com/wp-content/uploads/2018/09/how-ngrok-work-150x87.png 150w, https://greyli.com/wp-content/uploads/2018/09/how-ngrok-work-300x173.png 300w" sizes="(max-width: 599px) 100vw, 599px" /></a><p class="wp-caption-text">左边是美国的Peter，右边是你</p></div>
<p>在这篇文章，我们会了解如何使用<a href="https://ngrok.com/">ngrok</a>来实现这个目的。本文将会以一个简单的Flask程序作为示例，不过你也可以替换为任意的Web程序，比如使用Django、PHP或是JAVA等语言/框架编写的Web程序。</p>
<h2>运行本地服务器</h2>
<p>我们先来编写一个简单的Flask程序：</p>
<pre class="">from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'It works!'</pre>
<p>将上面的代码保存为app.py，然后打开一个命令行窗口，使用下面的命令运行这个程序：</p>
<pre class="">$ flask run</pre>
<p>如果还没有安装Flask，可以执行<code>pip install flask</code>命令进行安装（你也可以创建一个虚拟环境，推荐使用<a href="http://greyli.com/pipenv-new-python-dependency-management-tool/">Pipenv</a>）。</p>
<p>默认情况下，Flask内置的开发服务器会监听本地机的5000端口，你可以使用127.0.0.1:5000或localhost:5000访问程序。</p>
<h2>安装和配置ngrok</h2>
<p>ngrok支持三大主流操作系统，安装流程比较简单，如下所示：</p>
<ol>
<li>访问<a href="https://ngrok.com/download">https://ngrok.com/download</a></li>
<li>根据操作系统下载对应的压缩包</li>
<li>解压到合适的目录</li>
</ol>
<p>压缩包里包含一个名为ngrok的二进制文件，我们打开一个命令行窗口，切换到这个文件所在的目录。现在可以先执行help命令来测试一下。在Windows下，你可以使用下面的命令：</p>
<pre class="">&gt; .\ngrok help</pre>
<p>或是直接执行：</p>
<pre class="">&gt; ngrok help</pre>
<p>在Linux/macOS下，则可以使用下面的命令：</p>
<pre class="">$ ./ngrok help</pre>
<h2>建立映射/HTTP隧道</h2>
<p>因为我们的Flask程序已经运行在本地机的5000端口，我们只需要启动ngrok服务，输入对应的端口即可建立映射，或者说建立一条HTTP隧道：</p>
<pre class="">$ ./ngrok http 5000</pre>
<p><em>附注 在Windows下可以使用<code>ngrok http 5000</code>命令。</em></p>
<p>输出的信息中包含ngrok为你随机分配的域名：</p>
<pre class="">Session Status online
Account Grey Li (Plan: Free)
Version 2.2.8
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://d15a56b1.ngrok.io -&gt; localhost:5000
Forwarding https://d15a56b1.ngrok.io -&gt; localhost:5000

Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00</pre>
<p>其中的https://d15a56b1.ngrok.io就是为你分配的可以外部访问的网址，所有发向这个网址的请求都会转发到你的本地机5000端口，即localhost:5000或127.0.0.1:5000。这时访问这个网站会看到我们上面程序中定义的输出“It works!”。</p>
<p>当有请求进入后，你可以在这里看到请求列表。另外，你也可以访问<a href="http://127.0.0.1:4040">http://127.0.0.1:4040</a>访问ngrok本地程序提供的Web监控页面。</p>
<h2>注册账户与付费套餐</h2>
<p>在上面的流程里我们并没有介绍注册账户，因为这是可选的。未注册时，你执行ngrok http命令的输出会和上面稍微有些不同，你会看到下面两行：</p>
<pre class="">Session Status online
Session Expires 7 hours, 42 minutes</pre>
<p>这是因为未注册账户每个会话只会维持8小时，过期后你需要重新启动。</p>
<p>注册用户没有这个限制，注册相当简单，这里不再赘述，注册完成后需要执行下面的命令连接本地ngrok程序：</p>
<pre class="">$ ./ngrok authtoken &lt;令牌值&gt;</pre>
<p><em>附注 Windows系统可以使用<code>ngrok authtoken &lt;令牌值&gt;</code>命令。</em></p>
<p>命令中的令牌值可以在注册后跳转的控制面板页面看到，如下图所示：</p>
<p><a href="http://greyli.com/wp-content/uploads/2018/09/ngrok-token.png"><img class="alignnone size-full wp-image-1948" src="http://greyli.com/wp-content/uploads/2018/09/ngrok-token.png" alt="" width="338" height="318" srcset="https://greyli.com/wp-content/uploads/2018/09/ngrok-token.png 338w, https://greyli.com/wp-content/uploads/2018/09/ngrok-token-150x141.png 150w, https://greyli.com/wp-content/uploads/2018/09/ngrok-token-300x282.png 300w" sizes="(max-width: 338px) 100vw, 338px" /></a></p>
<p>你也可以访问<a href="https://dashboard.ngrok.com/auth">https://dashboard.ngrok.com/auth</a>查看。</p>
<p>每次建立映射，ngrok都会分配一个随机子域的网址，如果你想拥有一个固定的域名，则要升级套餐。升级套餐的好处很多，但如果只是临时测试用的话，免费账户或是不注册使用就足够了。</p>
<p><span data-offset-key="fq2c6-0-0">注：更多的功能介绍可以访问</span><a class="Link ztext-link" href="https://ngrok.com/product" target="_blank" rel="noopener" data-offset-key="fq2c6-1-0" data-editable="true"><span data-offset-key="fq2c6-1-0">https://ngrok.com/product</span></a><span data-offset-key="fq2c6-2-0">查看。文图均来</span><a class="Link ztext-link" href="http://ngrok.com/" target="_blank" rel="noopener" data-offset-key="fq2c6-3-0" data-editable="true"><span data-offset-key="fq2c6-3-0">ngrok.com</span></a><span data-offset-key="fq2c6-4-0">。</span></p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/ngrok-flask/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
