Forums

TwythonError: Cannot connect to proxy. Socket error: [Errno 111] Connection refused.

Hey,

So I am getting the following error when trying to run my code...

Traceback (most recent call last):
File "follow_users_followers.py", line 30, in <module>
  retweeters = twitter.get_retweeters_ids(id = k)
File "/home/graingerkid/.local/lib/python2.7/site-packages/twython/endpoints.py", line 125, in get_retweeters_ids
  return self.get('statuses/retweeters/ids', params=params)
File "/home/graingerkid/.local/lib/python2.7/site-packages/twython/api.py", line 230, in get
  return self.request(endpoint, params=params, version=version)
File "/home/graingerkid/.local/lib/python2.7/site-packages/twython/api.py", line 224, in request
  content = self._request(url, method=method, params=params, api_call=url)
File "/home/graingerkid/.local/lib/python2.7/site-packages/twython/api.py", line 145, in _request
  raise TwythonError(str(e))

twython.exceptions.TwythonError: Cannot connect to proxy. Socket error: [Errno 111] Connection refused.

Can anyone point me in the right direction to fix this, it is actually making the first call successfully but it then fails on the second.

Here is my code...

from twython import Twython # pip install twython
import time
import csv

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''

twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)

api_calls = 0

user_timeline = twitter.get_user_timeline(screen_name="", count=200, include_retweets=False)
api_calls = api_calls +1

dic = {}

for tweet in user_timeline:
    dic[tweet['id_str']] = tweet['retweet_count']

retweeter_handles = []

for k, v in dic.iteritems():
    if v > 0:
        retweeters = twitter.get_retweeters_ids(id = k)
        api_calls = api_calls +1
        for i in retweeters['ids']:
            retweeter_handles.extend(retweeters['ids'])
            #print retweeters['ids']
            time.sleep(150)

output = twitter.lookup_user(user_id=retweeter_handles[0:100])

username_list=[]

#Loop through the results
for user in output:
    username_list.append(user['screen_name'])

with open("output.csv", "wb") as f:
    for user in username_list:
        writer = csv.writer(f)
        writer.writerows(user)

FYI this runs perfect off my desktop

Thanks in advance

[edit by admin: formatting]

Hi there -- that looks like it's because the site you're trying to access isn't on our whitelist, so it can't be accessed from free accounts -- although you have a paid account now, it looks like you posted this while you had a free one -- is that right?

If it's still not working after the upgrade to a paid account, let me know.

Hey mate. I actually upgraded thinking it would fix the problem but it didn't. I tried a few times before then posting here

One possibility -- are you running it from a console you started before you upgraded? Or from a web app that you haven't reloaded since you upgraded?

I've tried from console and as a scheduled task but no joy

Are you setting a proxy anywhere in your code?

No but I shouldn't have to should I?

Exactly, you shouldn't. But for some reason it's trying to use one: twython.exceptions.TwythonError: Cannot connect to proxy. Socket error: [Errno 111] Connection refused.

Have the errors changed to something subtly different since you upgraded?

Or just to clarify my earlier question -- when you run it from a console, is the console one that you started before you upgraded?

If it's a new one, and it's a bash console, what do you get if you run

echo $http_proxy

...?

I followed what you said and killed all consoles and also logged out/in and it seems to be running now, just another thing though, when you log out and then go back to the console it doesn't show the process running anymore? Does it keep running in the background and how can i get the view of it printing back?

Thanks for all your help, excellent support from you guys :)

In general, your process should carry on running in your console and you should be able to come back to it and see it still running. However, sometimes we need to restart the process that looks after the consoles on one of the console servers and then you'll see that your process stops.