Wednesday 21 February 2018

pyqgis - Switch QGIS proxy settings programatically



I am using QGIS at two offices with different proxy-servers.


At the moment I have to change the proxy settings manually every time i switch the office.


I was wondering if its possible to change the proxy-settings programatically with PyQGIS? then I could write a plugin to switch between the proxy-settings.


EDIT1:


In the meantime i found a way to change the proxy settings of QGIS but still it's not working.


With this code I can change the settings:


from PyQt4.QtCore import QUrl, QSettings
from PyQt4.QtNetwork import QNetworkRequest, QNetworkProxy
from qgis.core import QgsNetworkAccessManager


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():

#print str(key)+":"+str(val)
settings_key=key
#print str(settings_key)
# Get user defined current setting
for key2, val2 in current_choice.iteritems():
if key2==settings_key:
#print key
#print val
settings_val=val2
current_setting = s.value(str(val).decode('unicode-escape'))

#print str(val).decode('unicode-escape')
#print str(key)+": "+str(current_setting)
s.setValue(unicode(str(val)), settings_val)
s.sync()

# procedure to set proxy if needed

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", "" )
if proxyEnabled == "true": # test if there are proxy settings
proxy = QNetworkProxy()
if proxyType == "DefaultProxy":
proxy.setType(QNetworkProxy.DefaultProxy)
elif proxyType == "Socks5Proxy":
proxy.setType(QNetworkProxy.Socks5Proxy)
elif proxyType == "HttpProxy":

proxy.setType(QNetworkProxy.HttpProxy)
elif proxyType == "HttpCachingProxy":
proxy.setType(QNetworkProxy.HttpCachingProxy)
elif proxyType == "FtpCachingProxy":
proxy.setType(QNetworkProxy.FtpCachingProxy)
proxy.setHostName(proxyHost)
proxy.setPort(int(proxyPort))
proxy.setUser(proxyUser)
proxy.setPassword(proxyPassword)
QNetworkProxy.setApplicationProxy(proxy)


This works so far as I can see the changed settings in the QGIS UI (settings->options).


The settings are also written to the windows registry but the changes won't have any effect until i click the OK button in the QGIS settings dialog.


You can test this by setting the proxy programmatically to some proxy-settings that should prevent QGIS from accessing the internet (e.g. localhost:98765) and try to load and pan through a wms-layer. Any idea what's missing?


Edit2: I just piped the output from qgis to a file and had a look at what is going on when I change the proxy-settings using the GUI:


src/core/qgsnetworkaccessmanager.cpp: 364: (setupDefaultProxyAndCache) [9134ms] setting proxy 3 192.168.95.165:8080 /
src/core/qgsnetworkaccessmanager.cpp: 167: (setFallbackProxyAndExcludes) [0ms] proxy settings: (type:HttpProxy host: 192.168.95.165:8080, user:, password:not set

So there are two functions called (setupDefaultProxyAndCache and setFallbackProxyAndExcludes). Perhaps something like that has to be done when using pyQGIS to change the settings?



Answer




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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...