原文:
方式一: 通过icanhazip.com返回的ip地址进行检测
import requests
'''代理IP地址(高匿)'''
proxy = {
'http': 'http://117.85.105.170:808',
'https': 'https://117.85.105.170:808'
}
'''head 信息'''
head = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'Connection': 'keep-alive'}
'''http://icanhazip.com会返回当前的IP地址'''
p = requests.get('http://icanhazip.com', headers=head, proxies=proxy)
print(p.text)
方式二: Telnet方法
import telnetlib
def test_ip(ip,port):
try:
telnetlib.Telnet(ip,port,timeout=2)
print("代理ip有效!")
except:
print("代理ip无效!")
test_ip("221.6.139.158","9000")
以上两种方法来源于:
https://www.cnblogs.com/hankleo/p/11771682.html
下面是我自己修改的版本:
def verification(ip, port):
ips = ip+':'+port
proxy = {
'http': 'http://{}'.format(ips),
'https': 'https://{}'.format(ips)
}
'''head 信息'''
head = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'Connection': 'keep-alive'}
'''http://icanhazip.com会返回当前的IP地址'''
try:
p = requests.get('http://icanhazip.com', headers=head, proxies=proxy, timeout=10)
if p.status_code == 200:
# 我发现,返回200都不一定是能够用的,所以还得判断输出内容是否是所使用的代理ip
if ip == str(p.text.strip()):
return True
return False
except:
return False