-
Notifications
You must be signed in to change notification settings - Fork 2
/
webUtil.py
37 lines (31 loc) · 1.19 KB
/
webUtil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from qgis.core import QgsBlockingNetworkRequest
from qgis.PyQt.QtNetwork import QNetworkRequest
from qgis.PyQt.QtCore import QUrl
def getUrlData(url, data=None, returnBytes=False):
"""Performs a blocking “get” operation on the specified *url* and returns the response,
if *data* is given a "post" is performed.
:param url: the url to fetch
:param data: the data to post as bytes
:param returnBytes: return bytes instead of string if True
:return: the response as a string
"""
bnr = QgsBlockingNetworkRequest()
#print("URL: "+ url)
if not data:
respcode = bnr.get(QNetworkRequest( QUrl(url) ) )
else:
respcode = bnr.post(QNetworkRequest( QUrl(url) ) , data )
if respcode == 0:
response = bnr.reply().content().data()
if returnBytes == False: response = response.decode('utf-8')
else:
raise metaError( bnr.reply().errorString() )
return response
class metaError(Exception):
"""Exception, a error in metadataXML
:param message: a message to pass with the exception
"""
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)