调用指定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/birthin your browser. If it doesn’t load, the API might be down or unreachable from your network. - Adjust request timeout: The default timeout in
requestsis 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
requestssession 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.getwithout 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
相关产品推荐
相关产品推荐

