第一课,从IP代理开始
一、IP 代理的定义与原理
??IP 代理,也称为网络代理,是一种中间服务,它在客户端和目标服务器之间建立一个转发连接。
??当客户端发出请求时,代理服务器会代替客户端与目标服务器建立连接,并将接收到的数据返回给客户端。这种方式可以有效隐藏客户端的真实 IP 地址,保护用户隐私。
二、IP 代理的使用方法
import requests
from bs4 import BeautifulSoup
import urllib3
urllib3.disable_warnings()
import threadpool
import time
import random
import os
def get_proxy_html_89():
datas = []
page = 1
while page <= 100:
url = 'https://www.89ip.cn/index_'+str(page)+'.html'
response = requests.get(url,verify=False)
# 解析HTML
soup = BeautifulSoup(response.content, 'html.parser')
# 提取表格
table = soup.find('table', {'class': 'layui-table'})
# 提取表格数据
rows = table.find_all('tr')
for row in rows[1:]:
data = [td.text.strip() for td in row.find_all('td')]
datas.append(data)
page += 1
# 返回结果
datas = remove_duplicates(datas)
return datas
def get_proxy_ip3366_net():
datas = []
for i in range(1,10):
params = {
'action': 'china',
'page': str(i),
}
response = requests.get('https://proxy.ip3366.net/free/', params=params)
# 解析HTML
soup = BeautifulSoup(response.content, 'html.parser')
# 提取表格
table = soup.find('table', {'class': 'table table-bordered table-striped'})
# 提取表格数据
rows = table.find_all('tr')
for row in rows[1:]:
td_IP = row.find('td',{'data-title':'IP'}).text.strip()
td_PORT = row.find('td',{'data-title':'PORT'}).text.strip()
td_ADDR = row.find('td',{'data-title':'位置'}).text.strip()
td_END_TIME = row.find('td',{'data-title':'最后验证时间'}).text.strip()
datas.append([td_IP,td_PORT,td_ADDR,td_END_TIME])
# 返回结果
datas = remove_duplicates(datas)
return datas
# list去重
def remove_duplicates(lst):
return [item for item in lst if lst.count(item) == 1]
def test_proxy(data):
ip = data[0]
port = data[1]
proxy_url = ip+":"+port
try:
proxy = {'http': proxy_url, 'https': proxy_url}
response = requests.get('http://httpbin.org/ip', proxies=proxy, timeout=5)
if response.status_code == 200:
test_proxy_ip = response.json()['origin'].replace(" ","")
if test_proxy_ip==ip:
return data
else:
return None
else:
return None
except Exception:
return None
def test_one_proxy(proxy_url):
ip = proxy_url.split(":")[0]
try:
proxy = {'http': proxy_url, 'https': proxy_url}
response = requests.get('http://httpbin.org/ip', proxies=proxy, timeout=1)
if response.status_code == 200:
test_proxy_ip = response.json()['origin'].replace(" ","")
if test_proxy_ip == ip:
return True
else:
return False
else:
return False
except Exception:
return False
def multithreading(datas):
#多线程测试代理可用性
# 设置多线程数量
pools = 100
works = []
for data_one in datas:
func_params = [data_one]
works.append((func_params, None))
pool = threadpool.ThreadPool(pools)
reqs = threadpool.makeRequests(test_proxy, works, get_result)
[pool.putRequest(req) for req in reqs]
pool.wait()
pool.dismissWorkers(pools, do_join=True)
def get_result(request, results):
#获取多线程结果
try:
if results != None:
with open('results.txt','a',encoding="utf-8") as f:
f.write(str(results)+"\n")
f.close
# print("[+]",results)
except Exception as e:
print(e.args)
def get_proxy():
if os.path.exists('results.txt'):
os.remove('results.txt')
start_time = time.time()
datas = get_proxy_html_89()+get_proxy_ip3366_net()
multithreading(datas)
end_time = time.time()
# print('总共耗时:', end_time - start_time)
def random_proxy():
if not os.path.exists('results.txt'):
# print("未发现存在可用代理,开始爬取ing")
get_proxy()
random_proxy()
with open('results.txt','r',encoding="utf-8") as f:
lines = f.readlines()
f.close
if len(lines) == 0 :
# print("未发现存在可用代理,开始爬取ing")
get_proxy()
random_proxy()
else:
proxy = random.choice(lines)
proxys = proxy.replace("'","").replace(" ","").replace("[","").replace("]","").split(",")
ip = proxys[0]
port = proxys[1]
proxy_url = ip+":"+port
if test_one_proxy(proxy_url):
return proxy_url
else:
lines.remove(proxy)
with open('results.txt','w',encoding="utf-8") as f:
f.writelines(lines)
f.close
return random_proxy()
if __name__ == '__main__':
print(random_proxy())
三、IP 代理在网络安全中的优势
四、IP 代理在网络安全中的应用场景
五、总结
??因此,我们应该了解并掌握 IP 代理的使用方法,以便更好地保护自己的网络安全。同时,也要注意选择正规、可靠的代理服务器和工具,确保网络安全得到保障。
六、打广告时间