从指定 http 地址获取代理 :param proxy_source_url: :param kwargs: :return:
(proxy_source_url, **kwargs)
| 65 | |
| 66 | |
| 67 | def get_proxy_from_http(proxy_source_url, **kwargs): |
| 68 | """ |
| 69 | 从指定 http 地址获取代理 |
| 70 | :param proxy_source_url: |
| 71 | :param kwargs: |
| 72 | :return: |
| 73 | """ |
| 74 | filename = tools.get_md5(proxy_source_url) + ".txt" |
| 75 | abs_filename = os.path.join(proxy_path, filename) |
| 76 | update_interval = kwargs.get("local_proxy_file_cache_timeout", 60) |
| 77 | update_flag = 0 |
| 78 | if not update_interval: |
| 79 | # 强制更新 |
| 80 | update_flag = 1 |
| 81 | elif not os.path.exists(abs_filename): |
| 82 | # 文件不存在则更新 |
| 83 | update_flag = 1 |
| 84 | elif time.time() - os.stat(abs_filename).st_mtime > update_interval: |
| 85 | # 超过更新间隔 |
| 86 | update_flag = 1 |
| 87 | if update_flag: |
| 88 | response = requests.get(proxy_source_url, timeout=20) |
| 89 | with open(os.path.join(proxy_path, filename), "w") as f: |
| 90 | f.write(response.text) |
| 91 | return get_proxy_from_file(filename) |
| 92 | |
| 93 | |
| 94 | def get_proxy_from_file(filename, **kwargs): |
no test coverage detected