最近在構建自用 IP 池,其中在獲取 IP 後篩選存活 IP方法做如下總結。
主要有三種方式:
下面就上面三種方式使用簡單介紹:
import telnetlib
ip = '195.170.38.230'
port = '8080'
try:
res = telnetlib.Telnet(ip, port, timeout=10)
print(res)
except Exception as e:
print('>>>', e)
print('ip invalidate')
from urllib import request
link = 'https://www.baidu.com'
# 注意這裡的 proxy_ip 是 mapping 的形式 eg: {key: value},其中如果 port 不是 80 需要寫出
proxy_ip = {
'http': '195.170.38.230:8080'}
proxy_support = request.ProxyHandler(proxy_ip)
opener = request.build_opener(proxy_support)
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36')]
res = request.urlopen(link)
print(res)
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Safari/605.1.15'
}
link = 'https://www.baidu.com'
# 同理這裡如果 port 不是默認 80 需要單獨寫出
proxies = {
'http': '195.170.38.230:8080'}
print('正在測試ip:', real_ip)
result = requests.get(link, headers=headers, proxies=proxies, timeout=10)
if response.status_code not in SUCCESS_CODES:
print('>>>{}無效ip'.format(real_ip))
else:
print('success>>>{} ip有效'.format(real_ip))