[QGIS-Developer] Proxy support in Python Plugins

Richard Duivenvoorde rdmailings at duif.net
Sun Mar 31 03:08:38 PDT 2019


Hi Nils,

IF you use QgsNetworkAccessManager, you get all proxy and authentication
stuff for free with it.
In my plugins I use the Boundless wrapper made by Luigi and Alessandro:

https://github.com/boundlessgeo/lib-qgis-commons/blob/master/qgiscommons2/network/networkaccessmanager.py

https://github.com/rduivenvoorde/pdokservicesplugin/blob/master/pdokgeocoder.py#L30
I just add the Boundless networkaccessmanager to my plugin.

I know Nyall has tried to make it easier for Python devs to create a
thread-save non-synchron requests, but if I am correct that became too
complex/unsafe (Nyall?)
What was added though (QGIS 3.6) was:

QgsNetworkAccessManager.blockingGet

see:
https://qgis.org/pyqgis/3.6/core/QgsNetworkAccessManager.html#qgis.core.QgsNetworkAccessManager.blockingGet

Or in use:
https://github.com/rduivenvoorde/TimeManager/blob/guess_wmst_timeframes/raster/wmstlayer.py#L89

I think this can be used in your use case.

Off course you can also create your own use of it, but then indeed you
have to make yourself very familiar with how Qt handles network requests
etc.

Hope this helps.

Regards,

Richard

On 30/03/2019 17.33, Nils Nolde wrote:
> Hi,
> 
> I want to upgrade ORS Tools to not work with requests anymore to fetch
> GET and POST queries. However, I never used QgsNetworkAccessManager and
> have little knowledge about networking in general. I tried mostly
> Boundless Geo's NetworkAccessManager wrapper. Couldn't make it work for
> my plugin. When I set a free online Proxy in QGIS settings, all other
> apps/plugin in QGIS could access the internet, my plugin couldn't, it
> would timeout.
> 
> I did smth akin to:
> 
> nam = networkaccessmanager.NetworkAccessManager()
> 
> base_url = 'https://nominatim.openstreetmap.org/reverse'
> params = {'lat': point.y(), 'lon': point.x(), 'format': 'json'}
> url = base_url + '?' + urlencode(params)
> 
> response, content = self.nam.request(url=url, headers={'User-Agent':
> user_agent}, blocking=False)
> 
> Figuring it would automatically use the Proxy settings in QGIS. It
> doesn't, it seems. So 3 questions:
> 
> 1. How do I make my code use the QGIS proxy settings?
> 
> 2. How do I debug, i.e. how do I check that the request actually used
> the proxy settings? Couldn't find much, even on the native Qt parent.
> 
> 3. Is there a way to use non-blocking requests in native
> QgsNetworkAccessManager? Actually it's not so important, since batch
> requests are being done within the Processing framework (assuming that
> won't affect the main thread..), it's more informational.
> 
> I did some research, but as I don't find a terrible amount of time
> lately to maintain all the open source projects, I was hoping to get
> some quick answer from someone. I'm sure I'm missing smth really
> obvious. Sorry, if it really is obvious and I just can't see it.. Also,
> a reference to an existing plugin would suffice.
> 
> Many thanks
> Nils
> 
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer



More information about the QGIS-Developer mailing list