Proxy authentication support for Twython. Experimental, needs testing - pass a proxy object (username/password/host/port) to the setup method, Twython will try and route everything through the proxy and properly handle things. Headers can now be added to un-authed requests; proxies also work with both authed/un-authed requests.

This commit is contained in:
Ryan McGrath 2009-12-10 03:14:33 -05:00
parent b491faf757
commit f82702e706
2 changed files with 43 additions and 29 deletions

View file

@ -69,6 +69,7 @@ class setup:
self.username = username
self.apiVersion = version
self.proxy = proxy
self.headers = headers
if self.proxy is not None:
self.proxyobj = urllib2.ProxyHandler({'http': 'http://%s:%s@%s:%d' % (self.proxy["username"], self.proxy["password"], self.proxy["host"], self.proxy["port"])})
# Check and set up authentication
@ -81,8 +82,8 @@ class setup:
self.opener = urllib2.build_opener(self.proxyobj, self.handler)
else:
self.opener = urllib2.build_opener(self.handler)
if headers is not None:
self.opener.addheaders = [('User-agent', headers)]
if self.headers is not None:
self.opener.addheaders = [('User-agent', self.headers)]
try:
simplejson.load(self.opener.open("http://api.twitter.com/%d/account/verify_credentials.json" % self.apiVersion))
self.authenticated = True
@ -95,7 +96,7 @@ class setup:
else:
self.opener = urllib2.build_opener()
if self.headers is not None:
self.opener.addheaders = [('User-agent', headers)]
self.opener.addheaders = [('User-agent', self.headers)]
# URL Shortening function huzzah
def shortenURL(self, url_to_shorten, shortener = "http://is.gd/api.php", query = "longurl"):