def get_ips(self, pack_id, ip_nums=1, proxy_regions=None, times=0): if times >= 10: return False if proxy_regions is None: proxy_regions = [] print(f'Get new proxy IPs ... ({times} times)') try: url = f'{self.get_ip_url}' + \ f'?num={ip_nums}&type=2&pro=0&city=0&yys=0&port=11&' + \ f'pack={pack_id}&ts=1&ys=1&cs=1&lb=1&sb=0&pb=4&mr=0®ions=' + \ ','.join(proxy_regions) res = urlopen(url, timeout=2).read().decode('utf-8') res_json = json.loads(res) if res_json.get('code', -1) > 0: my_ip = get_my_ip() if my_ip: self.add_white_ip(my_ip) time.sleep(5) return self.get_ips(pack_id, ip_nums, proxy_regions, times=times + 1) elif res_json.get('success', False): ips = res_json.get('data', {}) return ips return False except Exception as e: print(f'Get new proxy IPs FAILED. {e}') return False
def get_ips(self, pack_id: int, ip_nums: int = 1, proxy_regions: list = None, times: int = 0): if times >= 10: return False if proxy_regions is None: proxy_regions = [] proxy_regions_join = ','.join(proxy_regions) # 参数文档:https://www.tyhttp.com/help/218.html url = f'http://{self.domain_tiqu}/getip' params = { 'num': ip_nums, # 提取的IP数量 'type': 2, # 数据格式(1:TXT 2:JSON 3:html) 'pack': pack_id, # 用户套餐ID 'port': 1, # 代理协议(1表示HTTP 11表示HTTPS 2表示SOCK5) 'ts': 1, # 是否显示IP过期时间(1显示 2不显示) 'ys': 1, # 是否显示IP运营商(1显示 2不显示) 'cs': 1, # 是否显示位置(1显示 2不显示) 'lb': 1, # 分隔符(1:\r\n 2:/br 3:\r 4:\n 5:\t 6 :自定义) 'pb': 4, # 'regions': proxy_regions_join, # # 'pro': 420000, # 代表省份 # 'city': 420000, # 代表城市 } try: print(f'Get new proxy IPs ... ({times} times)') res_json = self._request(url, params=params) if res_json.get('code', -1) > 0: my_ip = get_my_ip() if my_ip: self.add_white_ip(my_ip) time.sleep(5) return self.get_ips(pack_id, ip_nums, proxy_regions, times=times + 1) elif res_json.get('success', False): ips = res_json.get('data', []) return ips return False except Exception as e: print(f'Get new proxy IPs FAILED. {e}') return False
def get_pack_info(self, count=0): try: if len(self.packs_info) > 0: return self.packs_info if count >= 10: return False self.get_pack_info_count += 1 print(f'Get Proxy Pack Info ({count} times)') gpi_url = f'{self.get_pack_info_url}?neek={self.neek}&appkey={self._appkey}' gpi_json = urlopen(gpi_url, timeout=4).read().decode('utf-8') self.packs_info = json.loads(gpi_json) return self.packs_info except Exception as e: print(f'No Available Packs!!! 10s Later Retry...({count} times)') print(e) my_ip = get_my_ip() if my_ip: self.add_white_ip(my_ip) time.sleep(10) return self.get_pack_info(count=count + 1)
def get_pack_info(self, count=0): url = f'https://{self.domain}/index/index/get_my_pack_info' params = { 'neek': self.neek, 'appkey': self.appkey, } try: if len(self.packs_info) > 0: return self.packs_info if count >= 10: return None # self.get_pack_info_count += 1 print(f'Get Proxy Pack Info ({count} times)') res_json = self._request(url, params=params) return res_json except Exception as e: print(f'No Available Packs!!! 10s Later Retry...({count} times)') print(e) my_ip = get_my_ip() if my_ip: self.add_white_ip(my_ip) time.sleep(10) return self.get_pack_info(count=count + 1)
def check_and_add_white_ip(self): my_ip = get_my_ip() if my_ip: self.add_white_ip(my_ip) time.sleep(5) return my_ip