我打算在AWS EC2实例中使用python请求。
以下是在我的本地开发案例中运行良好的代码:
url = http://m.dcinside.com/board/twice/8072990?recommend=1&page=14
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}
try:
req = requests.get(url, allow_redirects=False, headers=headers)
except Exception as e:
print('requests error: ' + str(e) + ' at: ' + url)
return
但是在AWS EC2实例中,它向我显示了这一点:
requests error: HTTPConnectionPool(host='m.dcinside.com', port=80): Max retries exceeded with url: /board/twice/8072990?recommend=1&page=14 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fdf41d69a20>: Failed to establish a new connection: [Errno 110] Connection timed out',)) at: http://m.dcinside.com/board/twice/8072990?recommend=1&page=14
我的AWS EC2设置如下:
EC2 [这里是python容器。]具有公共DNS,IP,并且端口:8080已打开。
实际上,python容器也是django容器,并且效果很好。
为什么发生此问题?以及我该如何解决?