summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorengn33r <engn33r@users.noreply.github.com>2023-04-08 00:00:00 +0000
committerengn33r <engn33r@users.noreply.github.com>2023-04-08 00:00:00 +0000
commitc6a445f2b98b4f80befcd5260da82d157e3fdabf (patch)
tree72acc7602e9ccd389d7018cf3c87403ce801f69c
parentf85ae1fa2ad950469826187e3965f40d90ebb844 (diff)
downloadwebsocket-client-c6a445f2b98b4f80befcd5260da82d157e3fdabf.tar.gz
use str.format() from PEP 3101
-rw-r--r--websocket/_abnf.py4
-rw-r--r--websocket/_app.py16
-rw-r--r--websocket/_cookiejar.py2
-rw-r--r--websocket/_core.py2
-rw-r--r--websocket/_handshake.py24
-rw-r--r--websocket/_http.py8
-rw-r--r--websocket/_url.py2
-rwxr-xr-xwebsocket/_wsdump.py4
8 files changed, 31 insertions, 31 deletions
diff --git a/websocket/_abnf.py b/websocket/_abnf.py
index 2e5ad97..9074d40 100644
--- a/websocket/_abnf.py
+++ b/websocket/_abnf.py
@@ -159,7 +159,7 @@ class ABNF:
raise WebSocketProtocolException("rsv is not implemented, yet")
if self.opcode not in ABNF.OPCODES:
- raise WebSocketProtocolException("Invalid opcode %r", self.opcode)
+ raise WebSocketProtocolException("Invalid opcode {opcode}".format(opcode=self.opcode))
if self.opcode == ABNF.OPCODE_PING and not self.fin:
raise WebSocketProtocolException("Invalid ping frame.")
@@ -175,7 +175,7 @@ class ABNF:
code = 256 * self.data[0] + self.data[1]
if not self._is_valid_close_status(code):
- raise WebSocketProtocolException("Invalid close opcode %r", code)
+ raise WebSocketProtocolException("Invalid close opcode {opcode}".format(opcode=code))
@staticmethod
def _is_valid_close_status(code: int) -> bool:
diff --git a/websocket/_app.py b/websocket/_app.py
index 831e0dd..c3c1b38 100644
--- a/websocket/_app.py
+++ b/websocket/_app.py
@@ -54,11 +54,11 @@ class DispatcherBase:
def reconnect(self, seconds, reconnector):
try:
- _logging.info("reconnect() - retrying in {} seconds [{} frames in stack]".format(seconds, len(inspect.stack())))
+ _logging.info("reconnect() - retrying in {seconds_count} seconds [{frame_count} frames in stack]".format(seconds_count=seconds, frame_count=len(inspect.stack())))
time.sleep(seconds)
reconnector(reconnecting=True)
except KeyboardInterrupt as e:
- _logging.info("User exited {}".format(e))
+ _logging.info("User exited {err}".format(err=e))
class Dispatcher(DispatcherBase):
@@ -273,7 +273,7 @@ class WebSocketApp:
_logging.debug("Sending ping")
self.sock.ping(self.ping_payload)
except Exception as e:
- _logging.debug("Failed to send ping: {}".format(e))
+ _logging.debug("Failed to send ping: {err}".format(err=e))
def run_forever(self, sockopt=None, sslopt=None,
ping_interval=0, ping_timeout=None,
@@ -469,12 +469,12 @@ class WebSocketApp:
raise
if reconnect:
- _logging.info("{} - reconnect".format(e))
+ _logging.info("{err} - reconnect".format(err=e))
if custom_dispatcher:
- _logging.debug("Calling custom dispatcher reconnect [{} frames in stack]".format(len(inspect.stack())))
+ _logging.debug("Calling custom dispatcher reconnect [{frame_count} frames in stack]".format(frame_count=len(inspect.stack())))
dispatcher.reconnect(reconnect, setSock)
else:
- _logging.error("{} - goodbye".format(e))
+ _logging.error("{err} - goodbye".format(err=e))
teardown()
custom_dispatcher = bool(dispatcher)
@@ -483,7 +483,7 @@ class WebSocketApp:
setSock()
if not custom_dispatcher and reconnect:
while self.keep_running:
- _logging.debug("Calling dispatcher reconnect [{} frames in stack]".format(len(inspect.stack())))
+ _logging.debug("Calling dispatcher reconnect [{frame_count} frames in stack]".format(frame_count=len(inspect.stack())))
dispatcher.reconnect(reconnect, setSock)
return self.has_errored
@@ -522,6 +522,6 @@ class WebSocketApp:
callback(self, *args)
except Exception as e:
- _logging.error("error from callback {}: {}".format(callback, e))
+ _logging.error("error from callback {callback}: {err}".format(callback=callback, err=e))
if self.on_error:
self.on_error(self, e)
diff --git a/websocket/_cookiejar.py b/websocket/_cookiejar.py
index 5476d1d..7d26eae 100644
--- a/websocket/_cookiejar.py
+++ b/websocket/_cookiejar.py
@@ -60,5 +60,5 @@ class SimpleCookieJar:
return "; ".join(filter(
None, sorted(
- ["%s=%s" % (k, v.value) for cookie in filter(None, cookies) for k, v in cookie.items()]
+ ["{key}={value}".format(key=k, value=v.value) for cookie in filter(None, cookies) for k, v in cookie.items()]
)))
diff --git a/websocket/_core.py b/websocket/_core.py
index 1d68829..6b51eda 100644
--- a/websocket/_core.py
+++ b/websocket/_core.py
@@ -411,7 +411,7 @@ class WebSocket:
# handle error:
# 'NoneType' object has no attribute 'opcode'
raise WebSocketProtocolException(
- "Not a valid frame %s" % frame)
+ "Not a valid frame {frame}".format(frame=frame))
elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT):
self.cont_frame.validate(frame)
self.cont_frame.add(frame)
diff --git a/websocket/_handshake.py b/websocket/_handshake.py
index fc8f5a5..87a20a7 100644
--- a/websocket/_handshake.py
+++ b/websocket/_handshake.py
@@ -74,39 +74,39 @@ def _pack_hostname(hostname):
def _get_handshake_headers(resource, url, host, port, options):
headers = [
- "GET %s HTTP/1.1" % resource,
+ "GET {resource} HTTP/1.1".format(resource=resource),
"Upgrade: websocket"
]
if port == 80 or port == 443:
hostport = _pack_hostname(host)
else:
- hostport = "%s:%d" % (_pack_hostname(host), port)
+ hostport = "{h}:{p}".format(h=_pack_hostname(host), p=port)
if options.get("host"):
- headers.append("Host: %s" % options["host"])
+ headers.append("Host: {h}".format(h=options["host"]))
else:
- headers.append("Host: %s" % hostport)
+ headers.append("Host: {hp}".format(hp=hostport))
# scheme indicates whether http or https is used in Origin
# The same approach is used in parse_url of _url.py to set default port
scheme, url = url.split(":", 1)
if not options.get("suppress_origin"):
if "origin" in options and options["origin"] is not None:
- headers.append("Origin: %s" % options["origin"])
+ headers.append("Origin: {origin}".format(origin=options["origin"]))
elif scheme == "wss":
- headers.append("Origin: https://%s" % hostport)
+ headers.append("Origin: https://{hp}".format(hp=hostport))
else:
- headers.append("Origin: http://%s" % hostport)
+ headers.append("Origin: http://{hp}".format(hp=hostport))
key = _create_sec_websocket_key()
# Append Sec-WebSocket-Key & Sec-WebSocket-Version if not manually specified
if not options.get('header') or 'Sec-WebSocket-Key' not in options['header']:
- headers.append("Sec-WebSocket-Key: %s" % key)
+ headers.append("Sec-WebSocket-Key: {key}".format(key=key))
else:
key = options['header']['Sec-WebSocket-Key']
if not options.get('header') or 'Sec-WebSocket-Version' not in options['header']:
- headers.append("Sec-WebSocket-Version: %s" % VERSION)
+ headers.append("Sec-WebSocket-Version: {version}".format(version=VERSION))
if not options.get('connection'):
headers.append('Connection: Upgrade')
@@ -115,7 +115,7 @@ def _get_handshake_headers(resource, url, host, port, options):
subprotocols = options.get("subprotocols")
if subprotocols:
- headers.append("Sec-WebSocket-Protocol: %s" % ",".join(subprotocols))
+ headers.append("Sec-WebSocket-Protocol: {protocols}".format(protocols=",".join(subprotocols)))
header = options.get("header")
if header:
@@ -133,7 +133,7 @@ def _get_handshake_headers(resource, url, host, port, options):
cookie = "; ".join(filter(None, [server_cookie, client_cookie]))
if cookie:
- headers.append("Cookie: %s" % cookie)
+ headers.append("Cookie: {cookie}".format(cookie=cookie))
headers.append("")
headers.append("")
@@ -145,7 +145,7 @@ def _get_resp_headers(sock, success_statuses=SUCCESS_STATUSES):
status, resp_headers, status_message = read_headers(sock)
if status not in success_statuses:
response_body = sock.recv(int(resp_headers['content-length'])) # read the body of the HTTP error message response and include it in the exception
- raise WebSocketBadStatusException("Handshake status {status} {message} -+-+- {headers} -+-+- {body}".format(status = status, message = status_message, headers = resp_headers, body = response_body), status, status_message, resp_headers, response_body)
+ raise WebSocketBadStatusException("Handshake status {status} {message} -+-+- {headers} -+-+- {body}".format(status=status, message=status_message, headers=resp_headers, body=response_body), status, status_message, resp_headers, response_body)
return status, resp_headers
diff --git a/websocket/_http.py b/websocket/_http.py
index 17d3f8a..1a753b1 100644
--- a/websocket/_http.py
+++ b/websocket/_http.py
@@ -275,8 +275,8 @@ def _ssl_socket(sock, user_sslopt, hostname):
def _tunnel(sock, host, port, auth):
debug("Connecting proxy...")
- connect_header = "CONNECT %s:%d HTTP/1.1\r\n" % (host, port)
- connect_header += "Host: %s:%d\r\n" % (host, port)
+ connect_header = "CONNECT {h}:{p} HTTP/1.1\r\n".format(h=host, p=port)
+ connect_header += "Host: {h}:{p}\r\n".format(h=host, p=port)
# TODO: support digest auth.
if auth and auth[0]:
@@ -284,7 +284,7 @@ def _tunnel(sock, host, port, auth):
if auth[1]:
auth_str += ":" + auth[1]
encoded_str = base64encode(auth_str.encode()).strip().decode().replace('\n', '')
- connect_header += "Proxy-Authorization: Basic %s\r\n" % encoded_str
+ connect_header += "Proxy-Authorization: Basic {str}\r\n".format(str=encoded_str)
connect_header += "\r\n"
dump("request header", connect_header)
@@ -297,7 +297,7 @@ def _tunnel(sock, host, port, auth):
if status != 200:
raise WebSocketProxyException(
- "failed CONNECT via proxy status: %r" % status)
+ "failed CONNECT via proxy status: {status}".format(status=status))
return sock
diff --git a/websocket/_url.py b/websocket/_url.py
index 2d3d265..8348b5f 100644
--- a/websocket/_url.py
+++ b/websocket/_url.py
@@ -59,7 +59,7 @@ def parse_url(url):
if not port:
port = 443
else:
- raise ValueError("scheme %s is invalid" % scheme)
+ raise ValueError("scheme {scheme} is invalid".format(scheme=scheme))
if parsed.path:
resource = parsed.path
diff --git a/websocket/_wsdump.py b/websocket/_wsdump.py
index 860ac34..757c359 100755
--- a/websocket/_wsdump.py
+++ b/websocket/_wsdump.py
@@ -160,7 +160,7 @@ def main():
except websocket.WebSocketException:
return websocket.ABNF.OPCODE_CLOSE, None
if not frame:
- raise websocket.WebSocketException("Not a valid frame %s" % frame)
+ raise websocket.WebSocketException("Not a valid frame {frame}".format(frame=frame))
elif frame.opcode in OPCODE_DATA:
return frame.opcode, frame.data
elif frame.opcode == websocket.ABNF.OPCODE_CLOSE:
@@ -193,7 +193,7 @@ def main():
data = repr(data)
if args.verbose:
- msg = "%s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data)
+ msg = "{opcode}: {data}".format(opcode=websocket.ABNF.OPCODE_MAP.get(opcode), data=data)
else:
msg = data