Python中如何实现长时间连接HTTP代理
用 python 爬虫连接 http 代理但是好像都是用一次连接一次
proxies = {‘http’: ‘http://218.78.213.174:88’}
response=requests.post(url,headers=header,proxies=proxies,data=param)
大概代码就是这样
有什么方法可以长时间链接 http 代理 然后每次只要点击就可以了,谢谢。
Python中如何实现长时间连接HTTP代理
这应该跟 Connection Keep Alive 有关了,怀疑是因为 requests 不支持 keep alive ?
Python实现HTTP长连接代理
用requests库配合requests.Session()就能搞定HTTP长连接代理。Session对象会自动复用TCP连接,比单次请求效率高得多。
import requests
import time
# 代理配置(替换成你的代理信息)
proxies = {
'http': 'http://user:pass@proxy_ip:port',
'https': 'http://user:pass@proxy_ip:port'
}
# 创建Session对象
session = requests.Session()
session.proxies.update(proxies)
# 设置连接池参数
adapter = requests.adapters.HTTPAdapter(
pool_connections=10, # 连接池数量
pool_maxsize=10, # 最大连接数
max_retries=3 # 重试次数
)
session.mount('http://', adapter)
session.mount('https://', adapter)
# 测试长连接
urls = [
'http://httpbin.org/ip',
'http://httpbin.org/user-agent',
'http://httpbin.org/headers'
]
for i in range(3): # 模拟多次请求
print(f"\n=== 第{i+1}轮请求 ===")
for url in urls:
try:
response = session.get(url, timeout=10)
print(f"URL: {url}")
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}\n")
except Exception as e:
print(f"请求失败: {e}")
if i < 2: # 间隔一下
time.sleep(1)
# 记得用完关闭
session.close()
关键点:
requests.Session()- 核心对象,保持连接和cookiesHTTPAdapter- 控制连接池参数pool_connections- 保持的连接数,根据需求调整- 代理格式要写对,带认证的需要
user:pass@
如果你需要更底层的控制,可以用aiohttp做异步:
import aiohttp
import asyncio
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
connector = aiohttp.TCPConnector(limit=10) # 连接限制
async with aiohttp.ClientSession(
connector=connector,
timeout=aiohttp.ClientTimeout(total=30)
) as session:
# 设置代理
session.proxies = 'http://proxy_ip:port'
tasks = [fetch(session, 'http://httpbin.org/ip') for _ in range(5)]
results = await asyncio.gather(*tasks)
for res in results:
print(res)
# asyncio.run(main())
总结:用Session对象配合连接池参数就能实现长连接代理。
requests 应该是支持的,如果有大量请求会自动 keep-alive
既然自动那可能和时长有关?
我觉得不是自动吧
用 session 才会默认添加 keepalive 的…
requests 是支持 keep alive 的,但是必须在同一个 Session 实例中
requests 文档原话:
"Keep-alive and HTTP connection pooling are 100% automatic, thanks to urllib3."
点进去,发现:"keep-alive is 100% automatic within a session! Any requests that you make within a session will automatically reuse the appropriate connection"
所以#4 #5 是正确的。
http 1.1 前的设计本来就是针对短连接优化的, 一般服务器在发完数据就主动 close 了, 你想保持也没用, 即便不 close 你发第二次请求也不会得到应答; 而 http 代理一个情况, 除非你走的 CONNECT 方式

