407 Error with tweepy on Windows 7 64-bit OS


#1

I have a typical code written that I was using to listen to Twitter’s Streaming API, which worked perfectly until the other day. The only thing that has changed is that I changed the password to my computer. Here is the code I’m using:

import urllib2

proxy = urllib2.ProxyHandler({‘http’: ‘http://username:password@1proxy:port’})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)

import tweepy, json, datetime

consumerKey = 'xxxx’
consumerSecret = 'xxxx’
accessKey = 'xxxx’
accessSecret = ‘xxxx’

search = raw_input('Please enter your search term(s): ')

class Listener(tweepy.StreamListener):
def on_data(self, data):
decoded = json.loads(data)
params = (decoded[‘user’][‘screen_name’]
, decoded[‘text’].encode(‘ascii’, ‘ignore’)
, datetime.datetime.utcfromtimestamp((float(decoded[‘timestamp_ms’]))/1000).strftime(’%Y-%m-%d %H:%M:%S’))
print ‘@%s: %s %s’ %(params)
print ''
print ''
return True

def on_error(self, status):
    print status

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessKey, accessSecret)
stream = tweepy.Stream(auth, Listener())
stream.filter(track=[search])

I updated the password in my source code, but it still throws error 407 when it tries to get the URL. Has anything changed with Twitter’s API? Or is there some other reason my proxy would refuse to connect now? Thanks in advance!


#2

I have figured out the error! tweepy uses the httplib library and not urllib2, which is what I was trying to use to set my proxy information. httplib uses the environment variables ‘http_proxy’ and ‘https_proxy’ when looking for that kind of information and therefore those environment variables need to be updated when your password changes. This can be done by following the instructions on this site, but modifying it to change the http_proxy variable and not the PATH variable:


#3

TwitterAPI uses urllib2 (via python-requests) and has proxy server authentication built in. Here is an example.


#4

Thanks, Jonas! I might look into that library as an easier workaround than having to change an environment variable every time my password changes. Thanks again.


#5