5

For those who are interested I found the definitive way to set proxy settings from within qgis plugin in a user-transparent way. This is useful if you plan to use webservices with urllib or QwebWiew. Using Qsetting function is possible to read and write user application options setting stored in registry from qgis application. The problem is that registry keys use is not documented, but digging into qgis source is possible to find them and use it in plugin for other purpouse. Here is a block of code to properly set Proxy parameters.

    # procedure to set proxy if needed
    s = QSettings() #getting proxy from qgis options settings
    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)

2 Answers 2

3

To complete the answer from @gustry, you will have to start with the following code:

from PyQt4.QtCore import QUrl
from PyQt4.QtNetwork import QNetworkRequest
from qgis.core import QgsNetworkAccessManager

url = 'http://qgis.org/en/site/'

def urlCallFinished(reply):
    print(reply.readAll())
    reply.deleteLater()

networkAccessManager = QgsNetworkAccessManager.instance()
networkAccessManager.finished.connect(urlCallFinished)

req = QNetworkRequest(QUrl(url))
reply = networkAccessManager.get(req)

For the proxy part, QgsNetworkAccessManager can use QNetworkProxy as stated by the documentation and QGIS already manages it for you ;).

1

You should use QgsNetworkAccessManager provided by QGIS. The proxy is automatically set up for you.

from qgis.core import QgsNetworkAccessManager
network_manager = QgsNetworkAccessManager.instance()

http://qgis.org/api/classQgsNetworkAccessManager.html

1
  • 1
    How is the URL request called with this class and how do I get the response?
    – m13r
    Commented Jun 10, 2016 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.