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

Flask部署PythonAnywhere后无法正确获取用户位置的技术咨询

Why You're Getting the Wrong City on PythonAnywhere & How to Fix It

Ah, I see the issue here—let me break this down for you!

The Root Problem

Right now, your code is making the requests.get() call from your PythonAnywhere server, not from the user's device. That means the freegeoip.net API is seeing the IP address of PythonAnywhere's server, not the IP of the person visiting your site. That's why you're always getting the same city (it's the city where PythonAnywhere's server is located).

When you test locally, your computer is the server, so the API uses your local/VPN IP—hence why it works there.

How to Get the User's Actual Location

You need to:

  1. Grab the user's real IP address from their request to your Flask app.
  2. Pass that IP directly to the geo-lookup API, so it returns data for the user's location instead of the server's.

Step 1: Fetch the User's IP Address

Flask gives you access to the user's IP via request.remote_addr, but if the user is behind a proxy (like a VPN or CDN), you'll need to check the X-Forwarded-For header instead. Here's a helper function to handle both cases:

from flask import request

def get_user_ip():
    # Check for proxy-forwarded IP first
    forwarded_ip = request.headers.get('X-Forwarded-For')
    if forwarded_ip:
        # The header might have multiple IPs separated by commas—take the first one
        return forwarded_ip.split(',')[0].strip()
    # Fallback to direct IP if no proxy
    return request.remote_addr

Step 2: Modify Your API Request to Use the User's IP

The freegeoip.net API lets you specify an IP to look up by appending it to the URL. Update your index function to use this:

import requests
from flask import Flask

app = Flask(__name__)

# Add the get_user_ip function here

@app.route('/')
def index():
    user_ip = get_user_ip()
    # Pass the user's IP to the geo API
    url = f'http://freegeoip.net/json/{user_ip}'
    
    # Add error handling to avoid crashes if the API is down
    try:
        response = requests.get(url, timeout=5)
        response.raise_for_status()  # Raise an error if the request failed
        geo_data = response.json()  # Use built-in JSON parser instead of json.loads
        city = geo_data.get('city', 'Unknown')
        print(city)
        return f"Your detected city: {city}"
    except requests.exceptions.RequestException as e:
        print(f"Geo lookup failed: {str(e)}")
        return "Could not determine your location right now."

if __name__ == '__main__':
    app.run()

Extra Notes

  • Rate Limits: Be aware that free geo-lookup APIs often have request limits. If you get blocked, you might need to switch to a paid service or implement caching for repeated IPs.
  • Proxy Headers: If you're using a CDN or reverse proxy, make sure it's properly forwarding the user's IP in the X-Forwarded-For header—otherwise, you'll still get the proxy's IP instead of the user's.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:35:51