Greetings,
Would you be able to point me towards some example for to check if a connection to a URL is working?
I wish to do this check in "validateDeviceConfigUi"
kind regards,
Z
if not self.myqLogin(username=valuesDict['myqLogin'], password=valuesDict['myqPassword'], brand=valuesDict['openerBrand']):
errorDict['myqLogin'] = u"Login to MyQ server failed, check login, password, and brand"
errorDict['myqPassword'] = u"Login to MyQ server failed, check login, password, and brand"
return (False, valuesDict, errorDict)
url = self.apiData[brand]["service"] + '/api/v4/user/validate'
headers = {
'User-Agent': userAgent,
"BrandId": "2",
"ApiVersion": "4.1",
"Culture": "en",
'MyQApplicationId': self.apiData[brand]["appID"]
}
payload = {
'username': username,
'password': password
}
try:
response = requests.post(url, json=payload, headers=headers)
except requests.exceptions.RequestException as err:
self.logger.debug(u"myqLogin failure, request url = %s" % (url))
self.logger.error(u"myqLogin failure, RequestException: %s" % (str(err)))
self.securityToken = ""
return False
if (response.status_code != requests.codes.ok):
self.logger.debug(u"myqLogin failure, Enum err code %s" % (response.status_coderl))
self.securityToken = ""
return False
## Script to test connection to URL
# Assumes Indigo variable statURL is to be updated with status of URL
# Insert subject URL as TestURL.
from urllib2 import Request, urlopen, URLError, HTTPError
# Enter test URL here
TestUrl = 'http://XXX.XXX.XXX.XXX'
## DEBUGGING
debug = 0 ## Set to 1 to print URL's full response to Indigo log, to 0 for no debug
# Test connection
req = Request(TestUrl)
try:
response = urlopen(req)
except HTTPError, e:
indigo.server.log(str(e.code),"Couldn't fulfill request to server. Error code: ")
indigo.variable.updateValue('statURL', "OFFLINE")
except URLError, e:
indigo.server.log(str(e.reason),"We failed to reach server. Reason: ")
indigo.variable.updateValue('statURL', "OFFLINE")
else:
indigo.server.log("URL connection OK.")
indigo.variable.updateValue('statURL', "ONLINE")
if debug:
indigo.server.log(response.read(), "Response from testURL is:")
Users browsing this forum: No registered users and 5 guests