Forums

Errrno Socket Error with urllib

I've just upgraded my account to the "Hacker" account as I want to have a script running that will collect some JSON data from an API I have on another site I host, check it for some conditions and post an update to twitter if they match.

Sometimes the script is making the call to the API correctly, but other times I'm getting a [Errno socket error] [Errno -2] Name or service not known error. It's producing the error every second time so, otherwise it works as expected.

Below is the code. It's running in a virtualenv with urllib, tweepy, and requests[security] installed via pip

from urllib import urlopen
import json
import datetime, time
import tweepy

#Twitter
consumer_key = 'REMOVED'
consumer_secret = 'REMOVED'
access_token = 'REMOVED'
access_token_secret = 'REMOVED'

#last unique id used for tweet
vendor_tracks = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
#Full name for appending to tweet
vendor_name = ['Devrim Kay in the EDZ', 'MIDA Mini-Tool at Devrim Kay', 'Sloane on Titan', 'Failsafe on Nessus', 'Asher Mir on Io', 'Man O War at Asher Mir', 'Tyra', 'Drang at Tyra Karn', 'Commander Zavala at The Tower', 'Lord Shaxx at The Tower', 'Banshee-44 at The Tower', 'Ikora Rey at The Tower', 'Benedict 99-40 at The Tower', 'Lakshmi-2 at The Tower', 'Executor Hideo at The Tower', 'Arach Jalaal at The Tower', 'The Emissary at The Third Spire']
#URL of API call
url = "https://api.vendorengrams.xyz/getVendorDrops?key=REMOVED"

#Function to get JSON
def get_jsonparsed_data(url):
    response = urlopen(url)
    data = str(response.read())
    return json.loads(data)

#Function To Send Tweet
def sendTweet(vendor, id_passed):
    vendor_unique_period = id_passed
    print "Unique Period is: " + str(vendor_unique_period)
    if vendor_unique_period != vendor_tracks[vendor]:
        vendor_tracks[vendor] = vendor_unique_period
        current_time = datetime.datetime.now()
        seconds = current_time.second
        minutes = current_time.minute
        if minutes < 30:
            minutes = 30 - (minutes + 1)
            seconds = 60 - seconds
        else:
            minutes = 60 - (minutes + 1)
            seconds = 60 - seconds
        if seconds == 60:
            seconds = 0
            minutes = minutes + 1
        time_string = str(minutes) + "m "
        if seconds != 0:
            time_string = time_string + str(seconds) + "s"
        mid_part = "300PL gear"
        if vendor == 1 or vendor == 5 or vendor == 7:
            mid_part = "at 300PL"
        tweet = vendor_name[vendor] + " is dropping " + mid_part + " for the next " + time_string
        try:
            auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
            auth.set_access_token(access_token, access_token_secret)
            api = tweepy.API(auth)
            api.update_status(tweet)
        except Exception as e:
            print("TwitterError: " + str(e))

while True:
    try:
        returned_drops = get_jsonparsed_data(url)
        print "Collected Data at " + time.asctime( time.localtime(time.time()))
        for vendor in returned_drops:
            if vendor['type'] == 3 and vendor['verified'] == 1:
                sendTweet(vendor['vendor'], vendor['id'])
                time.sleep(30)
    except Exception as e:
        pass
        print("There was an error: " + str(e))
    time.sleep(180)

which line is the error actually come from? you might try using requests instead of urlopen, it tends to be much easier to use and to debug...

It’s the response = urlopen(url) line that’s causing the error.

I’ll look into switching it to requests though.

The error [Errno -2] Name or service not known normally means that when we asked the DNS system to tell us where the hostname in the URL (which looks like it will always be api.vendorengrams.xyz in your code), we got no response back. That would suggest a DNS issue on the part of the site's owner.

Is the server you're trying to connect to generally pretty reliable? I just did a lookup for their A record on What's my DNS (a third-party site for testing whether DNS configuration is set up properly) and it came back with errors from about 20% of the queries that they made.

That might be it. The server is mine but it’s been a bit more popular than I was expecting so I’m still playing catch-up on handling the excessive loads. I’ll look into writing some extra recovery handling code until I can get the server balanced out.