Ubuntu VDS服务器上Python请求遇ConnectionResetError问题求助
Alright, let's break down why your second API request is failing on the VDS but working perfectly elsewhere, and walk through actionable fixes.
可能的核心原因
IP被目标网站标记为爬虫/限流
Most anti-scraping systems flag data center IPs (common with VDS providers) as high-risk traffic, while residential IPs (like your Windows machine) or virtual machine IPs often slip through. The first API might have looser checks, butapi.csgorun.ruappears to enforce stricter anti-bot measures.SSL/TLS协议版本不兼容
Looking at your error log, your VDS is using Python 3.6, which ships with an older SSL library. The target server likely dropped support for outdated TLS versions (like TLS 1.0/1.1) that Python 3.6 uses by default, causing the connection reset during the SSL handshake.缺失合规的请求头
Your local environment might automatically send browser-like request headers (like a properUser-Agent), while the older requests library on your VDS sends a generic, bot-looking header that gets rejected.
分步解决方案
1. 先排查IP是否被限制
On your VDS, run this curl command to test the problematic URL directly:
curl -v https://api.csgorun.ru/games/3512446
- If you see a connection reset or "403 Forbidden" response, your IP is almost certainly blocked.
- Fix options: Use a residential proxy in your Python code, request an IP change from your VDS provider, or check if the target site offers an official API key to bypass anti-scraping limits.
2. 修复SSL/TLS兼容性问题
First, upgrade your requests/urllib3 libraries to the latest versions compatible with Python 3.6:
pip install --upgrade requests urllib3 pyopenssl
Then modify your code to enforce modern TLS versions with a custom adapter, and add a browser-like User-Agent:
import requests from requests.adapters import HTTPAdapter from urllib3.poolmanager import PoolManager import ssl # Custom adapter to force modern TLS protocols class TLSAdapter(HTTPAdapter): def init_poolmanager(self, connections, maxsize, block=False): ctx = ssl.create_default_context() # Allow older cipher suites for wider compatibility ctx.set_ciphers('DEFAULT@SECLEVEL=1') # Disable outdated, insecure protocols ctx.options |= ssl.OP_NO_SSLv2 ctx.options |= ssl.OP_NO_SSLv3 ctx.options |= ssl.OP_NO_TLSv1 ctx.options |= ssl.OP_NO_TLSv1_1 self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize, block=block, ssl_context=ctx) # Create a session with the TLS adapter session = requests.Session() session.mount('https://', TLSAdapter()) # Add a browser-like User-Agent to avoid bot detection headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' } url = 'https://api.csgo3.run/current-state?montaznayaPena=null' gameId = 3512446 response = session.get(url, headers=headers) print(response) url = f'https://api.csgorun.ru/games/{gameId}' response = session.get(url, headers=headers) print(response)
3. 检查VDS网络配置
Test if your VDS can reach the target server's HTTPS port:
telnet api.csgorun.ru 443
- If the connection fails, your VDS provider might have outbound firewall rules blocking port 443 for this specific domain. Contact their support team to adjust these rules.
额外提示
- If possible, upgrade Python on your VDS to 3.8+ (since Python 3.6 is end-of-life) — newer versions have better native SSL support and fewer compatibility headaches.
- Avoid making frequent requests in quick succession, as this can trigger anti-scraping limits even with a valid IP and proper headers.
内容的提问来源于stack exchange,提问作者Definazu

