PyQGIS Proxies Issue – Requests Library Fails to Handle Proxies in PyQGIS

PROXYpyqgisqgis

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.

Best Answer

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.