python请求库在亚马逊aws中不起作用

我正在尝试以下代码:

import requests

headers = {
    'authority': 'www.nseindia.com',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 OPR/72.0.3815.320',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'sec-fetch-site': 'none',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'accept-language': 'en-GB,en;q=0.9',
}

nse = requests.Session()
x = nse.get("https://www.nseindia.com/", headers=headers)

print(x.text)

以下代码在我的电脑上运行,但是当我将它放入 aws 时它没有响应。

我还检查了 pinghttps://www.nseindia.com/它是否正常工作。

requests 适用于谷歌等其他网站,但不适用于 aws 上的这个特定网站。

在 EC2 中:

Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> headers = {
...     'authority': 'www.nseindia.com',
...     'upgrade-insecure-requests': '1',
...     'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 OPR/72.0.3815.320',
...     'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
...     'sec-fetch-site': 'none',
...     'sec-fetch-mode': 'navigate',
...     'sec-fetch-user': '?1',
...     'sec-fetch-dest': 'document',
...     'accept-language': 'en-GB,en;q=0.9',
... }
>>> nse = requests.Session()
>>> nse.get("https://www.nseindia.com/", headers=headers)

最后一行没有输出。

在我的电脑中:

Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> headers = {
...     'authority': 'www.nseindia.com',
...     'upgrade-insecure-requests': '1',
...     'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 OPR/72.0.3815.320',
...     'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
...     'sec-fetch-site': 'none',
...     'sec-fetch-mode': 'navigate',
...     'sec-fetch-user': '?1',
...     'sec-fetch-dest': 'document',
...     'accept-language': 'en-GB,en;q=0.9',
... }
>>> nse = requests.Session()
>>> nse.get("https://www.nseindia.com/", headers=headers)
<Response [200]>
>>> 

检测到问题:

在EC2

ping www.nseindia.com
PING www.nseindia.com (23.9.215.115) 56(84) bytes of data.
64 bytes from a23-9-215-115.deploy.static.akamaitechnologies.com (23.9.215.115): icmp_seq=1 ttl=51 time=1.07 ms
64 bytes from a23-9-215-115.deploy.static.akamaitechnologies.com (23.9.215.115): icmp_seq=2 ttl=51 time=1.09 ms

电脑输入

ping www.nseindia.com
PING www.nseindia.com (23.35.32.140) 56(84) bytes of data.
64 bytes from a23-35-32-140.deploy.static.akamaitechnologies.com (23.35.32.140): icmp_seq=1 ttl=57 time=65.8 ms
64 bytes from a23-35-32-140.deploy.static.akamaitechnologies.com (23.35.32.140): icmp_seq=2 ttl=57 time=61.5 ms
64 bytes from a23-35-32-140.deploy.static.akamaitechnologies.com (23.35.32.140): icmp_seq=3 ttl=57 time=73.1 ms

ping 不同的IP。

回答

ping 后您会获得不同的 IP,因为它www.nseindia.com是通过akamai CDN提供给您的。因此,无论您是从家庭/工作还是从 AWS 服务器执行此操作,您都在ping不同的边缘位置

更重要的是,AWS 的 IP 地址范围是公开的。因此,网站明确阻止 AWS 连接以防止抓取、攻击或其他不需要的访问的情况并不少见。因此,似乎nseindia 正在阻止所有这些 AWS IP 地址。这是一个已知问题,如此处和此处所示,例如。

解决方案不是使用 AWS 或其他流行的供应商(nseindia 也阻止其他供应商)。您可以尝试通过一些商业 VPN、家庭/工作网络或未列入黑名单的东西来代理您的 AWS 请求。可悲的是,这是一种试探性的方法。但您还必须考虑绕过这些限制的潜在法律/道德问题。


以上是python请求库在亚马逊aws中不起作用的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>