From ec696efcc64c7a4f7678bf901bb7476f6efa097b Mon Sep 17 00:00:00 2001 From: xueweihan <595666367@qq.com> Date: Tue, 22 Nov 2022 10:53:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=9B=AE=E6=A0=87=E7=BD=91=E7=AB=99?= =?UTF-8?q?=E6=94=B9=E7=89=88=E5=AF=BC=E8=87=B4=E8=8E=B7=E5=8F=96=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=20IP=20=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fetch_ips.py | 44 ++++++++------------------------------------ 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/fetch_ips.py b/fetch_ips.py index f998e9614..83f5a824f 100644 --- a/fetch_ips.py +++ b/fetch_ips.py @@ -109,21 +109,18 @@ def make_ipaddress_url(raw_url: str): :param raw_url: 原始 url :return: ipaddress 的 url """ - dot_count = raw_url.count(".") - if dot_count > 1: - raw_url_list = raw_url.split(".") - tmp_url = raw_url_list[-2] + "." + raw_url_list[-1] - ipaddress_url = "https://" + tmp_url + IPADDRESS_PREFIX + "/" + raw_url - else: - ipaddress_url = "https://" + raw_url + IPADDRESS_PREFIX - return ipaddress_url + return f'https://www.ipaddress.com/site/{raw_url}' @retry(tries=3) def get_ip(session: requests.session, raw_url: str): url = make_ipaddress_url(raw_url) try: - rs = session.get(url, timeout=5) + headers = { + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)' + ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1' + '06.0.0.0 Safari/537.36'} + rs = session.get(url, headers=headers, timeout=5) pattern = r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b" ip_list = re.findall(pattern, rs.text) ip_counter_obj = Counter(ip_list).most_common(1) @@ -135,32 +132,6 @@ def get_ip(session: requests.session, raw_url: str): raise Exception -@retry(tries=3) -def update_gitee_gist(session: requests.session, host_content): - gitee_token = os.getenv("gitee_token") - gitee_gist_id = os.getenv("gitee_gist_id") - gist_file_name = os.getenv("gitee_gist_file_name") - url = "https://gitee.com/api/v5/gists/{}".format(gitee_gist_id) - headers = { - "Content-Type": "application/json"} - data = { - "access_token": gitee_token, - "files": {gist_file_name: {"content": host_content}}, - "public": "true"} - json_data = json.dumps(data) - try: - response = session.patch(url, data=json_data, headers=headers, - timeout=20) - if response.status_code == 200: - print("update gitee gist success") - else: - print("update gitee gist fail: {} {}".format(response.status_code, - response.content)) - except Exception as e: - traceback.print_exc(e) - raise Exception(e) - - def main(): session = requests.session() content = "" @@ -170,7 +141,8 @@ def main(): host_name, ip = get_ip(session, raw_url) content += ip.ljust(30) + host_name + "\n" content_list.append((ip, host_name,)) - except Exception: + except Exception as e: + traceback.print_exc(e) continue if not content: