Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions dlipower/dlipower.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ def name(self, new_name):
class PowerSwitch(object):
""" Powerswitch class to manage the Digital Loggers Web power switch """
__len = 0
login_timeout = 1.0
login_timeout = 2.0
secure_login = False

def __init__(self, userid=None, password=None, hostname=None, timeout=None,
cycletime=None, retries=None, use_https=False):
Expand Down Expand Up @@ -350,10 +351,13 @@ def login(self):
try:
response = self.session.post('%s/login.tgi' % self.base_url, headers=headers, data=data, timeout=self.timeout, verify=False)
except requests.exceptions.ConnectTimeout:
self.secure_login = False
self.session = None
return

if response.status_code == 200:
self.secure_login = True
if 'Set-Cookie' in response.headers:
self.secure_login = True

def load_configuration(self):
""" Return a configuration dictionary """
Expand Down Expand Up @@ -388,9 +392,7 @@ def save_configuration(self):
json.dump(config, file_h, sort_keys=True, indent=4)
file_h.close()
else:
raise DLIPowerException(
'Unable to open configuration file for write'
)
raise DLIPowerException('Unable to open configuration file for write')

def verify(self):
""" Verify we can reach the switch, returns true if ok """
Expand All @@ -407,13 +409,13 @@ def geturl(self, url='index.htm'):
request = None
for i in range(0, self.retries):
try:
if self.secure_login:
if self.secure_login and self.session:
request = self.session.get(full_url, timeout=self.timeout, verify=False)
else:
request = requests.get(full_url, auth=(self.userid, self.password,), timeout=self.timeout, verify=False)
except requests.exceptions.RequestException as e:
logger.warning("Request timed out - %d retries left.", self.retries - i - 1)
logger.debug("Catched exception %s", str(e))
logger.exception("Caught exception %s", str(e))
continue
if request.status_code == 200:
result = request.content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interactions:
<td><input type=\"password\" name=\"Password\" size=\"16\"></td>\n</tr>\n\
<tr class=\"right\">\n<td></td>\n<td><input onClick=\"calcResponse(); return\
\ false;\" type=\"Submit\" value=\"OK\">\n<input type=\"hidden\" name=\"Challenge\"\
\ value=\"ITW8Uw2hPKMJiEsJ\">\n</td></tr>\n</table>\n</form>\n</td></tr></table>\n\
\ value=\"oIeoqVawfRRUw9ux\">\n</td></tr>\n</table>\n</form>\n</td></tr></table>\n\
<script type=\"text/javascript\">\n<!--\ndocument.login.Username.focus();\n\
//-->\n</script>\n<form name=\"secin\" id=\"secin\" action=\"/login.tgi\"\
\ method=\"post\" accept-charset=\"UTF-8\">\n<div style=\"display:none\">\n\
Expand All @@ -45,7 +45,7 @@ interactions:
Content-Type: [text/html; charset="utf-8"]
status: {code: 200, message: OK}
- request:
body: Username=admin&Password=d52fa46c47d34b48f3227b53d1726224
body: Username=admin&Password=0cfa6411f90855e62c7b150d86206af7
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Expand Down Expand Up @@ -87,6 +87,6 @@ interactions:
Cache-Control: ['max-age=0, private, must-revalidate']
Connection: [close]
Content-Type: [text/html; charset="utf-8"]
Set-Cookie: [DLILPC="W/PIS5M5EzInLeMs"; Version=1; Path=/]
Set-Cookie: [DLILPC="LAriM1TIVcuIPO8d"; Version=1; Path=/]
status: {code: 200, message: OK}
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interactions:
<td><input type=\"password\" name=\"Password\" size=\"16\"></td>\n</tr>\n\
<tr class=\"right\">\n<td></td>\n<td><input onClick=\"calcResponse(); return\
\ false;\" type=\"Submit\" value=\"OK\">\n<input type=\"hidden\" name=\"Challenge\"\
\ value=\"FDf75n5UaLGTSrsl\">\n</td></tr>\n</table>\n</form>\n</td></tr></table>\n\
\ value=\"M6YWKz+WEb0MB6hX\">\n</td></tr>\n</table>\n</form>\n</td></tr></table>\n\
<script type=\"text/javascript\">\n<!--\ndocument.login.Username.focus();\n\
//-->\n</script>\n<form name=\"secin\" id=\"secin\" action=\"/login.tgi\"\
\ method=\"post\" accept-charset=\"UTF-8\">\n<div style=\"display:none\">\n\
Expand All @@ -45,7 +45,7 @@ interactions:
Content-Type: [text/html; charset="utf-8"]
status: {code: 200, message: OK}
- request:
body: Username=admin&Password=47c50226746355d3d630354a15b09a5f
body: Username=admin&Password=82ac0c586ae8bf32af8cbd328df19a37
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Expand Down Expand Up @@ -87,6 +87,6 @@ interactions:
Cache-Control: ['max-age=0, private, must-revalidate']
Connection: [close]
Content-Type: [text/html; charset="utf-8"]
Set-Cookie: [DLILPC="VVk3JkJz9Xtze6mF"; Version=1; Path=/]
Set-Cookie: [DLILPC="bKlfB9sEtuYv/uvZ"; Version=1; Path=/]
status: {code: 200, message: OK}
version: 1
Loading