Google Colaboratory中ngrok隧道连接报错求助:运行Flask应用后访问提示隧道未找到
Hey there! That tunnel error is super common lately because ngrok changed their requirements for free users. Let's walk through the fixes step by step:
1. The #1 Fix: Add Your ngrok Auth Token
ngrok now requires a free account and auth token to create tunnels—older tutorials probably didn't mention this because it wasn't needed before. Here's how to set it up:
- Head to the ngrok website, sign up for a free account, and grab your unique Auth Token from the dashboard.
- In your Colab notebook, add this line before running your Flask code:
Replace!ngrok config add-authtoken YOUR_AUTH_TOKEN_HEREYOUR_AUTH_TOKEN_HEREwith the token you copied.
2. Update or Replace flask-ngrok
The flask-ngrok library isn't actively maintained anymore, which can cause compatibility issues with newer Colab or ngrok versions. Try this more reliable alternative using pyngrok:
- First, uninstall the old package and install pyngrok:
!pip uninstall flask-ngrok -y !pip install pyngrok - Then use this updated code instead of your original:
from flask import Flask from pyngrok import ngrok # Set your ngrok auth token ngrok.set_auth_token("YOUR_AUTH_TOKEN_HERE") app = Flask(__name__) @app.route("/") def hello_world(): return 'Hello, World!' # Run Flask in a background thread so we can start ngrok import threading threading.Thread(target=app.run, kwargs={"use_reloader": False}).start() # Create the ngrok tunnel public_url = ngrok.connect(5000) print(f"Access your app here: {public_url}")
3. Check for Port Conflicts
If the above fixes don't work, try using a different port (sometimes Colab has other processes using port 5000):
- Change
app.run(port=5000)toapp.run(port=5001) - Update the ngrok connect line to
ngrok.connect(5001)
Give these steps a shot—most folks fix the tunnel error by adding the auth token first. Let me know if you run into any other issues!
内容的提问来源于stack exchange,提问作者Olive Yew




