drop python<3.7 support
Filter all code over `pyupgrade --py38-plus`. Signed-off-by: Tomasz Kłoczko <kloczek@github.com>
This commit is contained in:
parent
99f83ca083
commit
042c95ce95
|
|
@ -29,7 +29,7 @@ from websockify import token_plugins
|
|||
from websockify import auth_plugins
|
||||
|
||||
|
||||
class FakeSocket(object):
|
||||
class FakeSocket:
|
||||
def __init__(self, data=b''):
|
||||
self._data = data
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ class FakeSocket(object):
|
|||
return StringIO(self._data.decode('latin_1'))
|
||||
|
||||
|
||||
class FakeServer(object):
|
||||
class FakeServer:
|
||||
class EClose(Exception):
|
||||
pass
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ class FakeServer(object):
|
|||
|
||||
class ProxyRequestHandlerTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super(ProxyRequestHandlerTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self.handler = websocketproxy.ProxyRequestHandler(
|
||||
FakeSocket(), "127.0.0.1", FakeServer())
|
||||
self.handler.path = "https://localhost:6080/websockify?token=blah"
|
||||
|
|
@ -69,7 +69,7 @@ class ProxyRequestHandlerTestCase(unittest.TestCase):
|
|||
|
||||
def tearDown(self):
|
||||
patch.stopall()
|
||||
super(ProxyRequestHandlerTestCase, self).tearDown()
|
||||
super().tearDown()
|
||||
|
||||
def test_get_target(self):
|
||||
class TestPlugin(token_plugins.BasePlugin):
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
""" Unit tests for websocketserver """
|
||||
import unittest
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ def raise_oserror(*args, **kwargs):
|
|||
raise OSError('fake error')
|
||||
|
||||
|
||||
class FakeSocket(object):
|
||||
class FakeSocket:
|
||||
def __init__(self, data=b''):
|
||||
self._data = data
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ class FakeSocket(object):
|
|||
|
||||
class WebSockifyRequestHandlerTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super(WebSockifyRequestHandlerTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self.tmpdir = tempfile.mkdtemp('-websockify-tests')
|
||||
# Mock this out cause it screws tests up
|
||||
patch('os.chdir').start()
|
||||
|
|
@ -68,7 +68,7 @@ class WebSockifyRequestHandlerTestCase(unittest.TestCase):
|
|||
"""Called automatically after each test."""
|
||||
patch.stopall()
|
||||
os.rmdir(self.tmpdir)
|
||||
super(WebSockifyRequestHandlerTestCase, self).tearDown()
|
||||
super().tearDown()
|
||||
|
||||
def _get_server(self, handler_class=websockifyserver.WebSockifyRequestHandler,
|
||||
**kwargs):
|
||||
|
|
@ -101,7 +101,7 @@ class WebSockifyRequestHandlerTestCase(unittest.TestCase):
|
|||
|
||||
class WebSockifyServerTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super(WebSockifyServerTestCase, self).setUp()
|
||||
super().setUp()
|
||||
self.tmpdir = tempfile.mkdtemp('-websockify-tests')
|
||||
# Mock this out cause it screws tests up
|
||||
patch('os.chdir').start()
|
||||
|
|
@ -110,7 +110,7 @@ class WebSockifyServerTestCase(unittest.TestCase):
|
|||
"""Called automatically after each test."""
|
||||
patch.stopall()
|
||||
os.rmdir(self.tmpdir)
|
||||
super(WebSockifyServerTestCase, self).tearDown()
|
||||
super().tearDown()
|
||||
|
||||
def _get_server(self, handler_class=websockifyserver.WebSockifyRequestHandler,
|
||||
**kwargs):
|
||||
|
|
@ -181,7 +181,7 @@ class WebSockifyServerTestCase(unittest.TestCase):
|
|||
sock, '127.0.0.1')
|
||||
|
||||
def test_do_handshake_no_ssl(self):
|
||||
class FakeHandler(object):
|
||||
class FakeHandler:
|
||||
CALLED = False
|
||||
def __init__(self, *args, **kwargs):
|
||||
type(self).CALLED = True
|
||||
|
|
@ -256,7 +256,7 @@ class WebSockifyServerTestCase(unittest.TestCase):
|
|||
def test_do_handshake_ssl_sets_ciphers(self):
|
||||
test_ciphers = 'TEST-CIPHERS-1:TEST-CIPHER-2'
|
||||
|
||||
class FakeHandler(object):
|
||||
class FakeHandler:
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
|
@ -291,7 +291,7 @@ class WebSockifyServerTestCase(unittest.TestCase):
|
|||
def test_do_handshake_ssl_sets_opions(self):
|
||||
test_options = 0xCAFEBEEF
|
||||
|
||||
class FakeHandler(object):
|
||||
class FakeHandler:
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
|
@ -302,7 +302,7 @@ class WebSockifyServerTestCase(unittest.TestCase):
|
|||
def fake_select(rlist, wlist, xlist, timeout=None):
|
||||
return ([sock], [], [])
|
||||
|
||||
class fake_create_default_context(object):
|
||||
class fake_create_default_context:
|
||||
OPTIONS = 0
|
||||
def __init__(self, purpose):
|
||||
self.verify_mode = None
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ class AuthenticationError(Exception):
|
|||
if log_msg is None:
|
||||
log_msg = response_msg
|
||||
|
||||
super().__init__('%s %s' % (self.code, log_msg))
|
||||
super().__init__('{} {}'.format(self.code, log_msg))
|
||||
|
||||
|
||||
class InvalidOriginError(AuthenticationError):
|
||||
|
|
@ -64,7 +64,7 @@ class BasicHTTPAuth():
|
|||
self.demand_auth()
|
||||
|
||||
def validate_creds(self, username, password):
|
||||
if '%s:%s' % (username, password) == self.src:
|
||||
if '{}:{}'.format(username, password) == self.src:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ class WebsockifySysLogHandler(handlers.SysLogHandler):
|
|||
if self.unixsocket:
|
||||
try:
|
||||
self.socket.send(msg)
|
||||
except socket.error:
|
||||
except OSError:
|
||||
self._connect_unixsocket(self.address)
|
||||
self.socket.send(msg)
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class ReadOnlyTokenFile(BasePlugin):
|
|||
for line in [l.strip() for l in open(f).readlines()]:
|
||||
if line and not line.startswith('#'):
|
||||
try:
|
||||
tok, target = re.split(':\s', line)
|
||||
tok, target = re.split(r':\s', line)
|
||||
self._targets[tok] = target.strip().rsplit(':', 1)
|
||||
except ValueError:
|
||||
logger.error("Syntax error in %s on line %d" % (self.source, index))
|
||||
|
|
@ -301,7 +301,7 @@ class TokenRedis(BasePlugin):
|
|||
else:
|
||||
logger.error("Unable to parse token: %s" % responseStr)
|
||||
return None
|
||||
logger.debug("host: %s, port: %s" % (host, port))
|
||||
logger.debug("host: {}, port: {}".format(host, port))
|
||||
return [host, port]
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class WebSocketWantReadError(ssl.SSLWantReadError):
|
|||
class WebSocketWantWriteError(ssl.SSLWantWriteError):
|
||||
pass
|
||||
|
||||
class WebSocket(object):
|
||||
class WebSocket:
|
||||
"""WebSocket protocol socket like class.
|
||||
|
||||
This provides access to the WebSocket protocol by behaving much
|
||||
|
|
@ -451,13 +451,13 @@ class WebSocket(object):
|
|||
self._queue_str("HTTP/1.1 %d %s\r\n" % (code, message))
|
||||
|
||||
def send_header(self, keyword, value):
|
||||
self._queue_str("%s: %s\r\n" % (keyword, value))
|
||||
self._queue_str("{}: {}\r\n".format(keyword, value))
|
||||
|
||||
def end_headers(self):
|
||||
self._queue_str("\r\n")
|
||||
|
||||
def send_request(self, type, path):
|
||||
self._queue_str("%s %s HTTP/1.1\r\n" % (type.upper(), path))
|
||||
self._queue_str("{} {} HTTP/1.1\r\n".format(type.upper(), path))
|
||||
|
||||
def ping(self, data=b''):
|
||||
"""Write a ping message to the WebSocket
|
||||
|
|
|
|||
|
|
@ -90,11 +90,11 @@ Traffic Legend:
|
|||
|
||||
# Connect to the target
|
||||
if self.server.wrap_cmd:
|
||||
msg = "connecting to command: '%s' (port %s)" % (" ".join(self.server.wrap_cmd), self.server.target_port)
|
||||
msg = "connecting to command: '{}' (port {})".format(" ".join(self.server.wrap_cmd), self.server.target_port)
|
||||
elif self.server.unix_target:
|
||||
msg = "connecting to unix socket: %s" % self.server.unix_target
|
||||
else:
|
||||
msg = "connecting to: %s:%s" % (
|
||||
msg = "connecting to: {}:{}".format(
|
||||
self.server.target_host, self.server.target_port)
|
||||
|
||||
if self.server.ssl_target:
|
||||
|
|
@ -198,7 +198,7 @@ Traffic Legend:
|
|||
if cqueue or c_pend: wlist.append(self.request)
|
||||
try:
|
||||
ins, outs, excepts = select.select(rlist, wlist, [], 1)
|
||||
except (select.error, OSError):
|
||||
except OSError:
|
||||
exc = sys.exc_info()[1]
|
||||
if hasattr(exc, 'errno'):
|
||||
err = exc.errno
|
||||
|
|
@ -352,22 +352,22 @@ class WebSocketProxy(websockifyserver.WebSockifyServer):
|
|||
# Need to call wrapped command after daemonization so we can
|
||||
# know when the wrapped command exits
|
||||
if self.wrap_cmd:
|
||||
dst_string = "'%s' (port %s)" % (" ".join(self.wrap_cmd), self.target_port)
|
||||
dst_string = "'{}' (port {})".format(" ".join(self.wrap_cmd), self.target_port)
|
||||
elif self.unix_target:
|
||||
dst_string = self.unix_target
|
||||
else:
|
||||
dst_string = "%s:%s" % (self.target_host, self.target_port)
|
||||
dst_string = "{}:{}".format(self.target_host, self.target_port)
|
||||
|
||||
if self.listen_fd != None:
|
||||
src_string = "inetd"
|
||||
else:
|
||||
src_string = "%s:%s" % (self.listen_host, self.listen_port)
|
||||
src_string = "{}:{}".format(self.listen_host, self.listen_port)
|
||||
|
||||
if self.token_plugin:
|
||||
msg = " - proxying from %s to targets generated by %s" % (
|
||||
msg = " - proxying from {} to targets generated by {}".format(
|
||||
src_string, type(self.token_plugin).__name__)
|
||||
else:
|
||||
msg = " - proxying from %s to %s" % (
|
||||
msg = " - proxying from {} to {}".format(
|
||||
src_string, dst_string)
|
||||
|
||||
if self.ssl_target:
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ for mod, msg in [('ssl', 'TLS/SSL/wss is disabled'),
|
|||
globals()[mod] = __import__(mod)
|
||||
except ImportError:
|
||||
globals()[mod] = None
|
||||
print("WARNING: no '%s' module, %s" % (mod, msg))
|
||||
print("WARNING: no '{}' module, {}".format(mod, msg))
|
||||
|
||||
if sys.platform == 'win32':
|
||||
# make sockets pickle-able/inheritable
|
||||
|
|
@ -87,7 +87,7 @@ class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHa
|
|||
super().__init__(req, addr, server)
|
||||
|
||||
def log_message(self, format, *args):
|
||||
self.logger.info("%s - - [%s] %s" % (self.client_address[0], self.log_date_time_string(), format % args))
|
||||
self.logger.info("{} - - [{}] {}".format(self.client_address[0], self.log_date_time_string(), format % args))
|
||||
|
||||
#
|
||||
# WebSocketRequestHandler logging/output functions
|
||||
|
|
@ -102,17 +102,17 @@ class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHa
|
|||
def msg(self, msg, *args, **kwargs):
|
||||
""" Output message with handler_id prefix. """
|
||||
prefix = "% 3d: " % self.handler_id
|
||||
self.logger.log(logging.INFO, "%s%s" % (prefix, msg), *args, **kwargs)
|
||||
self.logger.log(logging.INFO, "{}{}".format(prefix, msg), *args, **kwargs)
|
||||
|
||||
def vmsg(self, msg, *args, **kwargs):
|
||||
""" Same as msg() but as debug. """
|
||||
prefix = "% 3d: " % self.handler_id
|
||||
self.logger.log(logging.DEBUG, "%s%s" % (prefix, msg), *args, **kwargs)
|
||||
self.logger.log(logging.DEBUG, "{}{}".format(prefix, msg), *args, **kwargs)
|
||||
|
||||
def warn(self, msg, *args, **kwargs):
|
||||
""" Same as msg() but as warning. """
|
||||
prefix = "% 3d: " % self.handler_id
|
||||
self.logger.log(logging.WARN, "%s%s" % (prefix, msg), *args, **kwargs)
|
||||
self.logger.log(logging.WARN, "{}{}".format(prefix, msg), *args, **kwargs)
|
||||
|
||||
#
|
||||
# Main WebSocketRequestHandler methods
|
||||
|
|
@ -131,7 +131,7 @@ class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHa
|
|||
if self.rec:
|
||||
# Python 3 compatible conversion
|
||||
bufstr = buf.decode('latin1').encode('unicode_escape').decode('ascii').replace("'", "\\'")
|
||||
self.rec.write("'{{{0}{{{1}',\n".format(tdelta, bufstr))
|
||||
self.rec.write(f"'{{{tdelta}{{{bufstr}',\n")
|
||||
self.send_parts.append(buf)
|
||||
|
||||
while self.send_parts:
|
||||
|
|
@ -174,7 +174,7 @@ class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHa
|
|||
if self.rec:
|
||||
# Python 3 compatible conversion
|
||||
bufstr = buf.decode('latin1').encode('unicode_escape').decode('ascii').replace("'", "\\'")
|
||||
self.rec.write("'}}{0}}}{1}',\n".format(tdelta, bufstr))
|
||||
self.rec.write(f"'}}{tdelta}}}{bufstr}',\n")
|
||||
|
||||
bufs.append(buf)
|
||||
|
||||
|
|
@ -187,11 +187,11 @@ class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHa
|
|||
""" Send a WebSocket orderly close frame. """
|
||||
self.request.shutdown(socket.SHUT_RDWR, code, reason)
|
||||
|
||||
def send_pong(self, data=''.encode('ascii')):
|
||||
def send_pong(self, data=b''):
|
||||
""" Send a WebSocket pong frame. """
|
||||
self.request.pong(data)
|
||||
|
||||
def send_ping(self, data=''.encode('ascii')):
|
||||
def send_ping(self, data=b''):
|
||||
""" Send a WebSocket ping frame. """
|
||||
self.request.ping(data)
|
||||
|
||||
|
|
@ -231,7 +231,7 @@ class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHa
|
|||
|
||||
if self.record:
|
||||
# Record raw frame data as JavaScript array
|
||||
fname = "%s.%s" % (self.record,
|
||||
fname = "{}.{}".format(self.record,
|
||||
self.handler_id)
|
||||
self.log_message("opening record file: %s", fname)
|
||||
self.rec = open(fname, 'w+')
|
||||
|
|
@ -420,7 +420,7 @@ class WebSockifyServer():
|
|||
|
||||
@staticmethod
|
||||
def get_logger():
|
||||
return logging.getLogger("%s.%s" % (
|
||||
return logging.getLogger("{}.{}".format(
|
||||
WebSockifyServer.log_prefix,
|
||||
WebSockifyServer.__class__.__name__))
|
||||
|
||||
|
|
@ -684,7 +684,7 @@ class WebSockifyServer():
|
|||
_, exc, _ = sys.exc_info()
|
||||
# Connection was not a WebSockets connection
|
||||
if exc.args[0]:
|
||||
self.msg("%s: %s" % (address[0], exc.args[0]))
|
||||
self.msg("{}: {}".format(address[0], exc.args[0]))
|
||||
except WebSockifyServer.Terminate:
|
||||
raise
|
||||
except Exception:
|
||||
|
|
|
|||
Loading…
Reference in New Issue