jannewmarch
August 14th, 2008, 12:49 PM
Hi
I often have to connect through an HTTP proxy, and it requires authentication as well. I need to read my comics at work... So I came up with the following which uses the Gnome gconf settings (view/change by gconf-editor). I have tested it with: no proxy; non-auth proxy and auth proxy and it seems to work okay, Platform: Ubuntu 8.0.4, kernel 2.6.24-19, screenlets 0.0.12
import urllib2
import gconf
gconf_client = gconf.client_get_default()
# Equivalent to urllib2.urlopen(url) but uses proxies if specified by GConf
# Call by self.urlopen(url)
def urlopen(self, URL):
# get info from GConf about possible proxies, auth, etc
# requires info to be set in Gnome by e.g. gconf-edit
# We query on need: we won't be making so many queries that we need
# to listen and track changes
use_proxy = self.gconf_client.get_bool("/system/http_proxy/use_http_proxy")
if not use_proxy:
# no proxy, direct connection
print "Direct connection to " + URL
proxy_support = urllib2.ProxyHandler({})
else:
# TODO: check our URL isn't in ignore_hosts list
# not very likely though, and too messy
proxy_host = self.gconf_client.get_string("/system/http_proxy/host")
proxy_port = self.gconf_client.get_int("/system/http_proxy/port")
if proxy_host == None or proxy_port == 0:
raise Exception("GConf: proxy_host and proxy_port cannot be None")
use_auth = self.gconf_client.get_bool("/system/http_proxy/use_authentication")
if not use_auth:
# simple proxy without auth
print "Using simple proxy"
proxy_info = {
'host': proxy_host,
'port': proxy_port
}
proxy_support = urllib2.ProxyHandler(
{'http': "http://%(host)s:%(port)d" % proxy_info}
)
else:
# proxy with authentication
print "Using proxy with auth"
auth_password = self.gconf_client.get_string("/system/http_proxy/authentication_password")
auth_user = self.gconf_client.get_string("/system/http_proxy/authentication_user")
if auth_user == None or auth_password == None:
raise Exception("GConf: authentication_user and authentication_password cannot be None")
# code from Andre Bocchini <lists@andrebocchini.com>
# at http://bytes.com/forum/thread22918.html
proxy_info = {
'user': auth_user,
'pass': auth_password,
'host': proxy_host,
'port': proxy_port
}
proxy_support = urllib2.ProxyHandler(
{'http': "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info}
)
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
return urllib2.urlopen(URL)
I've used this in ComicStripScreenlet and will be putting this into other screenlets (e.g. ClearWeather) that break behind HTTP proxies.
Note: the code does not handle working through an autoproxy proxy.pac setting ("system/proxy/mode" set to "auto" and "system/proxy/autoconfig_url" set). The autoproxy file contains a JavaScript function. There is Python module at http://code.google.com/p/pacparser that can handle these files, but this requires installation of O/S specific C libraries to handle the Spidermonkey Javascript module. It isn't present on my system and I don't want to force installation. So if you use autoproxy, for now duplicate the proxy host/port settings explicitly and this code will ALWAYS go through the proxy.
Cheers
Jan
--
I often have to connect through an HTTP proxy, and it requires authentication as well. I need to read my comics at work... So I came up with the following which uses the Gnome gconf settings (view/change by gconf-editor). I have tested it with: no proxy; non-auth proxy and auth proxy and it seems to work okay, Platform: Ubuntu 8.0.4, kernel 2.6.24-19, screenlets 0.0.12
import urllib2
import gconf
gconf_client = gconf.client_get_default()
# Equivalent to urllib2.urlopen(url) but uses proxies if specified by GConf
# Call by self.urlopen(url)
def urlopen(self, URL):
# get info from GConf about possible proxies, auth, etc
# requires info to be set in Gnome by e.g. gconf-edit
# We query on need: we won't be making so many queries that we need
# to listen and track changes
use_proxy = self.gconf_client.get_bool("/system/http_proxy/use_http_proxy")
if not use_proxy:
# no proxy, direct connection
print "Direct connection to " + URL
proxy_support = urllib2.ProxyHandler({})
else:
# TODO: check our URL isn't in ignore_hosts list
# not very likely though, and too messy
proxy_host = self.gconf_client.get_string("/system/http_proxy/host")
proxy_port = self.gconf_client.get_int("/system/http_proxy/port")
if proxy_host == None or proxy_port == 0:
raise Exception("GConf: proxy_host and proxy_port cannot be None")
use_auth = self.gconf_client.get_bool("/system/http_proxy/use_authentication")
if not use_auth:
# simple proxy without auth
print "Using simple proxy"
proxy_info = {
'host': proxy_host,
'port': proxy_port
}
proxy_support = urllib2.ProxyHandler(
{'http': "http://%(host)s:%(port)d" % proxy_info}
)
else:
# proxy with authentication
print "Using proxy with auth"
auth_password = self.gconf_client.get_string("/system/http_proxy/authentication_password")
auth_user = self.gconf_client.get_string("/system/http_proxy/authentication_user")
if auth_user == None or auth_password == None:
raise Exception("GConf: authentication_user and authentication_password cannot be None")
# code from Andre Bocchini <lists@andrebocchini.com>
# at http://bytes.com/forum/thread22918.html
proxy_info = {
'user': auth_user,
'pass': auth_password,
'host': proxy_host,
'port': proxy_port
}
proxy_support = urllib2.ProxyHandler(
{'http': "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info}
)
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
return urllib2.urlopen(URL)
I've used this in ComicStripScreenlet and will be putting this into other screenlets (e.g. ClearWeather) that break behind HTTP proxies.
Note: the code does not handle working through an autoproxy proxy.pac setting ("system/proxy/mode" set to "auto" and "system/proxy/autoconfig_url" set). The autoproxy file contains a JavaScript function. There is Python module at http://code.google.com/p/pacparser that can handle these files, but this requires installation of O/S specific C libraries to handle the Spidermonkey Javascript module. It isn't present on my system and I don't want to force installation. So if you use autoproxy, for now duplicate the proxy host/port settings explicitly and this code will ALWAYS go through the proxy.
Cheers
Jan
--