【问题标题】:python requests lib is not working in amazon awspython请求库在亚马逊AWS中不起作用
【发布时间】:2020-12-03 07:49:08
【问题描述】:

我正在尝试以下代码:

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 时它没有响应。

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

requests 适用于 google 等其他网站,但不适用于 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。

【问题讨论】:

  • “没有响应”是什么意思?对谁没有回应?你看到有什么例外吗?它只是挂在那里吗?你是什​​么意思“在aws中”,这是在EC2实例上吗?
  • 不响应意味着在此行之后没有输出x = nse.get("https://www.nseindia.com/", headers=headers)request.get 有一个 time_out 值,如果你没有设置任何值,它会等待它收到任何数据。在这种情况下,它不会从该站点接收任何数据,因此它会继续等待。
  • 是的,我的意思是 EC2 aws。
  • 您将响应存储在xEC2。你在PC 上没有做同样的事情。
  • @MrugeshKadia 不,这不是问题 :)

标签: python-3.x amazon-web-services python-requests


【解决方案1】:

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

此外,IP address ranges of AWS为公众所知的。因此,网站明确阻止 AWS 连接以防止抓取、攻击或其他不需要的访问的情况并不少见。因此,nseindia 似乎正在阻止所有这些 AWS IP 地址。这是一个已知问题,例如 herehere

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

【讨论】:

【解决方案2】:

服务器的位置是什么? nseindia 可能不允许印度以外的 IP 地址。 尝试使用任何 VPN 或使用您的浏览器 cookie 来伪造您的位置(不道德)。 此外, Session() 已被弃用。你可以使用 session.Session()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多