【发布时间】: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。
-
您将响应存储在
x中EC2。你在PC上没有做同样的事情。 -
@MrugeshKadia 不,这不是问题 :)
标签: python-3.x amazon-web-services python-requests