检验代理是否有效 @param proxy: xxx.xxx.xxx:xxx @param check_url: 利用目标网站检查,目标网站url。默认为None, 使用代理服务器的socket检查, 但不能排除Connection closed by foreign host @return: True / False
(proxy, check_url=None)
| 804 | |
| 805 | |
| 806 | def is_valid_proxy(proxy, check_url=None): |
| 807 | """ |
| 808 | 检验代理是否有效 |
| 809 | @param proxy: xxx.xxx.xxx:xxx |
| 810 | @param check_url: 利用目标网站检查,目标网站url。默认为None, 使用代理服务器的socket检查, 但不能排除Connection closed by foreign host |
| 811 | @return: True / False |
| 812 | """ |
| 813 | is_valid = False |
| 814 | |
| 815 | if check_url: |
| 816 | proxies = {"http": f"http://{proxy}", "https": f"https://{proxy}"} |
| 817 | headers = { |
| 818 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" |
| 819 | } |
| 820 | response = None |
| 821 | try: |
| 822 | response = requests.get( |
| 823 | check_url, headers=headers, proxies=proxies, stream=True, timeout=20 |
| 824 | ) |
| 825 | is_valid = True |
| 826 | |
| 827 | except Exception as e: |
| 828 | log.error("check proxy failed: {} {}".format(e, proxy)) |
| 829 | |
| 830 | finally: |
| 831 | if response: |
| 832 | response.close() |
| 833 | |
| 834 | else: |
| 835 | ip, port = proxy.split(":") |
| 836 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sk: |
| 837 | sk.settimeout(7) |
| 838 | try: |
| 839 | sk.connect((ip, int(port))) # 检查代理服务器是否开着 |
| 840 | is_valid = True |
| 841 | |
| 842 | except Exception as e: |
| 843 | log.error("check proxy failed: {} {}:{}".format(e, ip, port)) |
| 844 | |
| 845 | return is_valid |
| 846 | |
| 847 | |
| 848 | def is_valid_url(url): |