Recently, we are building our own IP pool , Among them, it is getting IP Post screening survival IP The methods are summarized as follows .
There are three main ways :
The following is a brief introduction to the above three methods :
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'
# Notice the proxy_ip yes mapping In the form of eg: {key: value}, Among them, if port No 80 Need to write
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'
# Similarly, if port Not by default 80 Need to be written separately
proxies = {
'http': '195.170.38.230:8080'}
print(' Being tested ip:', real_ip)
result = requests.get(link, headers=headers, proxies=proxies, timeout=10)
if response.status_code not in SUCCESS_CODES:
print('>>>{} Invalid ip'.format(real_ip))
else:
print('success>>>{} ip It works '.format(real_ip))