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

Python Requests无法建立连接问题求助

解决方案
  • 先验证系统层面网络连通性
    打开命令提示符(CMD),执行以下命令测试:

    • ping google.com:查看是否能解析并连通目标服务器
    • curl https://google.com:模拟HTTP请求,确认系统网络栈是否正常工作
      如果这两个命令都失败,优先排查本地网络的DNS配置、路由连接问题,比如切换到公共DNS(8.8.8.8或114.114.114.114)。
  • 排查防火墙/杀毒软件拦截

    • 临时关闭Windows Defender防火墙:进入「控制面板→系统和安全→Windows Defender防火墙」,选择「启用或关闭Windows Defender防火墙」,暂时关闭后重新运行Python代码测试。
    • 检查第三方杀毒软件(如360、腾讯电脑管家等)的网络防护规则,确认是否拦截了Python进程的网络请求,可临时关闭防护后验证。
  • 配置代理(如果网络需要代理)
    如果你的网络环境需要通过代理访问外部网站,requests默认不会自动使用系统代理,可手动配置:

    import requests
    
    # 替换为你的代理地址和端口
    proxies = {
        'http': 'http://proxy.example.com:8080',
        'https': 'https://proxy.example.com:8080',
    }
    
    try:
        response = requests.get('https://google.com', proxies=proxies, timeout=10)
        print(response.status_code)
    except Exception as e:
        print(e)
    

    也可以让requests自动读取系统代理:

    import requests
    from requests.utils import get_environ_proxies
    
    url = 'https://google.com'
    proxies = get_environ_proxies(url)
    try:
        response = requests.get(url, proxies=proxies, timeout=10)
        print(response.status_code)
    except Exception as e:
        print(e)
    
  • 测试国内站点排除区域限制
    尝试请求国内网站(如https://www.baidu.com),如果能正常响应,说明可能是目标站点的区域访问限制问题,可考虑使用合规的网络访问方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 21:39:51