Switch QGIS proxy settings programatically
Changing these keys changed the settings in both the GUI and the Windows Registry of QGIS 2.14 Essen using your code above in the Python Console:
u'proxy/proxyEnabled', u'proxy/proxyExcludedUrls', u'proxy/proxyHost', u'proxy/proxyPassword', u'proxy/proxyPort', u'proxy/proxyType', u'proxy/proxyUser'
And at the end add:
QSettings.sync()
Problem solved. QgsNetworkAccessManager had to be set to the new values:
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtNetwork import QNetworkRequest, QNetworkProxy
my_settings={"Proxy enabled": u'proxy/proxyEnabled', "Proxy Host ": u'proxy/proxyHost', "Proxy Port": u'proxy/proxyPort'}
fiddler={"Proxy enabled": True, "Proxy Host ": "localhost", "Proxy Port": 8888}
freiburg={"Proxy enabled": True, "Proxy Host ": "192.168.95.165", "Proxy Port": 8080}
aus={"Proxy enabled": False, "Proxy Host ": "192.168.95.165", "Proxy Port": 8080}
current_choice=aus
s = QSettings() #getting proxy from qgis options settings
for key, val in my_settings.iteritems():
settings_key=key
for key2, val2 in current_choice.iteritems():
if key2==settings_key:
settings_val=val2
current_setting = s.value(str(val).decode('unicode-escape'))
s.setValue(unicode(str(val)), settings_val)
s.sync()
proxyEnabled = s.value("proxy/proxyEnabled", "")
proxyType = s.value("proxy/proxyType", "" )
proxyHost = s.value("proxy/proxyHost", "" )
proxyPort = s.value("proxy/proxyPort", "" )
proxyUser = s.value("proxy/proxyUser", "" )
proxyPassword = s.value("proxy/proxyPassword", "" )
proxy = QNetworkProxy()
#setting HttpPtoxy
proxy.setType(QNetworkProxy.HttpProxy)
proxy.setHostName(proxyHost)
proxy.setPort(int(proxyPort))
proxy.setUser(proxyUser)
proxy.setPassword(proxyPassword)
QNetworkProxy.setApplicationProxy(proxy)
network_manager=QgsNetworkAccessManager.instance()
stringlist= ""
network_manager.setupDefaultProxyAndCache ()
network_manager.setFallbackProxyAndExcludes(proxy, stringlist)