#!/usr/bin/python

import dbus
import os

priv = "com.ubuntu.systemservice.setnoproxy"

session_bus = dbus.SessionBus()
system_bus = dbus.SystemBus()

ser = dbus.Interface(system_bus.get_object("com.ubuntu.SystemService","/"),
                     "com.ubuntu.SystemService")

try:
    ser.set_no_proxy("host1,host2")
except dbus.DBusException, e:
    print e
    obj = session_bus.get_object('org.freedesktop.PolicyKit.AuthenticationAgent', '/', False)
    auth = dbus.Interface(obj, "org.freedesktop.PolicyKit.AuthenticationAgent")
    res = auth.ObtainAuthorization(priv,
                                   dbus.UInt32(0),
                                   dbus.UInt32(os.getpid()), 
                                   timeout=300)
    print res
    if res:
        print "setting no_proxy"
        res=ser.set_no_proxy("host1,host2")
        print res
    else:
        print "authentication failure"
