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

使用requests.get调用HTTP端点获取指标时遇连接错误求助

Troubleshooting gaierror When Using requests.get in a Program (Works Manually)

Hey there, let's break down this issue step by step. That gaierror(-2, 'Name or service not known') error is telling you that your program can't resolve the address you're trying to connect to—even though it works when you run the exact same code manually in the command line. Here are the most common fixes and checks to try:

  • Double-check the URL in your program for typos
    It's easy to accidentally introduce a typo when copying the URL into your code (extra spaces, missing characters, wrong port, etc.). Compare the URL string in your program exactly to the one you ran manually. Even a single wrong digit in the IP or a missing slash in the UUID path can cause this error.

  • Test address resolution within your program's environment
    The gaierror means the system can't resolve the hostname/IP to a network address. Even though you're using an IP (not a domain), sometimes system network configurations or proxy settings can interfere. Add this quick test to your program to confirm:

    import socket
    try:
        socket.gethostbyname("10.206.124.139")
        print("IP resolves successfully")
    except socket.gaierror as e:
        print(f"Failed to resolve IP: {e}")
    

    If this fails, your program's environment has a network configuration issue (e.g., incorrect hosts file entries, restricted DNS access).

  • Disable proxies in your requests call
    Your command line might not use a proxy, but your program could inherit system-wide proxy settings that block access to internal IPs. Try disabling proxies explicitly in your requests.get call:

    import requests
    url = "http://10.206.124.139:9209/metrics/8ebab4dd-84bd-48c2-998c-aade88d8c82c/46567dbe-24d0-4cca-b432-a28ac5e831ec/0e297dfe-c64d-4139-bb14-a884c3e1ebc9/dbd5ca46-de73-4fe0-8273-9a9a0f5faa7a/549b0b1a-d3cc-47f0-8917-3919cb432aa6"
    response = requests.get(url, proxies={"http": None, "https": None})
    

    This rules out proxy interference as the cause.

  • Confirm your program runs in the same network context
    If your program is running in a container, virtual machine, or a different server than your command line, it might not have network access to 10.206.124.139. Test connectivity directly in the program's environment:

    curl http://10.206.124.139:9209/metrics/8ebab4dd-84bd-48c2-998c-aade88d8c82c/46567dbe-24d0-4cca-b432-a28ac5e831ec/0e297dfe-c64d-4139-bb14-a884c3e1ebc9/dbd5ca46-de73-4fe0-8273-9a9a0f5faa7a/549b0b1a-d3cc-47f0-8917-3919cb432aa6
    

    If curl fails here, the issue is network-level (firewall rules, incorrect network routing) rather than code-related.

  • Check for broken URL variable interpolation
    If you're building the URL with variables (e.g., storing UUIDs in separate variables and concatenating), make sure the final URL matches exactly. A common mistake is missing slashes or using empty variables:

    # Wrong: Missing slash after /metrics
    uuid1 = "8ebab4dd-84bd-48c2-998c-aade88d8c82c"
    url = f"http://10.206.124.139:9209/metrics{uuid1}/..."
    
    # Correct: Includes necessary slashes
    url = f"http://10.206.124.139:9209/metrics/{uuid1}/..."
    

    Print the final URL in your program to verify it's identical to the one you ran manually.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:02:40