鱼C论坛

 找回密码
 立即注册
查看: 3383|回复: 6

[已解决]使用代理 IP报错‘连接超时’

[复制链接]
发表于 2021-4-27 23:26:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 H原子 于 2021-4-27 23:25 编辑

源代码:
'''python'''
  1. import requests
  2. proxies = {'http':'49.86.178.39:9999'}
  3. def returnip():
  4.     url = 'http://ip.myhostadmin.net/'#返回当前访问IP的地址,已测试能成功返回当前主机IP

  5.     response = requests.get(url,proxies=proxies,timeout=5)
  6.     html = response.content.decode()

  7.     print(html)
  8.    
  9. if __name__ == '__main__':
  10.     returnip()
复制代码


试了很多代理IP,都连接超时
  1. raise ConnectTimeout(e, request=request)
  2. requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='49.86.178.39', port=9999): Max retries exceeded with url: http://ip.myhostadmin.net/ (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x033C8D30>, 'Connection to 49.86.178.39 timed out. (connect timeout=5)'))
复制代码

去掉超时选项,报错 ‘由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败’
  1. raise ProxyError(e, request=request)
  2. requests.exceptions.ProxyError: HTTPConnectionPool(host='49.86.178.39', port=9999): Max retries exceeded with url: http://ip.myhostadmin.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x033A8D10>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))
复制代码

请大佬解惑!!!
最佳答案
2021-4-28 10:15:15
本帖最后由 suchocolate 于 2021-4-28 10:16 编辑

用下面的代码验证一下就知道了,返回的不是代理的ip,说明代理不可用。
现在免费的都不稳定,还是付费的稳定,另外新人没必要用代理。
  1. import requests


  2. def main():
  3.     proxy = {'http': '49.86.178.39:9999'}
  4.     url = 'https://my.ip.cn/api/index?ip=&type=0'
  5.     headers = {'user-agent': 'firefox', 'Referer': 'https://my.ip.cn/', 'X-Requested-With': 'XMLHttpRequest'}
  6.     r = requests.get(url, headers=headers, proxies=proxy)
  7.     print(r.text)


  8. if __name__ == '__main__':
  9.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-28 08:40:18 | 显示全部楼层
首先要确定ip的可用性
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-28 08:50:37 From FishC Mobile | 显示全部楼层
这其实也在确定IP的可用性,不过我测试过很多代理IP网站上的IP,都不行,有没有具体的其它方法呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-28 10:15:15 | 显示全部楼层    本楼为最佳答案   
本帖最后由 suchocolate 于 2021-4-28 10:16 编辑

用下面的代码验证一下就知道了,返回的不是代理的ip,说明代理不可用。
现在免费的都不稳定,还是付费的稳定,另外新人没必要用代理。
  1. import requests


  2. def main():
  3.     proxy = {'http': '49.86.178.39:9999'}
  4.     url = 'https://my.ip.cn/api/index?ip=&type=0'
  5.     headers = {'user-agent': 'firefox', 'Referer': 'https://my.ip.cn/', 'X-Requested-With': 'XMLHttpRequest'}
  6.     r = requests.get(url, headers=headers, proxies=proxy)
  7.     print(r.text)


  8. if __name__ == '__main__':
  9.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-17 18:57:26 | 显示全部楼层
suchocolate 发表于 2021-4-28 10:15
用下面的代码验证一下就知道了,返回的不是代理的ip,说明代理不可用。
现在免费的都不稳定,还是付费的稳 ...

为什么这个代码运行后,返回的ip是{"rs":1,"code":0,"address":"中国  香港  ","ip":"162.158.179.133","isDomain":0}
而且我用同组的电脑ip也不能
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-17 19:04:16 | 显示全部楼层
RAINFOR 发表于 2021-11-17 18:57
为什么这个代码运行后,返回的ip是{"rs":1,"code":0,"address":"中国  香港  ","ip":"162.158.179.133"," ...

贴一下你的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-17 20:46:27 | 显示全部楼层
  1. def main():
  2.     proxy = {'http': '49.86.178.39:9999'}
  3.     url = 'https://my.ip.cn/api/index?ip=&type=0'
  4.     headers = {'user-agent': 'firefox', 'Referer': 'https://my.ip.cn/', 'X-Requested-With': 'XMLHttpRequest'}
  5.     r = requests.get(url, headers=headers, proxies=proxy)
  6.     print(r.text)

  7. 就是用上文那个代码


  8. if __name__ == '__main__':
  9.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-1 06:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表