class ProxyAip(object): def __init__(self): self.app = Flask(__name__) self.mongo = MongoPool() @self.app.route('/') def hello_world(): tips = '/random?protocol&domain&count' return tips # 随机获取一个代理 @self.app.route('/random') def random(): # 获取协议 protocol = request.args.get('protocol') # 获取域名 domain = request.args.get('domain') proxy = self.mongo.random_proxy(protocol, domain, count=PROXIES_MAX_COUT) if protocol: return '{}://{}:{}'.format(protocol, proxy.ip, proxy.prot) else: return '{}:{}'.format(proxy.ip, proxy.prot) # 获取队列中代理 @self.app.route('/proxies') def proxies(): protocol = request.args.get('protocol') domain = request.args.get('domain') proxies = self.mongo.get_proxies(protocol, domain, count=PROXIES_MAX_COUT) proxies = [proxy.__dict__ for proxy in proxies] return json.dumps(proxies) # 获取过滤掉不可用域名的代理 @self.app.route('/disable_domain') def disable_domain(): ip = request.args.get('ip') domain = request.args.get('domain') if ip is None: return '提供ip参数' if domain is None: return '提供domian参数' self.mongo.disable_domain(ip, domain) return '{}禁用()成功!'.format(ip, domain) def run(self): self.app.run('127.0.0.1', port=8000) @classmethod def start(cls): proxy_api = ProxyAip() proxy_api.run()
class Proxy_Api(object): def __init__(self): #初始化一个Flask服务 self.app = Flask(__name__) # 创建MongoPool对象用于操作数据库 self.mongo_pool = MongoPool() @self.app.route('/random') def random(): protocol = request.args.get('protocol') domain = request.args.get('domain') # 随机返回多个proxy对象 proxy = self.mongo_pool.random_proxy(protocol, domain, count=PROXIES_MAX_COUNT, nick_type=2) #print(proxy) if protocol: return '{}://{}:{}'.format(protocol, proxy.ip, proxy.port) else: return '{}:{}'.format(proxy.ip, proxy.port) @self.app.route('/proxies') def proxies(): protocol = request.args.get('protocol') domain = request.args.get('domain') proxies = self.mongo_pool.get_proxies(protocol, domain, count=PROXIES_MAX_COUNT) #把Proxy对象列表转换成字典才可以转换成json proxies = [proxy.__dict__ for proxy in proxies] #字典转换成json print(json.dumps(proxies)) return json.dumps(proxies, ensure_ascii=False) @self.app.route('/disable_domain') def disable_domain(): ip = request.args.get('ip') domain = request.args.get('domain') if ip is None: return '请提供ip这个参数' if domain is None: return '请提供domain这个参数' self.mongo_pool.disable_domain(ip, domain) return '{}禁用域名{}成功'.format(ip, domain) def run(self): self.app.run('127.0.0.1', port=16889) @classmethod def start(cls): Pa = cls() Pa.run()
class ProxyApi(object): """ 代理ip的web服务 """ def __init__(self): """ 初始化flask服务 """ # 创建flask服务 self.app = Flask(__name__) # 创建数据库操作对象 self.proxy_pool = MongoPool() @self.app.route('/random') def _random(): """ 根据条件随意获取一个代理ip :return: """ # 获取url中的请求参数 protocol = request.args.get('protocol') nick_type = request.args.get('nick_type') domain = request.args.get('domain') nick_type = int(nick_type) if nick_type else None # 从数据库中取出数据 proxy = self.proxy_pool.random(protocol=protocol, domain=domain, nick_type=nick_type, count=AVAILABLE_IP_COUNT) if proxy: # 判断是否有协议类型 if protocol: return "{}://{}:{}".format(protocol, proxy.ip, proxy.port) else: return "{}:{}".format(proxy.ip, proxy.port) else: return '' @self.app.route('/proxies') def proxies(): """ 根据条件获取若干个代理ip :return: """ # 获取url中的请求参数 protocol = request.args.get('protocol') nick_type = request.args.get('nick_type') domain = request.args.get('domain') count = request.args.get('count') nick_type = int(nick_type) if nick_type else None count = int(count) if count else AVAILABLE_IP_COUNT # 从数据库中取出数据 proxies_list = self.proxy_pool.get_proxies(protocol=protocol, domain=domain, nick_type=nick_type, count=count) # 用于存放若干个代理ip result = [] # 依次将代理ip转化为字典格式并存入结果列表中 for proxy in proxies_list: result.append(proxy.__dict__) # 将结果列表转化为字符串格式 return json.dumps(result) @self.app.route('/disable_domain') def disable_domain(): """ 更新此代理ip下的不可用域名列表 """ # 获取url中的请求参数 ip = request.args.get('ip') domain = request.args.get('domain') # 判断传入参数是否为空 if ip is None: return "请传入有效ip" if domain is None: return "请传入有效domain" # 更新此代理ip下的不可用域名列表 self.proxy_pool.disable_domain(ip=ip, domain=domain) return "此代理ip不可用域名列表更新成功" @self.app.route('/headers') def headers(): # 请求头 with open('user-agent.txt', 'r', encoding='utf-8') as f: USER_AGENTS_LIST = f.readlines() # 去掉开头的",末尾的"\n return str(random.choice(USER_AGENTS_LIST)[1:-2]) def run(self): """ 启动flask服务 :return: """ self.app.run(host=FLASK_HOST, port=FLASK_PORT) @classmethod def start(cls): """ 开启web服务 :return: """ # 创建并运行web服务 api = cls() api.run()
class ProxyApi(object): def __init__(self): # 初始一个Flask的Web服务 self.app = Flask(__name__) self.proxy_pool = MongoPool() @self.app.route('/random') def random(): # 从传入参数中获取URL # 根据protocol参数获取协议 protocol = request.args.get('protocol') # 根据domain参数获取域名 domain = request.args.get('domain') proxy = self.proxy_pool.random(protocol=protocol, domain=domain, count=settings.AVAILABLE_IP_COUNT) # 如果有协议, 就返回带有协议代理IP和端口号 if protocol: return '{}://{}:{}'.format(protocol, proxy.ip, proxy.port) else: # 如果没有协议就返回, 不带协议的IP和端口号 return '{}:{}'.format(proxy.ip, proxy.port) @self.app.route('/proxies') def proxies(): # 根据protocol参数获取协议 protocol = request.args.get('protocol') # 根据domain参数获取域名 domain = request.args.get('domain') proxies = self.proxy_pool.get_proxies( protocol=protocol, domain=domain, count=settings.AVAILABLE_IP_COUNT) lis = [] for proxy in proxies: lis.append(proxy.__dict__) return json.dumps(lis) @self.app.route('/disable_domain') def disable_domain(): # 获取IP地址 ip = request.args.get('ip') # 获取不可用域名 domain = request.args.get('domain') if ip is None: return '请传入ip参数' if domain is None: return '请传入domain参数' # 更新域名成功 self.proxy_pool.disable_domain(ip=ip, domain=domain) return '该IP添加不可用域名成功' def run(self): self.app.run(host="0.0.0.0", port=6868) @classmethod def start(cls): proxy_api = cls() proxy_api.run()