GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Make all klap logging lazy · python-kasa/python-kasa@9ec0799

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • kasa
    • klapprotocol.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎kasa/klapprotocol.py‎

Copy file name to clipboardExpand all lines: kasa/klapprotocol.py
+39-23Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,14 @@ async def perform_handshake1(
164164
session, url, data=payload
165165
)
166166

167-
cookie = self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME)
167+
if _LOGGER.isEnabledFor(logging.DEBUG):
168+
_LOGGER.debug(
169+
"Handshake1 posted at %s. Host is %s, Response status is %s, Request was %s",
170+
datetime.datetime.now(),
171+
self.host,
172+
response_status,
173+
payload and payload.hex(),
174+
)
168175

169176
if response_status != 200:
170177
raise AuthenticationException(
@@ -175,16 +182,16 @@ async def perform_handshake1(
175182
remote_seed = response_data[0:16]
176183
server_hash = response_data[16:]
177184

178-
cookie = self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME)
179-
_LOGGER.debug(
180-
f"Handshake1 posted at {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Response status is {response_status}, Request was {self.local_auth_hash.hex()}"
181-
)
182-
183-
_LOGGER.debug(
184-
"Server remote_seed is: %s, server hash is: %s",
185-
remote_seed.hex(),
186-
server_hash.hex(),
187-
)
185+
if _LOGGER.isEnabledFor(logging.DEBUG):
186+
cookie = self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME)
187+
_LOGGER.debug(
188+
"Handshake1 success at %s. Host is %s, Cookie is %s, Server remote_seed is: %s, server hash is: %s",
189+
datetime.datetime.now(),
190+
self.host,
191+
cookie,
192+
remote_seed.hex(),
193+
server_hash.hex(),
194+
)
188195

189196
local_seed_auth_hash = self._sha256(self._local_seed + self.local_auth_hash)
190197

@@ -242,10 +249,15 @@ async def perform_handshake2(self, session, remote_seed, auth_hash) -> None:
242249
session, url, data=payload
243250
)
244251

245-
cookie = self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME)
246-
_LOGGER.debug(
247-
f"Handshake2 posted {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Response status is {response_status}, Request was {payload!r}"
248-
)
252+
if _LOGGER.isEnabledFor(logging.DEBUG):
253+
_LOGGER.debug(
254+
"Handshake2 posted %s. Host is %s, Session cookie is %s, Response status is %s, Request was %s",
255+
datetime.datetime.now(),
256+
self.host,
257+
self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME),
258+
response_status,
259+
payload.hex(),
260+
)
249261

250262
if response_status != 200:
251263
self.authentication_failed = True
@@ -336,7 +348,8 @@ async def _execute_query(self, request: str, retry_count: int) -> Dict:
336348

337349
except AuthenticationException as auex:
338350
_LOGGER.debug(
339-
f"Unable to complete handshake for device {self.host}, authentication failed"
351+
"Unable to complete handshake for device %s, authentication failed",
352+
self.host,
340353
)
341354
raise auex
342355

@@ -352,10 +365,10 @@ async def _execute_query(self, request: str, retry_count: int) -> Dict:
352365
session, url, params={"seq": seq}, data=payload
353366
)
354367

368+
msg = f"at {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Retry count is {retry_count}, Sequence is {seq}, Response status is {response_status}, Request was {request}"
355369
if response_status != 200:
356-
_LOGGER.error(
357-
f"Query failed after succesful authentication at {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Retry count is {retry_count}, Sequence is {seq}, Response status is {response_status}, Request was {request}"
358-
)
370+
if _LOGGER.isEnabledFor(logging.ERROR):
371+
_LOGGER.error("Query failed after succesful authentication " + msg)
359372
# If we failed with a security error, force a new handshake next time.
360373
if response_status == 403:
361374
self.handshake_done = False
@@ -369,9 +382,8 @@ async def _execute_query(self, request: str, retry_count: int) -> Dict:
369382
% (self.host, response_status, seq)
370383
)
371384
else:
372-
_LOGGER.debug(
373-
f"Query posted at {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Retry count is {retry_count}, Sequence is {seq}, Response status is {response_status}, Request was {request}"
374-
)
385+
if _LOGGER.isEnabledFor(logging.DEBUG):
386+
_LOGGER.debug("Query posted " + msg)
375387

376388
self.handle_cookies(session, url)
377389

@@ -383,7 +395,11 @@ async def _execute_query(self, request: str, retry_count: int) -> Dict:
383395

384396
json_payload = json_loads(decrypted_response)
385397

386-
_LOGGER.debug("%s << %s", self.host, pf(json_payload))
398+
_LOGGER.debug(
399+
"%s << %s",
400+
self.host,
401+
_LOGGER.isEnabledFor(logging.DEBUG) and pf(json_payload),
402+
)
387403

388404
return json_payload
389405

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.