You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过代理IP访问Flask本地开发服务器?

Fixing Proxy Issues When Accessing Local Flask Server from a Proxy-Configured Browser

Hey there! Let's clear up the confusion and get this sorted out—this is a common mix-up when dealing with proxies and local development servers.

First, let's break down what's happening:
When you uncheck "Bypass proxy for local addresses" in Chrome, your browser sends every request (including ones to localhost or 127.0.0.1) through the proxy server. But your default Flask development server only runs on your local machine—so the proxy server can't reach it, which is why you're seeing errors.

Also, a quick clarification: Browser proxy settings only control how your browser sends requests. If you want your Flask server itself to use the proxy (e.g., when your Flask app calls external APIs), that's a separate configuration—browser settings won't affect that.

Let's address both possible scenarios:

Scenario 1: You want your browser to access the local Flask server through the proxy

This only works if your proxy is running on your local machine (like a local proxy tool). If it's a remote proxy, the proxy server can't reach your local Flask instance unless you expose it to the public internet.

  • For local proxies:

    1. Keep your browser's proxy settings as is (no bypass for local addresses).
    2. Start your Flask server with the --host=0.0.0.0 flag to make it listen on all your machine's network interfaces:
      flask run --host=0.0.0.0
      
    3. Access your server using your machine's local IP address (e.g., 192.168.1.100:5000) instead of localhost—this ensures the proxy can route the request correctly.
  • For remote proxies:
    You'll need to expose your local Flask server to the public internet using a tool like ngrok. Once you have a public URL from ngrok, use that URL in your browser (which will route through the proxy to the public endpoint).

Scenario 2: You want your Flask server to use the proxy (not the browser accessing it)

This is probably the more common use case—if you need your Flask app to send requests through the proxy (e.g., calling external APIs), here's how to fix it:

  1. Re-enable the "Bypass proxy for local addresses" setting in Chrome. This way, your browser will directly connect to localhost:5000 without going through the proxy, so you can access your Flask server normally.
  2. Configure your Flask app to use the proxy for outgoing requests:
    • If you're using the requests library, specify the proxy directly in your code:
      import requests
      
      # Replace with your proxy IP and port
      PROXY_SETTINGS = {
          "http": "http://your-proxy-ip:port",
          "https": "https://your-proxy-ip:port"
      }
      
      # Use the proxy when making requests
      response = requests.get("https://external-api.example.com", proxies=PROXY_SETTINGS)
      
    • Alternatively, set environment variables before starting Flask to apply the proxy globally:
      # On Linux/macOS
      export HTTP_PROXY=http://your-proxy-ip:port
      export HTTPS_PROXY=https://your-proxy-ip:port
      flask run
      
      # On Windows (Command Prompt)
      set HTTP_PROXY=http://your-proxy-ip:port
      set HTTPS_PROXY=https://your-proxy-ip:port
      flask run
      

Final Note

Double-check which scenario you actually need—mixing up browser proxy vs. server-side proxy is the most likely culprit here. Once you align your settings with your actual goal, things should work smoothly!

内容的提问来源于stack exchange,提问作者ssast

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:21:20