当我调用python API时,EC2实例的HTTP超时 [英] HTTP timeout from an EC2 instance when I call a python API
问题描述
我正在尝试从我使用Ubuntu配置的EC2实例中调用 python api .当我在计算机上本地运行文件时,它可以运行,但是当我在EC2实例上运行相同文件时,出现此错误:
I am trying to call a python api from an EC2 instance that I have configured with Ubuntu. When I run the file locally on my computer it works, but when I run the same file on my EC2 instance, I get this error:
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='stats.nba.com', port=443, timeout=20)
这是我的文件.对我的mongodb的调用在EC2实例上有效,但是stats_nba API端点不起作用.
Here is my file. The calls to my mongodb work on the EC2 instance, but the stats_nba API endpoint does not work.
from nba_api.stats.endpoints import commonplayerinfo
import pymongo
from pymongo import MongoClient
import requests
import time
cluster = MongoClient("My Mongo Server")
db = cluster['nba_data']
collection = db["players"]
addCol = db['playerCommonInfo']
players = collection.find({})
ids = []
pastIds = []
for player in players:
ids.append(player['_id'])
previousInfo = addCol.find({})
for prev in previousInfo:
pastIds.append(prev['_id'])
headers = {
'Host': 'stats.nba.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://stats.nba.com/',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
}
print("Getting Player Info...")
for person in ids:
if person not in pastIds:
try:
# the line where the error occurs on my EC2 Instance
player_info = commonplayerinfo.CommonPlayerInfo(player_id=person, headers=headers, timeout=50)
h = player_info.common_player_info.get_dict()['headers']
h[0] = '_id'
d = player_info.common_player_info.get_dict()['data']
dict1 = dict(zip(h, d[0]))
addCol.insert_one(dict1)
print (dict1['DISPLAY_FIRST_LAST'])
time.sleep(6)
except requests.exceptions.ReadTimeout:
print("Read Timeout")
print("Waiting 1 min until retry")
time.sleep(60)
print("finished")
我已在EC2实例上将安全性设置"配置为对HTTP和https流量开放.
I have configured my Security Settings on my EC2 instance to be open to http and https traffic.
我的EC2网络设置是否出现某种类型的错误?我找不到任何指向正确方向的资源.
Is this some type of error with my EC2 network settings? I can't find any resources pointing me in te right direction.
推荐答案
在安全组设置中,未添加具有端口443规则的https.您能否添加https并重试.
In security groups settings , https with port 443 rule is not added. Could you add https and try again.
这篇关于当我调用python API时,EC2实例的HTTP超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!