问题:

在使用代理服务器访问 http://httpbin.org/ip,测试返回IP地址,原python代码:

import requests

url='http://httpbin.org/ip'

headers = {

    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36',

}
proxy={
    'http':'http://125.74.93.60:8083'
    # 'http':'http://202.102.86.228:8080',
    #  'http':'http://111.43.79.98:8085',
    # 'http':'http://39.103.175.42:3128',
    # "http":"http://127.0.0.1:80",
}

response=requests.get(url=url,headers=headers,proxies=proxy)

print(response.text)

看来许多博主request的使用方法,也没问题啊,但是运行起来,报错如下:

requests.exceptions.ProxyError: HTTPConnectionPool(host='125.74.93.60', port=8083): Max retries exceeded with url: http://httpbin.org/ip (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000013231F13088>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))

解决办法:

headers中加上'Connection' key,值为‘close',就可以了。

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36',
'Connection':'close'}
Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐