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

Python requests通过Kerberos认证代理服务器失败问题求助

使用requests实现Kerberos协议的HTTP代理认证

你的代码存在两个核心问题:

  1. 代理参数未通过proxies关键字参数传递,而是错误地作为params参数传入
  2. 将Kerberos认证信息传给了目标服务器的auth参数,而非代理专用的proxy_auth参数

以下是修正后的代码,可实现通过Kerberos认证的HTTP代理访问Telegram Bot API:

import requests
from requests_kerberos import HTTPKerberosAuth, OPTIONAL

# 配置代理(Telegram API使用HTTPS,需同时设置http和https代理)
proxies = {
    'http': 'http://prx.ies.local:3128',
    'https': 'http://prx.ies.local:3128'
}

# 发起带Kerberos代理认证的请求
try:
    response = requests.get(
        # 替换为你的Telegram Bot API请求地址,例如:https://api.telegram.org/bot<你的token>/sendMessage
        url,
        proxies=proxies,
        # 使用Kerberos认证代理,OPTIONAL表示不强制双向认证,按需改为REQUIRED
        proxy_auth=HTTPKerberosAuth(mutual_authentication=OPTIONAL)
    )
    response.raise_for_status()  # 主动抛出HTTP错误
    print(response.text)
except requests.exceptions.RequestException as e:
    print(f"请求失败: {str(e)}")

关键说明:

  • proxies字典需明确指定http和https协议对应的代理地址,否则HTTPS请求会绕过代理
  • proxy_auth参数专门用于代理服务器的认证,将HTTPKerberosAuth()传入此处,而非auth参数(auth是给目标服务的认证)
  • mutual_authentication=OPTIONAL适配大多数不需要双向Kerberos认证的代理场景,若你的代理强制双向认证,可改为REQUIRED
  • 确保kinit获取的Kerberos票据在有效期内,且当前用户拥有该代理的访问权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 13:21:08