获取代理池
获取代理池使用了GIthub上的项目:https://github.com/jhao104/proxy_pool ,并利用REDIS存储获取到的代理地址
启动项目
在cli
目录下通过ProxyPool.py
启动
# 首先启动调度程序
>>>python proxyPool.py schedule
# 然后启动webApi服务
>>>python proxyPool.py webserver
Api
启动过几分钟后就能看到抓取到的代理IP,可以直接到数据库中查看,也可以通过api访问http://127.0.0.1:5010 查看。
api | method | Description | arg |
---|---|---|---|
/ | GET | api介绍 | None |
/get | GET | 随机获取一个代理 | None |
/get_all | GET | 获取所有代理 | None |
/get_status | GET | 查看代理数量 | None |
/delete | GET | 删除代理 | proxy=host:ip |
保存代理
自己编写了个小脚本将ProxyPool.py
获取到的代理保存在ips.txt
中
get_proxy.py:
import requests
def get_proxy():
return requests.get("http://127.0.0.1:5010/get_all/").json()
def delete_proxy(proxy):
requests.get("http://127.0.0.1:5010/delete/?proxy={}".format(proxy))
res = requests.get("http://127.0.0.1:5010/get_status").json()
count = res.get("useful_proxy")
print("目前代理池中共计:%s个代理." % count)
f = open("ips.txt", "w")
for i in range(count):
b = get_proxy()[i]["proxy"]
f.write(b + "\n")
f.close()
本地代理转发
借用前人的成果,实现的效果是启用本地127.0.0.1:9999
服务,将ips.txt
内的代理转发给本地客户端
ips.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
from socket import error
import threading
import random
import time
localtime = time.asctime(time.localtime(time.time()))
class ProxyServerTest:
def __init__(self, proxyip):
# 本地socket服务
self.ser = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.proxyip = proxyip
def run(self):
try:
# 本地服务IP和端口
self.ser.bind(('127.0.0.1', 9999))
# 最大连接数
self.ser.listen(5)
except error as e:
print("[-]The local service : " + str(e))
return "[-]The local service : " + str(e)
while True:
try:
# 接收客户端数据
client, addr = self.ser.accept()
print('[*]accept %s connect' % (addr,))
data = client.recv(1024)
if not data:
break
print('[*' + localtime + ']: Accept data...')
except error as e:
print("[-]Local receiving client : " + str(e))
return "[-]Local receiving client : " + str(e)
while True:
# 目标代理服务器,将客户端接收数据转发给代理服务器
mbsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
iplen = len(self.proxyip)
proxyip = self.proxyip[random.randint(0, iplen - 1)]
print("[!]Now proxy ip:" + str(proxyip))
prip = proxyip[0]
prpo = proxyip[1
try:
mbsocket.settimeout(3)
mbsocket.connect((prip, prpo))
except:
print("[-]RE_Connect...")
continue
break
# except :
# print("[-]Connect failed,change proxy ip now...")
# pass
try:
mbsocket.send(data)
except error as e:
print("[-]Sent to the proxy server : " + str(e))
return "[-]Sent to the proxy server : " + str(e)
while True:
try:
# 从代理服务器接收数据,然后转发回客户端
data_1 = mbsocket.recv(1024)
if not data_1:
break
print('[*' + localtime + ']: Send data...')
client.send(data_1)
except socket.timeout as e:
print(proxyip)
print("[-]Back to the client : " + str(e))
continue
# 关闭连接
client.close()
mbsocket.close()
def Loadips():
print("[*]Loading proxy ips..")
ip_list = []
ip = ['ip', 'port']
with open("ips.txt") as ips:
lines = ips.readlines()
for line in lines:
ip[0], ip[1] = line.strip().split(":")
ip[1] = eval(ip[1])
nip = tuple(ip)
ip_list.append(nip)
return ip_list
def main():
print('''*Atuhor : V@1n3R.
*Blog :http://www.Lz1y.cn
*date: 2017.7.17
*http://www.Lz1y.cn/wordpress/?p=643
__ __ _ _____ ____
\ \ / /_ _/ |_ __ |___ /| _ \
\ \ / / _` | | '_ \ |_ \| |_) |
\ V / (_| | | | | |___) | _ < _
\_/ \__,_|_|_| |_|____/|_| \_(_)
''')
ip_list = Loadips()
# ip_list = [('118.89.148.92',8088)]
# ip_list = tuple(ip_list)
try:
pst = ProxyServerTest(ip_list)
# 多线程
t = threading.Thread(target=pst.run, name='LoopThread')
print('[*]Waiting for connection...')
# 关闭多线程
t.start()
t.join()
except Exception as e:
print("[-]main : " + str(e))
return "[-]main : " + str(e)
if __name__ == '__main__':
main()
SqlMap使用代理池
sqlmap加上代理 "–proxy=http://127.0.0.1:9999" 即可使用
使用之前建议先检测一下代理的有效性:http://h.jiguangdaili.com/check/