|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 H原子 于 2021-4-27 23:25 编辑
源代码:
'''python'''- import requests
- proxies = {'http':'49.86.178.39:9999'}
- def returnip():
- url = 'http://ip.myhostadmin.net/'#返回当前访问IP的地址,已测试能成功返回当前主机IP
- response = requests.get(url,proxies=proxies,timeout=5)
- html = response.content.decode()
- print(html)
-
- if __name__ == '__main__':
- returnip()
复制代码
试了很多代理IP,都连接超时
- raise ConnectTimeout(e, request=request)
- 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)'))
复制代码
去掉超时选项,报错 ‘由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败’
- raise ProxyError(e, request=request)
- 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] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))
复制代码
请大佬解惑!!!
本帖最后由 suchocolate 于 2021-4-28 10:16 编辑
用下面的代码验证一下就知道了,返回的不是代理的ip,说明代理不可用。
现在免费的都不稳定,还是付费的稳定,另外新人没必要用代理。 - import requests
- def main():
- proxy = {'http': '49.86.178.39:9999'}
- url = 'https://my.ip.cn/api/index?ip=&type=0'
- headers = {'user-agent': 'firefox', 'Referer': 'https://my.ip.cn/', 'X-Requested-With': 'XMLHttpRequest'}
- r = requests.get(url, headers=headers, proxies=proxy)
- print(r.text)
- if __name__ == '__main__':
- main()
复制代码
|
|