【问题标题】:requests.get doesn't care about my proxies [http / https / sock4 / sock5]requests.get 不关心我的代理 [http / https / socks4 / socks5]
【发布时间】:2020-10-13 12:51:04
【问题描述】:

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

104.207.147.141:8080
103.216.51.210:8191
3.9.34.151:3128
191.232.214.74:8080
socks4://138.59.143.37:57669
socks4://185.169.181.24:4145
socks4://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 -> 工作

你能解释一下为什么吗?

【问题讨论】:

    标签: python proxy python-requests


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 2022-08-02
      • 2020-12-16
      • 2017-12-06
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多