使用requests.get调用HTTP端点获取指标时遇连接错误求助
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
Thegaierrormeans 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
requestscall
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 yourrequests.getcall: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 to10.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-3919cb432aa6If
curlfails 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

