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

调用指定API时出现ConnectionError请求超时问题求助

Troubleshooting Connection Timeout Error with Transparência do Registro Civil API

It looks like you’re hitting a connection timeout when trying to access the Brazilian Civil Registry’s transparency API. Let’s break down the issue and walk through practical fixes to get you back on track.

Your Code:

import requests
import json
from json.decoder import JSONDecodeError

session = requests.Session()
session.headers.update({'User-Agent': 'Custom user agent'})

resposta_estados = session.get('https://transparencia.registrocivil.org.br/api/record/birth')
resposta_estados = resposta_estados.json()

Error Message:

ConnectionError: HTTPSConnectionPool(host='transparencia.registrocivil.org.br', port=443): Max retries exceeded with url: /api/record/birth (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f7df8142220>: Failed to establish a new connection: [Errno 60] Operation timed out'))

Possible Fixes:

  • Check if the API endpoint is reachable: First, try visiting https://transparencia.registrocivil.org.br/api/record/birth in your browser. If it doesn’t load, the API might be down or unreachable from your network.
  • Adjust request timeout: The default timeout in requests is pretty short. Add a longer timeout parameter to give the server more time to respond:
    resposta_estados = session.get('https://transparencia.registrocivil.org.br/api/record/birth', timeout=30)  # 30 seconds
    
  • Use a standard User-Agent: Some APIs block non-standard or generic User-Agents. Swap yours for a browser-like string:
    session.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'})
    
  • Check network restrictions: If you’re behind a corporate firewall or proxy, it might be blocking outgoing requests to this domain. You may need to configure proxy settings in your requests session or check with your network admin.
  • Test rate limits: Even timeouts can sometimes stem from hitting rate limits. Wait a few minutes and try again with a single request to rule this out.
  • Simplify your request: Try a basic requests.get without the Session object first to isolate if the issue is with session setup:
    import requests
    response = requests.get('https://transparencia.registrocivil.org.br/api/record/birth', timeout=30, headers={'User-Agent': 'Mozilla/5.0 ...'})
    print(response.status_code)
    

Modified Code Example:

Here’s a version incorporating timeout, standard User-Agent, and error handling to make debugging easier:

import requests
import json
from json.decoder import JSONDecodeError

session = requests.Session()
# Use a browser-like User-Agent
session.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'})

try:
    resposta_estados = session.get('https://transparencia.registrocivil.org.br/api/record/birth', timeout=30)
    resposta_estados.raise_for_status()  # Triggers error for bad HTTP status codes
    data = resposta_estados.json()
    print(data)
except requests.exceptions.ConnectionError as e:
    print(f"Connection failed: {e}")
except requests.exceptions.Timeout as e:
    print(f"Request took too long: {e}")
except JSONDecodeError as e:
    print(f"Failed to parse response as JSON: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 10:25:45