def get_ip_kuaidaili(): target_url = 'http://dps.kdlapi.com/api/getdps/?orderid=973756560305556&num=1&pt=1&format=json&sep=1' resp = requests.get(target_url) resp_json = resp.json() # print(resp_json) # {'msg': '', 'code': 0, 'data': {'count': 1, 'proxy_list': ['121.232.148.175:3256']}} if resp_json['code'] == 0: proxy_list = resp_json['data']['proxy_list'] return proxy_list[0]def ip_check_pre(self): avalible = True for _ in range(4): time.sleep(1) try: self.ip_check() avalible = True break except (requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout, urllib3.exceptions.ReadTimeoutError): self.print('ip check timeout') continue except AssertionError: self.print('ip check assert') continue else: # raise ValueError('proxy not avalible') avalible = False return avalibledef ip_check(self): headers = {"User-Agent": "Mozilla/5.0"} proxies = { # "http": f"http://85.173.165.36:46330" "http": 'http://' + self.proxy } url = 'http://httpbin.org/ip' res = requests.get(url, proxies=proxies, headers=headers, timeout=5) # print(res.text) # res_json = json.loads(res.text) # print(res_json) try: res_json = res.json() # print('ip_check', res_json) ip = res_json['origin'] except json.decoder.JSONDecodeError: ip = res.text if platform.node() == "manjaro": assert ip != "116.52.193.63", '代理失效' else: assert ip != "182.92.232.230", '代理失效'