Fix flake8 errors related to blank lines

This commit is contained in:
Takashi Kajinami 2025-06-08 22:26:37 +09:00
parent 070876493b
commit 9e1c731089
15 changed files with 51 additions and 22 deletions

View File

@ -14,6 +14,7 @@ import os, sys, select, optparse, logging
sys.path.insert(0,os.path.join(os.path.dirname(__file__), "..")) sys.path.insert(0,os.path.join(os.path.dirname(__file__), ".."))
from websockify.websockifyserver import WebSockifyServer, WebSockifyRequestHandler from websockify.websockifyserver import WebSockifyServer, WebSockifyRequestHandler
class WebSocketEcho(WebSockifyRequestHandler): class WebSocketEcho(WebSockifyRequestHandler):
""" """
WebSockets server that echos back whatever is received from the WebSockets server that echos back whatever is received from the
@ -50,6 +51,7 @@ class WebSocketEcho(WebSockifyRequestHandler):
if closed: if closed:
break break
if __name__ == '__main__': if __name__ == '__main__':
parser = optparse.OptionParser(usage="%prog [options] listen_port") parser = optparse.OptionParser(usage="%prog [options] listen_port")
parser.add_option("--verbose", "-v", action="store_true", parser.add_option("--verbose", "-v", action="store_true",
@ -73,4 +75,3 @@ if __name__ == '__main__':
opts.web = "." opts.web = "."
server = WebSockifyServer(WebSocketEcho, **opts.__dict__) server = WebSockifyServer(WebSocketEcho, **opts.__dict__)
server.start_server() server.start_server()

View File

@ -22,6 +22,7 @@ print("Connecting to %s..." % URL)
sock.connect(URL) sock.connect(URL)
print("Connected.") print("Connected.")
def send(msg): def send(msg):
while True: while True:
try: try:
@ -36,6 +37,7 @@ def send(msg):
ins, outs, excepts = select.select([], [sock], []) ins, outs, excepts = select.select([], [sock], [])
if excepts: raise Exception("Socket exception") if excepts: raise Exception("Socket exception")
def read(): def read():
while True: while True:
try: try:
@ -47,6 +49,7 @@ def read():
ins, outs, excepts = select.select([], [sock], []) ins, outs, excepts = select.select([], [sock], [])
if excepts: raise Exception("Socket exception") if excepts: raise Exception("Socket exception")
counter = 1 counter = 1
while True: while True:
msg = "Message #%d" % counter msg = "Message #%d" % counter

View File

@ -10,6 +10,7 @@ import sys, os, select, random, time, optparse, logging
sys.path.insert(0,os.path.join(os.path.dirname(__file__), "..")) sys.path.insert(0,os.path.join(os.path.dirname(__file__), ".."))
from websockify.websockifyserver import WebSockifyServer, WebSockifyRequestHandler from websockify.websockifyserver import WebSockifyServer, WebSockifyRequestHandler
class WebSocketLoadServer(WebSockifyServer): class WebSocketLoadServer(WebSockifyServer):
recv_cnt = 0 recv_cnt = 0
@ -85,7 +86,6 @@ class WebSocketLoad(WebSockifyRequestHandler):
return data return data
def check(self, frames): def check(self, frames):
err = "" err = ""
@ -165,4 +165,3 @@ if __name__ == '__main__':
opts.web = "." opts.web = "."
server = WebSocketLoadServer(WebSocketLoad, **opts.__dict__) server = WebSocketLoadServer(WebSocketLoad, **opts.__dict__)
server.start_server() server.start_server()

View File

@ -9,6 +9,7 @@ from jwcrypto import jwt, jwk
from websockify.token_plugins import parse_source_args, ReadOnlyTokenFile, JWTTokenApi, TokenRedis from websockify.token_plugins import parse_source_args, ReadOnlyTokenFile, JWTTokenApi, TokenRedis
class ParseSourceArgumentsTestCase(unittest.TestCase): class ParseSourceArgumentsTestCase(unittest.TestCase):
def test_parameterized(self): def test_parameterized(self):
params = [ params = [
@ -31,6 +32,7 @@ class ParseSourceArgumentsTestCase(unittest.TestCase):
for src, args in params: for src, args in params:
self.assertEqual(args, parse_source_args(src)) self.assertEqual(args, parse_source_args(src))
class ReadOnlyTokenFileTestCase(unittest.TestCase): class ReadOnlyTokenFileTestCase(unittest.TestCase):
def test_empty(self): def test_empty(self):
mock_source_file = MagicMock() mock_source_file = MagicMock()
@ -201,6 +203,7 @@ class JWSTokenTestCase(unittest.TestCase):
self.assertEqual(result[0], "remote_host") self.assertEqual(result[0], "remote_host")
self.assertEqual(result[1], "remote_port") self.assertEqual(result[1], "remote_port")
class TokenRedisTestCase(unittest.TestCase): class TokenRedisTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
try: try:

View File

@ -18,6 +18,7 @@
import unittest import unittest
from websockify import websocket from websockify import websocket
class FakeSocket: class FakeSocket:
def __init__(self): def __init__(self):
self.data = b'' self.data = b''
@ -26,6 +27,7 @@ class FakeSocket:
self.data += buf self.data += buf
return len(buf) return len(buf)
class AcceptTestCase(unittest.TestCase): class AcceptTestCase(unittest.TestCase):
def test_success(self): def test_success(self):
ws = websocket.WebSocket() ws = websocket.WebSocket()
@ -116,6 +118,7 @@ class AcceptTestCase(unittest.TestCase):
'Sec-WebSocket-Key': 'DKURYVK9cRFul1vOZVA56Q==', 'Sec-WebSocket-Key': 'DKURYVK9cRFul1vOZVA56Q==',
'Sec-WebSocket-Protocol': 'foobar,gazonk'}) 'Sec-WebSocket-Protocol': 'foobar,gazonk'})
class PingPongTest(unittest.TestCase): class PingPongTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.ws = websocket.WebSocket() self.ws = websocket.WebSocket()
@ -142,6 +145,7 @@ class PingPongTest(unittest.TestCase):
self.ws.pong(b'foo') self.ws.pong(b'foo')
self.assertEqual(self.sock.data, b'\x8a\x03foo') self.assertEqual(self.sock.data, b'\x8a\x03foo')
class HyBiEncodeDecodeTestCase(unittest.TestCase): class HyBiEncodeDecodeTestCase(unittest.TestCase):
def test_decode_hybi_text(self): def test_decode_hybi_text(self):
buf = b'\x81\x85\x37\xfa\x21\x3d\x7f\x9f\x4d\x51\x58' buf = b'\x81\x85\x37\xfa\x21\x3d\x7f\x9f\x4d\x51\x58'

View File

@ -56,6 +56,7 @@ class FakeServer:
self.ssl_target = None self.ssl_target = None
self.unix_target = None self.unix_target = None
class ProxyRequestHandlerTestCase(unittest.TestCase): class ProxyRequestHandlerTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
@ -126,4 +127,3 @@ class ProxyRequestHandlerTestCase(unittest.TestCase):
self.handler.server.target_host = "someotherhost" self.handler.server.target_host = "someotherhost"
self.handler.auth_connection() self.handler.auth_connection()

View File

@ -66,4 +66,3 @@ class HttpWebSocketTest(unittest.TestCase):
# Then # Then
req_obj.end_headers.assert_called_once_with() req_obj.end_headers.assert_called_once_with()

View File

@ -231,6 +231,7 @@ class WebSockifyServerTestCase(unittest.TestCase):
def test_do_handshake_no_ssl(self): def test_do_handshake_no_ssl(self):
class FakeHandler: class FakeHandler:
CALLED = False CALLED = False
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
type(self).CALLED = True type(self).CALLED = True
@ -286,12 +287,16 @@ class WebSockifyServerTestCase(unittest.TestCase):
def __init__(self, purpose): def __init__(self, purpose):
self.verify_mode = None self.verify_mode = None
self.options = 0 self.options = 0
def load_cert_chain(self, certfile, keyfile, password): def load_cert_chain(self, certfile, keyfile, password):
pass pass
def set_default_verify_paths(self): def set_default_verify_paths(self):
pass pass
def load_verify_locations(self, cafile): def load_verify_locations(self, cafile):
pass pass
def wrap_socket(self, *args, **kwargs): def wrap_socket(self, *args, **kwargs):
raise ssl.SSLError(ssl.SSL_ERROR_EOF) raise ssl.SSLError(ssl.SSL_ERROR_EOF)
@ -317,17 +322,23 @@ class WebSockifyServerTestCase(unittest.TestCase):
class fake_create_default_context(): class fake_create_default_context():
CIPHERS = '' CIPHERS = ''
def __init__(self, purpose): def __init__(self, purpose):
self.verify_mode = None self.verify_mode = None
self.options = 0 self.options = 0
def load_cert_chain(self, certfile, keyfile, password): def load_cert_chain(self, certfile, keyfile, password):
pass pass
def set_default_verify_paths(self): def set_default_verify_paths(self):
pass pass
def load_verify_locations(self, cafile): def load_verify_locations(self, cafile):
pass pass
def wrap_socket(self, *args, **kwargs): def wrap_socket(self, *args, **kwargs):
pass pass
def set_ciphers(self, ciphers_to_set): def set_ciphers(self, ciphers_to_set):
fake_create_default_context.CIPHERS = ciphers_to_set fake_create_default_context.CIPHERS = ciphers_to_set
@ -352,19 +363,26 @@ class WebSockifyServerTestCase(unittest.TestCase):
class fake_create_default_context: class fake_create_default_context:
OPTIONS = 0 OPTIONS = 0
def __init__(self, purpose): def __init__(self, purpose):
self.verify_mode = None self.verify_mode = None
self._options = 0 self._options = 0
def load_cert_chain(self, certfile, keyfile, password): def load_cert_chain(self, certfile, keyfile, password):
pass pass
def set_default_verify_paths(self): def set_default_verify_paths(self):
pass pass
def load_verify_locations(self, cafile): def load_verify_locations(self, cafile):
pass pass
def wrap_socket(self, *args, **kwargs): def wrap_socket(self, *args, **kwargs):
pass pass
def get_options(self): def get_options(self):
return self._options return self._options
def set_options(self, val): def set_options(self, val):
fake_create_default_context.OPTIONS = val fake_create_default_context.OPTIONS = val
options = property(get_options, set_options) options = property(get_options, set_options)

View File

@ -76,6 +76,7 @@ class BasicHTTPAuth():
raise AuthenticationError(response_code=401, raise AuthenticationError(response_code=401,
response_headers={'WWW-Authenticate': 'Basic realm="Websockify"'}) response_headers={'WWW-Authenticate': 'Basic realm="Websockify"'})
class ExpectOrigin(): class ExpectOrigin():
def __init__(self, src=None): def __init__(self, src=None):
if src is None: if src is None:
@ -88,6 +89,7 @@ class ExpectOrigin():
if origin is None or origin not in self.source: if origin is None or origin not in self.source:
raise InvalidOriginError(expected=self.source, actual=origin) raise InvalidOriginError(expected=self.source, actual=origin)
class ClientCertCNAuth(): class ClientCertCNAuth():
"""Verifies client by SSL certificate. Specify src as whitespace separated list of common names.""" """Verifies client by SSL certificate. Specify src as whitespace separated list of common names."""

View File

@ -16,11 +16,8 @@ class WebsockifySysLogHandler(handlers.SysLogHandler):
_max_ident = 24 #safer for old daemons _max_ident = 24 #safer for old daemons
_send_length = False _send_length = False
_tail = '\n' _tail = '\n'
ident = None ident = None
def __init__(self, address=('localhost', handlers.SYSLOG_UDP_PORT), def __init__(self, address=('localhost', handlers.SYSLOG_UDP_PORT),
facility=handlers.SysLogHandler.LOG_USER, facility=handlers.SysLogHandler.LOG_USER,
socktype=None, ident=None, legacy=False): socktype=None, ident=None, legacy=False):
@ -46,7 +43,6 @@ class WebsockifySysLogHandler(handlers.SysLogHandler):
super().__init__(address, facility, socktype) super().__init__(address, facility, socktype)
def emit(self, record): def emit(self, record):
""" """
Emit a record. Emit a record.

View File

@ -84,6 +84,7 @@ class TokenFile(ReadOnlyTokenFile):
return super().lookup(token) return super().lookup(token)
class TokenFileName(BasePlugin): class TokenFileName(BasePlugin):
# source is a directory # source is a directory
# token is filename # token is filename

View File

@ -31,11 +31,15 @@ except ImportError:
warnings.warn("no 'numpy' module, HyBi protocol will be slower") warnings.warn("no 'numpy' module, HyBi protocol will be slower")
numpy = None numpy = None
class WebSocketWantReadError(ssl.SSLWantReadError): class WebSocketWantReadError(ssl.SSLWantReadError):
pass pass
class WebSocketWantWriteError(ssl.SSLWantWriteError): class WebSocketWantWriteError(ssl.SSLWantWriteError):
pass pass
class WebSocket: class WebSocket:
"""WebSocket protocol socket like class. """WebSocket protocol socket like class.
@ -873,4 +877,3 @@ class WebSocket:
f['payload'] = buf[hlen:(hlen+length)] f['payload'] = buf[hlen:(hlen+length)]
return f return f

View File

@ -20,6 +20,7 @@ from websockify import websockifyserver
from websockify import auth_plugins as auth from websockify import auth_plugins as auth
from urllib.parse import parse_qs, urlparse from urllib.parse import parse_qs, urlparse
class ProxyRequestHandler(websockifyserver.WebSockifyRequestHandler): class ProxyRequestHandler(websockifyserver.WebSockifyRequestHandler):
buffer_size = 65536 buffer_size = 65536
@ -248,7 +249,6 @@ Traffic Legend:
self.server.target_host, self.server.target_port) self.server.target_host, self.server.target_port)
raise self.CClose(closed['code'], closed['reason']) raise self.CClose(closed['code'], closed['reason'])
if target in outs: if target in outs:
# Send queued client data to the target # Send queued client data to the target
dat = tqueue.pop(0) dat = tqueue.pop(0)
@ -260,7 +260,6 @@ Traffic Legend:
tqueue.insert(0, dat[sent:]) tqueue.insert(0, dat[sent:])
self.print_traffic(".>") self.print_traffic(".>")
if target in ins: if target in ins:
# Receive target data, encode it and queue for client # Receive target data, encode it and queue for client
buf = target.recv(self.buffer_size) buf = target.recv(self.buffer_size)
@ -283,6 +282,7 @@ Traffic Legend:
cqueue.append(buf) cqueue.append(buf)
self.print_traffic("{") self.print_traffic("{")
class WebSocketProxy(websockifyserver.WebSockifyServer): class WebSocketProxy(websockifyserver.WebSockifyServer):
""" """
Proxy traffic to and from a WebSockets client to a normal TCP Proxy traffic to and from a WebSockets client to a normal TCP
@ -427,6 +427,7 @@ SSL_OPTIONS = {
ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_2, ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_2,
} }
def select_ssl_version(version): def select_ssl_version(version):
"""Returns SSL options for the most secure TSL version available on this """Returns SSL options for the most secure TSL version available on this
Python version""" Python version"""
@ -444,6 +445,7 @@ def select_ssl_version(version):
return SSL_OPTIONS[fallback] return SSL_OPTIONS[fallback]
def websockify_init(): def websockify_init():
# Setup basic logging to stderr. # Setup basic logging to stderr.
stderr_handler = logging.StreamHandler() stderr_handler = logging.StreamHandler()
@ -562,9 +564,7 @@ def websockify_init():
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
# Validate options. # Validate options.
if opts.token_source and not opts.token_plugin: if opts.token_source and not opts.token_plugin:
parser.error("You must use --token-plugin to use --token-source") parser.error("You must use --token-plugin to use --token-source")
@ -583,11 +583,9 @@ def websockify_init():
if opts.legacy_syslog and not opts.syslog: if opts.legacy_syslog and not opts.syslog:
parser.error("You must use --syslog to use --legacy-syslog") parser.error("You must use --syslog to use --legacy-syslog")
opts.ssl_options = select_ssl_version(opts.ssl_version) opts.ssl_options = select_ssl_version(opts.ssl_version)
del opts.ssl_version del opts.ssl_version
if opts.log_file: if opts.log_file:
# Setup logging to user-specified file. # Setup logging to user-specified file.
opts.log_file = os.path.abspath(opts.log_file) opts.log_file = os.path.abspath(opts.log_file)
@ -638,7 +636,6 @@ def websockify_init():
root = logging.getLogger() root = logging.getLogger()
root.setLevel(logging.DEBUG) root.setLevel(logging.DEBUG)
# Transform to absolute path as daemon may chdir # Transform to absolute path as daemon may chdir
if opts.target_cfg: if opts.target_cfg:
opts.target_cfg = os.path.abspath(opts.target_cfg) opts.target_cfg = os.path.abspath(opts.target_cfg)
@ -795,7 +792,6 @@ class LibProxyServer(ThreadingMixIn, HTTPServer):
super().__init__((listen_host, listen_port), RequestHandlerClass) super().__init__((listen_host, listen_port), RequestHandlerClass)
def process_request(self, request, client_address): def process_request(self, request, client_address):
"""Override process_request to implement a counter""" """Override process_request to implement a counter"""
self.handler_id += 1 self.handler_id += 1

View File

@ -12,6 +12,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
from websockify.websocket import WebSocket from websockify.websocket import WebSocket
class HttpWebSocket(WebSocket): class HttpWebSocket(WebSocket):
"""Class to glue websocket and http request functionality together""" """Class to glue websocket and http request functionality together"""
def __init__(self, request_handler): def __init__(self, request_handler):
@ -100,11 +101,14 @@ class WebSocketRequestHandlerMixIn:
""" """
pass pass
# Convenient ready made classes # Convenient ready made classes
class WebSocketRequestHandler(WebSocketRequestHandlerMixIn, class WebSocketRequestHandler(WebSocketRequestHandlerMixIn,
BaseHTTPRequestHandler): BaseHTTPRequestHandler):
pass pass
class WebSocketServer(HTTPServer): class WebSocketServer(HTTPServer):
pass pass

View File

@ -32,6 +32,7 @@ if sys.platform == 'win32':
from websockify.websocket import WebSocketWantReadError, WebSocketWantWriteError from websockify.websocket import WebSocketWantReadError, WebSocketWantWriteError
from websockify.websocketserver import WebSocketRequestHandlerMixIn from websockify.websocketserver import WebSocketRequestHandlerMixIn
class CompatibleWebSocket(WebSocketRequestHandlerMixIn.SocketClass): class CompatibleWebSocket(WebSocketRequestHandlerMixIn.SocketClass):
def select_subprotocol(self, protocols): def select_subprotocol(self, protocols):
# Handle old websockify clients that still specify a sub-protocol # Handle old websockify clients that still specify a sub-protocol
@ -40,6 +41,7 @@ class CompatibleWebSocket(WebSocketRequestHandlerMixIn.SocketClass):
else: else:
return '' return ''
# HTTP handler with WebSocket upgrade support # HTTP handler with WebSocket upgrade support
class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHandler): class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHandler):
""" """
@ -644,10 +646,10 @@ class WebSockifyServer():
""" Same as msg() but as warning. """ """ Same as msg() but as warning. """
self.logger.log(logging.WARN, *args, **kwargs) self.logger.log(logging.WARN, *args, **kwargs)
# #
# Events that can/should be overridden in sub-classes # Events that can/should be overridden in sub-classes
# #
def started(self): def started(self):
""" Called after WebSockets startup """ """ Called after WebSockets startup """
self.vmsg("WebSockets server started") self.vmsg("WebSockets server started")
@ -879,5 +881,3 @@ class WebSockifyServer():
# Restore signals # Restore signals
for sig, func in original_signals.items(): for sig, func in original_signals.items():
signal.signal(sig, func) signal.signal(sig, func)