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

Python Requests代理未生效排查:为何请求仍使用真实IP?

requests代理未生效问题排查

我在Python的requests库中使用代理,通过向jsonip.com发送请求测试代理是否生效,但响应返回的是我的真实IP而非代理IP,同时提供代理的网站显示“无活动”。请问我是否正确连接了代理?

以下是我的代码:

import time, requests, random
from requests.auth import HTTPProxyAuth
auth = HTTPProxyAuth("muyjgovw", "mtpysgrb3nkj")

def reqs():   
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        # 'Accept-Encoding': 'gzip, deflate, br',
        'Referer': 'https://www.google.com/',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'cross-site',
        'Sec-Fetch-User': '?1',
    }
    prox = [{"http": "http://64.137.58.19:6265"}]
    proxies = random.choice(prox)
    response = requests.get('https://jsonip.com/', headers=headers, proxies=proxies)
    print(response.status_code)
    print(response.json())
reqs()

代理网站显示无活动的截图


问题原因及修复方案

  • 未传入代理认证信息:你已经创建了HTTPProxyAuth对象,但没有在requests.get()中通过auth参数传入,代理服务器需要验证账号密码,导致请求未通过代理转发。
  • 代理协议不匹配:请求目标是HTTPS地址,但你只配置了http协议的代理,requests会对HTTPS请求使用直连,因此需要同时配置http和https协议的代理。

修正后的代码

import time, requests, random
from requests.auth import HTTPProxyAuth
auth = HTTPProxyAuth("muyjgovw", "mtpysgrb3nkj")

def reqs():   
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        # 'Accept-Encoding': 'gzip, deflate, br',
        'Referer': 'https://www.google.com/',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'cross-site',
        'Sec-Fetch-User': '?1',
    }
    # 同时配置http和https协议的代理
    prox = [
        {
            "http": "http://64.137.58.19:6265",
            "https": "http://64.137.58.19:6265"
        }
    ]
    proxies = random.choice(prox)
    # 传入auth参数
    response = requests.get('https://jsonip.com/', headers=headers, proxies=proxies, auth=auth)
    print(response.status_code)
    print(response.json())
reqs()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 07:35:35