@@ -548,12 +548,12 @@ def route_func(request: Request):
548
548
FIN = 0b10000000 # FIN bit indicating the final fragment
549
549
550
550
# opcodes
551
- CONT = 0 # Continuation frame, TODO: Currently not supported
552
- TEXT = 1 # Frame contains UTF-8 text
553
- BINARY = 2 # Frame contains binary data
554
- CLOSE = 8 # Frame closes the connection
555
- PING = 9 # Frame is a ping, expecting a pong
556
- PONG = 10 # Frame is a pong, in response to a ping
551
+ CONT = 0 # Continuation frame, TODO: Currently not supported
552
+ TEXT = 1 # Frame contains UTF-8 text
553
+ BINARY = 2 # Frame contains binary data
554
+ CLOSE = 8 # Frame closes the connection
555
+ PING = 9 # Frame is a ping, expecting a pong
556
+ PONG = 10 # Frame is a pong, in response to a ping
557
557
558
558
@staticmethod
559
559
def _check_request_initiates_handshake (request : Request ):
@@ -573,7 +573,7 @@ def _process_sec_websocket_key(request: Request) -> str:
573
573
if key is None :
574
574
raise ValueError ("Request does not have Sec-WebSocket-Key header" )
575
575
576
- response_key = hashlib .new (' sha1' , key .encode ())
576
+ response_key = hashlib .new (" sha1" , key .encode ())
577
577
response_key .update (Websocket .GUID )
578
578
579
579
return b2a_base64 (response_key .digest ()).strip ().decode ()
@@ -607,7 +607,6 @@ def __init__( # pylint: disable=too-many-arguments
607
607
608
608
request .connection .setblocking (False )
609
609
610
-
611
610
@staticmethod
612
611
def _parse_frame_header (header ):
613
612
fin = header [0 ] & Websocket .FIN
@@ -626,7 +625,7 @@ def _read_frame(self):
626
625
buffer = bytearray (self ._buffer_size )
627
626
628
627
header_length = self ._request .connection .recv_into (buffer , 2 )
629
- header_bytes = buffer [:header_length ]
628
+ header_bytes = buffer [:header_length ]
630
629
631
630
fin , opcode , has_mask , length = self ._parse_frame_header (header_bytes )
632
631
@@ -640,23 +639,23 @@ def _read_frame(self):
640
639
641
640
if length < 0 :
642
641
length = self ._request .connection .recv_into (buffer , - length )
643
- length = int .from_bytes (buffer [:length ], ' big' )
642
+ length = int .from_bytes (buffer [:length ], " big" )
644
643
645
644
if has_mask :
646
645
mask_length = self ._request .connection .recv_into (buffer , 4 )
647
646
mask = buffer [:mask_length ]
648
647
649
648
while 0 < length :
650
649
payload_length = self ._request .connection .recv_into (buffer , length )
651
- payload += buffer [:min (payload_length , length )]
650
+ payload += buffer [: min (payload_length , length )]
652
651
length -= min (payload_length , length )
653
652
654
653
if has_mask :
655
654
payload = bytes (x ^ mask [i % 4 ] for i , x in enumerate (payload ))
656
655
657
656
return opcode , payload
658
657
659
- def _handle_frame (self , opcode : int , payload : bytes ):
658
+ def _handle_frame (self , opcode : int , payload : bytes ) -> Union [ str , bytes , None ] :
660
659
# TODO: Handle continuation frames, currently not supported
661
660
if opcode == Websocket .CONT :
662
661
return None
@@ -667,14 +666,13 @@ def _handle_frame(self, opcode: int, payload: bytes):
667
666
668
667
if opcode == Websocket .PONG :
669
668
return None
670
- elif opcode == Websocket .PING :
669
+ if opcode == Websocket .PING :
671
670
self .send_message (payload , Websocket .PONG )
672
671
return payload
673
672
674
673
try :
675
674
payload = payload .decode () if opcode == Websocket .TEXT else payload
676
- except UnicodeError as error :
677
- print ("Payload UnicodeError: " , error , payload )
675
+ except UnicodeError :
678
676
pass
679
677
680
678
return payload
@@ -688,7 +686,9 @@ def receive(self, fail_silently: bool = False) -> Union[str, bytes, None]:
688
686
if self .closed :
689
687
if fail_silently :
690
688
return None
691
- raise RuntimeError ("Websocket connection is closed, cannot receive messages" )
689
+ raise RuntimeError (
690
+ "Websocket connection is closed, cannot receive messages"
691
+ )
692
692
693
693
try :
694
694
opcode , payload = self ._read_frame ()
@@ -700,7 +700,7 @@ def receive(self, fail_silently: bool = False) -> Union[str, bytes, None]:
700
700
return None
701
701
if error .errno == ETIMEDOUT : # Connection timed out
702
702
return None
703
- if error .errno == ENOTCONN : # Client disconnected without closing connection
703
+ if error .errno == ENOTCONN : # Client disconnected
704
704
self .close ()
705
705
return None
706
706
raise error
@@ -720,12 +720,12 @@ def _prepare_frame(opcode: int, message: bytes) -> bytearray:
720
720
# Message between 126 and 65535 bytes, use 2 bytes for length
721
721
elif payload_length < 65536 :
722
722
frame .append (126 )
723
- frame .extend (payload_length .to_bytes (2 , ' big' ))
723
+ frame .extend (payload_length .to_bytes (2 , " big" ))
724
724
725
725
# Message over 65535 bytes, use 8 bytes for length
726
726
else :
727
727
frame .append (127 )
728
- frame .extend (payload_length .to_bytes (8 , ' big' ))
728
+ frame .extend (payload_length .to_bytes (8 , " big" ))
729
729
730
730
frame .extend (message )
731
731
return frame
@@ -734,7 +734,7 @@ def send_message(
734
734
self ,
735
735
message : Union [str , bytes ],
736
736
opcode : int = None ,
737
- fail_silently : bool = False
737
+ fail_silently : bool = False ,
738
738
):
739
739
"""
740
740
Send a message to the client.
@@ -746,7 +746,7 @@ def send_message(
746
746
"""
747
747
if self .closed :
748
748
if fail_silently :
749
- return None
749
+ return
750
750
raise RuntimeError ("Websocket connection is closed, cannot send message" )
751
751
752
752
determined_opcode = opcode or (
@@ -762,7 +762,7 @@ def send_message(
762
762
self ._send_bytes (self ._request .connection , frame )
763
763
except BrokenPipeError as error :
764
764
if fail_silently :
765
- return None
765
+ return
766
766
raise error
767
767
768
768
def _send (self ) -> None :
@@ -775,6 +775,6 @@ def close(self):
775
775
**Always call this method when you are done sending events.**
776
776
"""
777
777
if not self .closed :
778
- self .send_message (b'' , Websocket .CLOSE , fail_silently = True )
778
+ self .send_message (b"" , Websocket .CLOSE , fail_silently = True )
779
779
self ._close_connection ()
780
780
self .closed = True
0 commit comments