From ddf53befdd52c4edc72f3b0ebd8eb9ea2fad7ec0 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 10 Feb 2022 10:44:00 +0800 Subject: [PATCH] bugfix --- appPublic/iplocation.py | 18 +++++++++++++++--- appPublic/thread_workers.py | 19 +++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/appPublic/iplocation.py b/appPublic/iplocation.py index c6a10a9..b9742f9 100644 --- a/appPublic/iplocation.py +++ b/appPublic/iplocation.py @@ -1,13 +1,21 @@ import os import sys +from requests import get from bs4 import BeautifulSoup from appPublic.http_client import Http_Client from appPublic.sockPackage import get_free_local_addr public_headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36" } -def ipip(ip): + +def get_outip(): + ip = get('https://api.ipify.org').content.decode('utf8') + return ip + +def ipip(ip=None): # ipip.net + if ip is None: + ip = get_outip() api= f"http://freeapi.ipip.net/{ip}" hc = Http_Client() r= hc.get(api, headers=public_headers) @@ -16,7 +24,9 @@ def ipip(ip): 'city':r[2] } -def iplocation(ip): +def iplocation(ip=None): + if ip is None: + ip = get_outip() # apikey come from # https://app.apiary.io/globaliptv/tests/runs # using my github accout @@ -26,7 +36,9 @@ def iplocation(ip): r= hc.get(api, headers=public_headers) return r -def ipaddress_com(ip): +def ipaddress_com(ip=None): + if ip is None: + ip = get_outip() url = f'https://www.ipaddress.com/ipv4/{ip}' print('ipaddress_com(),url=', url) hc = Http_Client() diff --git a/appPublic/thread_workers.py b/appPublic/thread_workers.py index 84ccc49..79150f5 100644 --- a/appPublic/thread_workers.py +++ b/appPublic/thread_workers.py @@ -8,11 +8,14 @@ class ThreadWorkers: self.semaphore = threading.Semaphore(value=max_workers) self.co_worker = 0 def _do(self, func, *args, **kwargs): - self.semaphore.acquire() - self.co_worker += 1 - func(*args, **kwargs) - self.co_worker -= 1 - self.semaphore.release() + try: + self.semaphore.acquire() + self.co_worker += 1 + func(*args, **kwargs) + finally: + self.co_worker -= 1 + self.semaphore.release() + def do(self, func, *args, **kwargs): b = Background(self._do, func, *args, **kwargs) @@ -21,6 +24,10 @@ class ThreadWorkers: def get_workers(self): return self.co_worker + def until_done(self): + while self.co_worker > 0: + time.sleep(0.01) + if __name__ == '__main__': def k(worker): t = random.randint(1,4) @@ -30,4 +37,4 @@ if __name__ == '__main__': w = ThreadWorkers(max_workers=30) for i in range(100000): w.do(k, w) - +