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()