Twitter Python program giving connection error
Question: I am working on a Twitter data mining application in Python, using the modules
twitter and redis, based upon the book "Mining the Social Web" by Matthew
A.Russell. Whenever I try to get the information of friends and followers
using the redis module, it gives me the following error:

    
    
    raise ConnectionError(self._error_message(e))
    ConnectionError: Error 10061 connecting localhost:6379. No connection could be made because the target machine actively refused it.
    

When I don't use Redis, I can get the ids of all friends and followers. But I
require Redis to perform certain analysis stuff on the data. My source code is
as follows:

    
    
    import json
    import locale
    import redis
    from prettytable import PrettyTable
    
    # Pretty printing numbers
    from twitter__util import pp 
    
    # These functions create consistent keys from 
    # screen names and user id values
    from twitter__util import getRedisIdByScreenName 
    from twitter__util import getRedisIdByUserId
    
    SCREEN_NAME = "timoreilly"
    
    locale.setlocale(locale.LC_ALL, '')
    
    def calculate():
        r = redis.Redis()  # Default connection settings on localhost
    
        follower_ids = list(r.smembers(getRedisIdByScreenName(SCREEN_NAME,
                            'follower_ids')))
    
        followers = r.mget([getRedisIdByUserId(follower_id, 'info.json')
                           for follower_id in follower_ids])
        followers = [json.loads(f) for f in followers if f is not None]
    
        freqs = {}
        for f in followers:
            cnt = f['followers_count']
            if not freqs.has_key(cnt):
                freqs[cnt] = []
    
            freqs[cnt].append({'screen_name': f['screen_name'], 'user_id': f['id']})
    
        # It could take a few minutes to calculate freqs, so store a snapshot for later use
    
        r.set(getRedisIdByScreenName(SCREEN_NAME, 'follower_freqs'),
              json.dumps(freqs))
    
        keys = freqs.keys()
        keys.sort()
    
        print 'The top 10 followers from the sample:'
    
        fields = ['Date', 'Count']
        pt = PrettyTable(fields=fields)
        [pt.set_field_align(f, 'l') for f in fields]
    
        for (user, freq) in reversed([(user['screen_name'], k) for k in keys[-10:]
                                        for user in freqs[k]]):
            pt.add_row([user, pp(freq)])
    
        pt.printt()
    
        all_freqs = [k for k in keys for user in freqs[k]]
        avg = reduce(lambda x, y: x + y, all_freqs) / len(all_freqs)
    
        print "\nThe average number of followers for %s's followers: %s" \
            % (SCREEN_NAME, pp(avg))
    
    # psyco can only compile functions, so wrap code in a function
    
    try:
        import psyco
        psyco.bind(calculate)
    except ImportError, e:
        pass  # psyco not installed
    
    calculate()
    

Any and all help would be really appreciated. Thanks!


Answer: That's the port that Redis runs on. If I were you, I'd double-check that the
Redis server is running on the correct port and interface. To test that,
ensure that you've a telnet client installed and do the following:

    
    
    $ telnet localhost 6379
    

If that fails, then the Redis server isn't actually running where the Redis
client library expects it to be by default, in which case, you'll have to
provide the client with the correct details in the constructor arguments.

