mirror of
https://github.com/jayofelony/pwnagotchi.git
synced 2025-07-01 18:37:27 -04:00
Version 2.3.8
Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>
This commit is contained in:
@ -3,8 +3,9 @@ import requests
|
||||
import websockets
|
||||
|
||||
from requests.auth import HTTPBasicAuth
|
||||
import asyncio # Add asyncio for async functionality
|
||||
from time import sleep # Add sleep function
|
||||
from time import sleep
|
||||
|
||||
requests.adapters.DEFAULT_RETRIES = 5 # increase retries number
|
||||
|
||||
|
||||
def decode(r, verbose_errors=True):
|
||||
@ -22,8 +23,7 @@ def decode(r, verbose_errors=True):
|
||||
|
||||
|
||||
class Client(object):
|
||||
def __init__(self, hostname='localhost', scheme='http', port=8081, username='user', password='pass',
|
||||
max_websockets=5):
|
||||
def __init__(self, hostname='localhost', scheme='http', port=8081, username='user', password='pass'):
|
||||
self.hostname = hostname
|
||||
self.scheme = scheme
|
||||
self.port = port
|
||||
@ -32,31 +32,46 @@ class Client(object):
|
||||
self.url = "%s://%s:%d/api" % (scheme, hostname, port)
|
||||
self.websocket = "ws://%s:%s@%s:%d/api" % (username, password, hostname, port)
|
||||
self.auth = HTTPBasicAuth(username, password)
|
||||
self.max_websockets = max_websockets
|
||||
self.websocket_semaphore = asyncio.Semaphore(max_websockets)
|
||||
|
||||
# session takes optional argument to pull a sub-dictionary
|
||||
# ex.: "session/wifi", "session/ble"
|
||||
def session(self, sess="session"):
|
||||
r = requests.get("%s/%s" % (self.url, sess), auth=self.auth)
|
||||
def session(self):
|
||||
r = requests.get("%s/session" % self.url, auth=self.auth)
|
||||
return decode(r)
|
||||
|
||||
async def start_websocket(self, consumer):
|
||||
s = "%s/events" % self.websocket
|
||||
# while True:
|
||||
# try:
|
||||
# async with websockets.connect(s, ping_interval=60, ping_timeout=90) as ws:
|
||||
# async for msg in ws:
|
||||
# try:
|
||||
# await consumer(msg)
|
||||
# except Exception as ex:
|
||||
# logging.debug("Error while parsing event (%s)", ex)
|
||||
# except websockets.exceptions.ConnectionClosedError:
|
||||
# logging.debug("Lost websocket connection. Reconnecting...")
|
||||
# except websockets.exceptions.WebSocketException as wex:
|
||||
# logging.debug("Websocket exception (%s)", wex)
|
||||
# except OSError as e:
|
||||
# logging.debug("Websocket OSError exception (%s) with parameter %s", e, s)
|
||||
# except Exception as e:
|
||||
# logging.debug("Other exception (%s) with parameter %s", e, s)
|
||||
|
||||
async with websockets.connect(s, ping_interval=60, ping_timeout=90) as ws:
|
||||
while True:
|
||||
try:
|
||||
async with websockets.connect(s, ping_interval=60, ping_timeout=90) as ws:
|
||||
async for msg in ws:
|
||||
try:
|
||||
await consumer(msg)
|
||||
except Exception as ex:
|
||||
logging.debug("Error while parsing event (%s)", ex)
|
||||
except websockets.ConnectionClosedError:
|
||||
logging.debug("Lost websocket connection. Reconnecting...")
|
||||
except websockets.WebSocketException as wex:
|
||||
logging.debug("Websocket exception (%s)", wex)
|
||||
except websockets.exceptions.ConnectionClosedError:
|
||||
logging.error("Lost websocket connection. Reconnecting...")
|
||||
except websockets.exceptions.WebSocketException as wex:
|
||||
logging.error("Websocket exception (%s)", wex)
|
||||
except OSError as e:
|
||||
logging.error("Websocket OSError exception (%s) with parameter %s", e, s)
|
||||
except Exception as e:
|
||||
logging.exception("Other error while opening websocket (%s) with parameter %s", e, s)
|
||||
logging.error("Other exception (%s) with parameter %s", e, s)
|
||||
|
||||
def run(self, command, verbose_errors=True):
|
||||
for _ in range(0,2):
|
||||
|
Reference in New Issue
Block a user