为什么相同的请求会在两台机器上导致不同的状态代码 200 和 429?

Mah*_*ahi 7 python linux python-3.x python-requests

这是我的脚本:

import requests, os

ips = ['158.46.169.208','158.46.169.252','158.46.169.76','158.46.171.23','158.46.172.217','158.46.172.55','158.46.172.98','158.46.173.104']
headers =  {"User-Agent": "Edg/90.0.818.56"}

os.system("python3 --version") #On Windows it changes to os.system("python --version")

for i in ips:
    pr = {'http':"http://"+"abcd-"+i+':xyz@example.io:22225','https':'https://'+"abcd-"+i+':xyz@example.io:22225'}
    res1 = requests.get("https://www.google.com/search?q=butter",headers=headers, proxies= pr)
    print(requests.get("https://www.httpbin.org/ip",proxies = pr,headers=headers).text)
    print(res1.status_code)
Run Code Online (Sandbox Code Playgroud)

Windows 10 上的输出:

import requests, os

ips = ['158.46.169.208','158.46.169.252','158.46.169.76','158.46.171.23','158.46.172.217','158.46.172.55','158.46.172.98','158.46.173.104']
headers =  {"User-Agent": "Edg/90.0.818.56"}

os.system("python3 --version") #On Windows it changes to os.system("python --version")

for i in ips:
    pr = {'http':"http://"+"abcd-"+i+':xyz@example.io:22225','https':'https://'+"abcd-"+i+':xyz@example.io:22225'}
    res1 = requests.get("https://www.google.com/search?q=butter",headers=headers, proxies= pr)
    print(requests.get("https://www.httpbin.org/ip",proxies = pr,headers=headers).text)
    print(res1.status_code)
Run Code Online (Sandbox Code Playgroud)

Ubuntu 18.04 上的输出:

Python 3.8.2
{
  "origin": "158.46.169.208"
}

200
{
  "origin": "158.46.169.252"
}

429
{
  "origin": "158.46.169.76"
}

200
{
  "origin": "158.46.171.23"
}

200
{
  "origin": "158.46.172.217"
}

200
{
  "origin": "158.46.172.55"
}

200
{
  "origin": "158.46.172.98"
}
Run Code Online (Sandbox Code Playgroud)

无论我运行脚本多少次(即使在两台机器上同时运行),输出始终保持不变。

我无法理解为什么它会阻止 Ubuntu 上的请求。同时,它允许在使用相同代理的同时来自 Windows 机器的相同请求。

我知道 429 错误意味着请求太多,但为什么我在 Windows 机器上运行时它总是成功?

编辑:我有一台双启动笔记本电脑,所以我在我也有 Windows 的笔记本电脑上登录到 Ubuntu 18.04。它似乎在这里工作正常。与 Windows 的行为相同。但是当我在装有 Ubuntu 18.04 的服务器上运行它时它仍然失败,并且一切都与上面相同。

Dom*_*que 0

您需要了解 HTTP 响应 200(HTTP OK)和 429(请求过多)的含义,如这篇 Wikipedia 文章中所述。

正如您所看到的,问题似乎不是由您的程序引起的,而是由您正在使用的机器引起的,该机器似乎发起了太多请求(至少服务器是这样理解的)。

简而言之,您正在处理与网络相关的问题,您可以考虑与网络管理员讨论这个问题。