HTTP timeout from an EC2 instance when I call a python API

可紊 提交于 2020-05-30 19:02:51

问题


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)

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")

I have configured my Security Settings on my EC2 instance to be open to http and https traffic.

Is this some type of error with my EC2 network settings? I can't find any resources pointing me in te right direction.


回答1:


In security groups settings , https with port 443 rule is not added. Could you add https and try again.



来源:https://stackoverflow.com/questions/58894521/http-timeout-from-an-ec2-instance-when-i-call-a-python-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!