requests.get 不关心我的代理 [http / https / sock4 / sock5]



我想测试列表中包含的代理的工作状态。 代理可以是HTTP/HTTPS或Socks4/socks5类型,它们的格式如下:

104.207.147.141:8080
103.216.51.210:8191
3.9.34.151:3128
191.232.214.74:8080
袜子4://138.59.143.37:57669袜子4://185.169.181.24:4145袜子4://45.115.112.214:40308

为了测试它们,我在网站上意识到一个请求 http://ifconfig.me/ip 如果正确到达,它会向我发送回我的 IP。

def tester(proxies, ip, ref_IPs):
whitelist = []
for proxy in range(0, len(proxies), 1):
print(proxies[proxy])
rep = requests.get("http://ifconfig.me/ip", {'http': proxies[proxy], 'https': proxies[proxy]}, timeout=1)
if rep.text == ip:
whitelist.append(ref_IPs[proxy])
fileCreator(cleaner(whitelist, 1))

整个过程运行良好,但代理似乎对请求没有任何影响。因此,无论地址如何,代理都被视为功能齐全。

191.232.214.74:8080 ->工作
socks4://138.59.143.37:57669 ->工作
AAA.k4.yy.43:B345 ->工作

你能给我解释一下为什么吗?

requests.get 有以下原型:

requests.get(url, params=None, **kwargs)

您需要的是 kwargs 中的参数"代理",因此您的请求必须如下所示:

rep = requests.get("http://ifconfig.me/ip", proxies={'http': proxies[proxy], 'https': proxies[proxy]}, timeout=1)

相关内容

最新更新