3

I'm developing a Python plugin for QGIS. Requests is widely used for HTTP networking in Python, so that's what I've been sticking with too in my plugin. For me, it works fine, but I've started getting reports from users that they get connection errors when they're behind a proxy. I checked the QGIS docs, and found this:

Plugins should make use of QgsNetworkAccessManager instead of using urllib2/requests/etc... which often fail to use correct proxy settings.

I'm okay with using the Qt/QGIS networking API instead, but can anyone explain why Requests fails here, and is it only in the PyQGIS environment, or Requests fail to pass requests through proxies in general? I haven't managed to find any info on this.

4
  • Can everyone install this plugin?
    – Taras
    Sep 9, 2021 at 18:42
  • I'm not sure this is on topic here - general questions about the requests lib should probably go to their mailing list or stackoverflow
    – Ian Turton
    Sep 10, 2021 at 9:23
  • @Taras yes, it's public Sep 10, 2021 at 22:00
  • Ian, well this is more about using Requests vs the Qt's native networking API in QGIS plugins. Sep 10, 2021 at 22:04

2 Answers 2

3

With requests, you can pass an object to specify proxies e.g https://stackoverflow.com/questions/8287628/proxies-with-python-requests-module. The issue you will hit with QGIS is that users will set proxy through QGIS going through Settings > Options > Network and QGIS will automatically use the configuration through network calls.

Going through QgsNetworkAccessManager path, you will more easily reuse the configuration for proxy when doing your network calls in your code. If you use requests, you will need to retrieve the proxy config with code and then set requests proxy. It will be about adding more boilerplate to do the same job. You will also add a 3rd party dependency in your plugin.

Now, at least, you may better understand the statement

Plugins should make use of QgsNetworkAccessManager instead of using urllib2/requests/etc... which often fail to use correct proxy settings.

2
  • Thanks, Thomas! Now I think I get it. What I was missing is the link to the QGIS proxy settings. So it's not that QgsNeworkAccessManager itself somehow magically finds the correct proxies, but rather that it can use the proxy definition from the QGIS Settings. Sep 12, 2021 at 17:39
  • 1
    You reuse it together with classes to make http network calls that accept the infos from the QgsNetworkAccessManager See answer from gis.stackexchange.com/questions/343126/… to get the relevant classes you need to replace your requests GET or POST. You may also Python tests code for these classes by looking at github.com/qgis/QGIS/tree/master/tests/src/python
    – ThomasG77
    Sep 12, 2021 at 20:12
1

I ran into a similar problem to this and couldn't get the QgsNetworkAccessManager to work. Used this instead:

from PyQt5 import QtNetwork



 

##Proxy Setting via QtNetwork

proxy = QtNetwork.QNetworkProxy()

proxy.setType(QtNetwork.QNetworkProxy.HttpProxy)

proxy.setHostName("proxyexample.com")#without http/https

proxy.setPort(0000)#as integer

QtNetwork.QNetworkProxy.setApplicationProxy(proxy)

Maybe it's also a hint for someone else.

Your Answer

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

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