Expose a Flask Web App to the Internet Without Port Forwarding

Ganesh Velrajan Ganesh Velrajan • 11 Min Read • Updated on Nov 03, 2026
Expose a Flask Web App to the Internet Without Port Forwarding

Short answer: Flask’s development server binds to 127.0.0.1 by default, so only your own machine can open it. To reach it from other devices (such as your mobile phone or another laptop) on your network, run flask run --host=0.0.0.0. To reach it from the internet without port forwarding or a cloud server without a public IP, create a secure tunnel using SocketXP: socketxp connect http://localhost:5000 gives your Flask app a public HTTPS URL.

TL;DR

Get a public web URL for your Flask app in 3 commands

  1. python3 -m flask --app myapp run --port 5000 (keep debug mode off)
  2. socketxp login <your-auth-token>
  3. socketxp connect http://localhost:5000 → https://<random-id>.socketxp.com

This guide covers all three levels of access, from your own machine to your LAN to the internet. It also covers the problems you’re most likely to hit along the way: the 0.0.0.0 setting, firewalls, port 5000 on macOS, http:// links behind a proxy, and keeping the app online after you close the terminal.

All the Python code examples shown in this artilce are tested with Python 3.14 and Flask 3.1.

Why Your Flask App Isn’t Reachable

There are three levels of reachability, and each one needs a different fix:

Who can access the appAddress Flask must listen onWhat else you need
Only you, on the same machine127.0.0.1 (default)Nothing
Other devices on your WiFi or LAN (phone, colleague’s laptop)0.0.0.0 or your LAN IPAllow the port through your OS firewall
Anyone on the internet127.0.0.1 or 0.0.0.0 or your LAN IPA secure reverse tunnel (SocketXP), port forwarding, or a cloud server with public IP address

Your WiFi router sits between your LAN and the internet. It performs NAT, so it doesn’t forward inbound connections from the internet to your laptop or Raspberry Pi unless you configure it to. If your ISP uses CGNAT, which is common on 4G/5G, Starlink and many home connections, you can’t configure it at all. That’s why changing Flask’s host setting alone never makes the app reachable from the internet.

Level 1: Run Flask on Localhost

Install Flask:

$ python3 -m pip install flask

Create a simple app in myapp.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return '<h2>Hello, World!</h2>'

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)

Run it with the flask command or directly with Python:

$ python3 -m flask --app myapp run
 * Serving Flask app 'myapp'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

Open http://localhost:5000 in your browser and you’ll see the “Hello, World!” page. At this point only your own machine can reach it.

Port 5000 already in use on macOS

On macOS Monterey and later, the AirPlay Receiver service listens on port 5000, and Flask fails with this error:

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 searching for and disabling 'AirPlay Receiver' in System Settings.

Either turn off AirPlay Receiver in System Settings, or use another port:

$ python3 -m flask --app myapp run --port 5001

Level 2: Access Flask from Other Devices on Your Network

To open the app from your phone or another computer on the same WiFi, tell Flask to listen on all network interfaces with --host=0.0.0.0:

$ python3 -m flask --app myapp run --host=0.0.0.0
 * Serving Flask app 'myapp'
 * Debug mode: off
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.29.215:5000
Press CTRL+C to quit

Flask prints your LAN address on startup. Open that URL (here http://192.168.29.215:5000) on any device connected to the same network.

There are three equivalent ways to set the host:

MethodExample
Command-line flagflask --app myapp run --host=0.0.0.0 --port=5000
Environment variablesFLASK_RUN_HOST=0.0.0.0 FLASK_RUN_PORT=5000 flask --app myapp run
In codeapp.run(host='0.0.0.0', port=5000)

What does 0.0.0.0 mean? 0.0.0.0 is the unspecified address. You never connect to it. When a server binds to it, the server accepts connections on every interface the machine has: loopback, Ethernet and WiFi. You can also bind to one specific LAN IP such as 192.168.1.20, but that address can change when your router’s DHCP lease renews, so 0.0.0.0 is the safer choice.

Find your machine’s LAN IP address

If you need the address yourself, for example to give it to a colleague:

OSCommand
Linux / Raspberry Pihostname -I or ip -4 addr
macOSipconfig getifaddr en0
Windowsipconfig (look for IPv4 Address)

Still can’t connect? Check the firewall

If the page times out on your phone even though Flask is on 0.0.0.0, your OS firewall is almost always the cause:

  • Ubuntu / Raspberry Pi (ufw): sudo ufw allow 5000/tcp
  • Windows: when Windows Defender Firewall asks whether to allow Python, allow it on Private networks. You can also add an inbound rule for TCP port 5000.
  • macOS: if the firewall is on, allow incoming connections for Python when prompted, or in System Settings → Network → Firewall → Options.

Also check that both devices are on the same network. Guest WiFi networks and some office networks isolate clients from each other.

Level 3: Expose Your Flask App to the Internet

Changing the host and firewall settings doesn’t make the app reachable from outside your network. You have four options:

OptionCostSetup timePublic HTTPSWorks behind CGNAT (4G/5G, Starlink)Router access needed
Router port forwardingFree30+ minNo (you add TLS yourself)NoYes
Deploy to a cloud VM or PaaS (AWS, GCP, Azure, DigitalOcean)Monthly billHoursYou configure itn/a (runs in the cloud)No
Cloudflare TunnelFree15–30 min (needs a domain on Cloudflare for a stable URL)YesYesNo
SocketXP tunnel30-day free plan, then paidAbout 2 minYesYesNo

Port forwarding exposes your home IP and fails on most mobile and satellite connections. A cloud deployment is right for production, but it’s slow and costly for demos, testing and webhooks. A secure reverse tunnel lets you keep the app on your laptop or Raspberry Pi and still give it a permanent public URL. For more tunneling options, see our comparison of the best ngrok alternatives.

How SocketXP works

Diagram: SocketXP agent creates an outbound TLS tunnel from the Flask app to the SocketXP cloud gateway, which serves a public HTTPS URL
  1. Your Flask app runs on localhost, unchanged.
  2. The SocketXP agent, running on the same machine as your Flask app, opens an outbound encrypted connection (a TLS encrypted secure reverse tunnel) to the SocketXP Cloud Gateway. Routers and firewalls allow outbound connections, so no inbound port needs to be opened.
  3. SocketXP gives you a public HTTPS URL. HTTP requests sent to that URL travel down through the cloud gateway and the tunnel to your Flask app.

Want Your Flask App Live on the Internet?

Skip the cloud setup and server costs. Get a permanent public HTTPS URL in seconds.

Step-by-Step: Flask Localhost to Public HTTPS URL

Step 1: Run Flask with debug mode off

$ python3 -m flask --app myapp run --port 5000

Security warning: Never expose an app that runs with debug=True or --debug. The Werkzeug interactive debugger lets anyone who triggers an error page run arbitrary Python code on your machine. Use debug mode only on 127.0.0.1, and only while nothing is exposed.

Step 2: Install the SocketXP agent

Download and install the SocketXP agent for Linux, macOS, Windows or Raspberry Pi from the download page.

Step 3: Log in with your auth token

Copy your auth token from the SocketXP portal:

Copying the SocketXP auth token from the SocketXP web portal

Paste the login command into your terminal:

$ socketxp login "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

Step 4: Create the public URL

$ socketxp connect http://localhost:5000
Public URL -> https://679aa48b-1162-44f7-b6c6-59129dd68b58.socketxp.com

Open the public URL from any device on any network:

Flask Hello World app opened through its public SocketXP HTTPS URL in a browser

The URL is permanent. It stays assigned to you until you delete the tunnel in the portal, so you can share it with clients or register it as a webhook endpoint.

Protect Your Public Flask App

A public URL is reachable by anyone who has the link. If the app isn’t meant to be public, add a login. This snippet adds HTTP Basic Auth to every route, using only Flask:

import hmac
import os

from flask import Flask, Response, request

app = Flask(__name__)

APP_USER = os.environ.get("APP_USER", "admin")
APP_PASS = os.environ["APP_PASS"]  # set this before starting the app


@app.before_request
def require_login():
    auth = request.authorization
    if (
        auth is None
        or auth.type != "basic"
        or not hmac.compare_digest(auth.username or "", APP_USER)
        or not hmac.compare_digest(auth.password or "", APP_PASS)
    ):
        return Response("Login required", 401, {"WWW-Authenticate": 'Basic realm="Flask app"'})
$ APP_PASS='a-long-random-password' python3 -m flask --app myapp run

Browsers show a login prompt, and requests without the right credentials get 401 Unauthorized. Basic Auth is only safe over HTTPS, and the SocketXP URL is HTTPS.

Behind any proxy or tunnel, Flask sees requests arriving from the local agent over plain HTTP. As a result, url_for(..., _external=True), redirects and OAuth callback URLs can come out as http://localhost:5000/... instead of your public https:// URL.

If your proxy or tunnel sends X-Forwarded-Proto and X-Forwarded-Host headers, Werkzeug’s ProxyFix middleware tells Flask to trust them:

from werkzeug.middleware.proxy_fix import ProxyFix

app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)

With this in place, a request with X-Forwarded-Proto: https and X-Forwarded-Host: abc.socketxp.com makes url_for('index', _external=True) return https://abc.socketxp.com/. Add ProxyFix only when the app is actually behind a proxy. Otherwise clients can spoof these headers.

Also set SESSION_COOKIE_SECURE = True once the app is served only over HTTPS, so session cookies are never sent over plain HTTP.

Keep Your Flask App Online 24/7

flask run and a terminal tunnel stop as soon as you close the terminal. For a Raspberry Pi dashboard, a home server or a long-running demo:

1. Serve the app with a production WSGI server. Use gunicorn on Linux, macOS and Raspberry Pi:

$ python3 -m pip install gunicorn
$ gunicorn -w 2 -b 127.0.0.1:5000 myapp:app

On Windows, use waitress instead: pip install waitress, then waitress-serve --listen=127.0.0.1:5000 myapp:app.

2. Run gunicorn as a systemd service so it restarts on boot or crash. Save this as /etc/systemd/system/myapp.service:

[Unit]
Description=My Flask app
After=network.target

[Service]
User=pi
WorkingDirectory=/home/pi/myapp
ExecStart=/home/pi/myapp/venv/bin/gunicorn -w 2 -b 127.0.0.1:5000 myapp:app
Restart=always

[Install]
WantedBy=multi-user.target
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now myapp

3. Run the SocketXP agent as a service. Define the tunnel in /etc/socketxp/config.json:

{
    "region": "",
    "tunnels": [
    {
        "destination": "http://127.0.0.1:5000"
    }]
}

Then install and start the service:

$ sudo socketxp service install --config /etc/socketxp/config.json
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now socketxp

See the SocketXP documentation for the full background service setup. Your Flask app and its public URL now come back automatically after every reboot. This is how you’d run a web app on a Raspberry Pi or an IoT device deployed in the field, including devices on Starlink or 4G/5G networks behind CGNAT.

Common Use Cases

  • Testing webhooks locally. Stripe, GitHub, Twilio and Slack need a public HTTPS endpoint to call. Point them at your SocketXP URL and debug the Flask handler on your laptop.
  • Client demos and feedback. Share a work-in-progress app without deploying it.
  • Mobile app backends. Test a phone app on mobile data against the Flask API running on your laptop.
  • Raspberry Pi and IoT dashboards. Monitor sensors and control devices from anywhere.
  • AI and LLM apps. Share a local chatbot built on Ollama. See Build a Local LLM Chatbot with Flask and Ollama for a complete example (code on GitHub), or access Ollama, vLLM or LM Studio remotely if you only need the model API.

Troubleshooting Checklist

SymptomLikely causeFix
Works on localhost, not from your phoneFlask bound to 127.0.0.1flask run --host=0.0.0.0
Bound to 0.0.0.0, phone still times outOS firewallAllow TCP 5000 (ufw, Windows Defender, macOS Firewall)
Works on WiFi, not on mobile dataNo route from the internet to your LANUse a tunnel such as SocketXP
Address already in use on MacAirPlay Receiver on port 5000Use --port 5001 or turn off AirPlay Receiver
Public URL works, but links/redirects go to http://localhostFlask unaware of the proxyAdd ProxyFix
App and URL go down when you close the terminalRunning in the foregroundUse gunicorn + systemd + socketxp service install
WARNING: This is a development serverNormal Flask dev server noticeUse gunicorn or waitress for long-running use

For Flask’s own guidance on running in production, see the official Flask deployment documentation.

Flask vs Django: Which One Should You Use?

Django is the other major Python web framework. It’s full-stack and batteries-included, while Flask is a minimal micro-framework. For a full breakdown of architecture, ORM, admin panel, REST APIs and learning curve, read Flask vs Django: Which Python Web Framework Should You Use?

Whichever you choose, exposing it with SocketXP works the same way. Only the default port differs (Flask uses 5000, Django uses 8000), and Django needs its ALLOWED_HOSTS setting updated before a tunnel works. See How to Remote Access Your Django Server from the Internet for the Django version of this guide.

Frequently Asked Questions

  1. Why can't I access my Flask app from another device or the internet?

    By default, the Flask development server binds to 127.0.0.1 (localhost), which only the same machine can reach. To let other devices on your network (your phone or another laptop) connect, run 'flask run --host=0.0.0.0' and allow the port through your OS firewall. To reach it from outside your network, use a tunnel service like SocketXP, because your router (WiFi, cellular or satellite) does not forward inbound connections to your machine.

  2. How do I run Flask on 0.0.0.0?

    Run 'flask --app myapp run --host=0.0.0.0', set the environment variable FLASK_RUN_HOST=0.0.0.0, or call app.run(host='0.0.0.0') in your code. Flask then listens on every network interface, and prints both the 127.0.0.1 address and your LAN address (for example http://192.168.1.20:5000) when it starts.

  3. How do I open my Flask app on my phone?

    Connect your phone to the same WiFi network, run Flask with --host=0.0.0.0, and open http://:5000 in the phone's browser. If it times out, your computer's firewall is blocking the port. If your phone is on mobile data, use a public URL from a tunnel like SocketXP instead.

  4. What's the difference between 127.0.0.1, 192.168.x.x and 0.0.0.0?

    127.0.0.1 is the loopback address, reachable only from the same machine. 192.168.x.x and 10.x.x.x are private LAN addresses, reachable by other devices on the same network but not from the internet. 0.0.0.0 is not an address you connect to. It's the 'unspecified' address, and binding to it tells Flask to listen on every network interface on the machine.

  5. Why does Flask say 'WARNING: This is a development server'?

    Flask prints that warning every time you start its built-in Werkzeug development server, whatever address it is bound to. It's not an error and it doesn't block remote access. It means the server isn't built for production traffic. For anything long-running, serve the app with a WSGI server such as gunicorn (Linux, macOS) or waitress (Windows).

  6. Why is port 5000 already in use on my Mac?

    On macOS Monterey and later, the AirPlay Receiver service listens on port 5000. Flask then fails with 'Address already in use. Port 5000 is in use by another program.' Either run Flask on another port with 'flask run --port 5001', or turn off AirPlay Receiver in System Settings.

  7. Is it safe to expose a Flask app to the internet?

    It's safe if you take three precautions: turn debug mode off (the Werkzeug debugger allows remote code execution), add authentication if the app isn't meant to be public, and serve it with gunicorn or waitress for anything long-running. SocketXP adds an encrypted HTTPS tunnel, so you don't open inbound firewall ports or expose your router's public IP address.

  8. Do I need port forwarding to make a Flask app public?

    No. Port forwarding requires router access and a public IP address, and it doesn't work behind CGNAT, which most 4G/5G, Starlink and many home ISPs use. A secure reverse tunnel service like SocketXP makes an outbound connection from your machine instead, so it works on any network without changing router or firewall settings.

  9. Does the SocketXP public URL change every time I restart my Flask app?

    No. The public URL assigned to you stays the same until you delete the tunnel from the SocketXP portal, so you can share the same link with clients or use it as a webhook URL.

  10. Is SocketXP free to use for exposing a Flask app?

    SocketXP has a free plan with all features included and no credit card required. It's valid for 30 days, after which you need a paid plan to keep using the service. See the pricing page for current plans.

  11. How is SocketXP different from ngrok for Flask apps?

    Both create a public HTTPS URL that forwards to a local port, so for a single Flask app the core function is similar. SocketXP also handles long-term remote access to IoT device fleets, secure SSH/VNC/RDP tunnels and centralized device management, which helps if your Flask app runs on a Raspberry Pi or other device in the field.

SocketXP IoT Remote Access and Device Management Platform

Remotely access, manage, and update your IoT & AIoT edge fleet with SocketXP's secure and scalable platform.

Start Your Free Trial Now!

Join thousands of satisfied users who trust SocketXP for a secure, reliable, and scalable IoT Edge device management solution. Start your free trial now.