@@ -548,12 +548,12 @@ def route_func(request: Request):
548548 FIN = 0b10000000 # FIN bit indicating the final fragment
549549
550550 # 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
557557
558558 @staticmethod
559559 def _check_request_initiates_handshake (request : Request ):
@@ -573,7 +573,7 @@ def _process_sec_websocket_key(request: Request) -> str:
573573 if key is None :
574574 raise ValueError ("Request does not have Sec-WebSocket-Key header" )
575575
576- response_key = hashlib .new (' sha1' , key .encode ())
576+ response_key = hashlib .new (" sha1" , key .encode ())
577577 response_key .update (Websocket .GUID )
578578
579579 return b2a_base64 (response_key .digest ()).strip ().decode ()
@@ -607,7 +607,6 @@ def __init__( # pylint: disable=too-many-arguments
607607
608608 request .connection .setblocking (False )
609609
610-
611610 @staticmethod
612611 def _parse_frame_header (header ):
613612 fin = header [0 ] & Websocket .FIN
@@ -626,7 +625,7 @@ def _read_frame(self):
626625 buffer = bytearray (self ._buffer_size )
627626
628627 header_length = self ._request .connection .recv_into (buffer , 2 )
629- header_bytes = buffer [:header_length ]
628+ header_bytes = buffer [:header_length ]
630629
631630 fin , opcode , has_mask , length = self ._parse_frame_header (header_bytes )
632631
@@ -640,23 +639,23 @@ def _read_frame(self):
640639
641640 if length < 0 :
642641 length = self ._request .connection .recv_into (buffer , - length )
643- length = int .from_bytes (buffer [:length ], ' big' )
642+ length = int .from_bytes (buffer [:length ], " big" )
644643
645644 if has_mask :
646645 mask_length = self ._request .connection .recv_into (buffer , 4 )
647646 mask = buffer [:mask_length ]
648647
649648 while 0 < length :
650649 payload_length = self ._request .connection .recv_into (buffer , length )
651- payload += buffer [:min (payload_length , length )]
650+ payload += buffer [: min (payload_length , length )]
652651 length -= min (payload_length , length )
653652
654653 if has_mask :
655654 payload = bytes (x ^ mask [i % 4 ] for i , x in enumerate (payload ))
656655
657656 return opcode , payload
658657
659- def _handle_frame (self , opcode : int , payload : bytes ):
658+ def _handle_frame (self , opcode : int , payload : bytes ) -> Union [ str , bytes , None ] :
660659 # TODO: Handle continuation frames, currently not supported
661660 if opcode == Websocket .CONT :
662661 return None
@@ -667,14 +666,13 @@ def _handle_frame(self, opcode: int, payload: bytes):
667666
668667 if opcode == Websocket .PONG :
669668 return None
670- elif opcode == Websocket .PING :
669+ if opcode == Websocket .PING :
671670 self .send_message (payload , Websocket .PONG )
672671 return payload
673672
674673 try :
675674 payload = payload .decode () if opcode == Websocket .TEXT else payload
676- except UnicodeError as error :
677- print ("Payload UnicodeError: " , error , payload )
675+ except UnicodeError :
678676 pass
679677
680678 return payload
@@ -688,7 +686,9 @@ def receive(self, fail_silently: bool = False) -> Union[str, bytes, None]:
688686 if self .closed :
689687 if fail_silently :
690688 return None
691- raise RuntimeError ("Websocket connection is closed, cannot receive messages" )
689+ raise RuntimeError (
690+ "Websocket connection is closed, cannot receive messages"
691+ )
692692
693693 try :
694694 opcode , payload = self ._read_frame ()
@@ -700,7 +700,7 @@ def receive(self, fail_silently: bool = False) -> Union[str, bytes, None]:
700700 return None
701701 if error .errno == ETIMEDOUT : # Connection timed out
702702 return None
703- if error .errno == ENOTCONN : # Client disconnected without closing connection
703+ if error .errno == ENOTCONN : # Client disconnected
704704 self .close ()
705705 return None
706706 raise error
@@ -720,12 +720,12 @@ def _prepare_frame(opcode: int, message: bytes) -> bytearray:
720720 # Message between 126 and 65535 bytes, use 2 bytes for length
721721 elif payload_length < 65536 :
722722 frame .append (126 )
723- frame .extend (payload_length .to_bytes (2 , ' big' ))
723+ frame .extend (payload_length .to_bytes (2 , " big" ))
724724
725725 # Message over 65535 bytes, use 8 bytes for length
726726 else :
727727 frame .append (127 )
728- frame .extend (payload_length .to_bytes (8 , ' big' ))
728+ frame .extend (payload_length .to_bytes (8 , " big" ))
729729
730730 frame .extend (message )
731731 return frame
@@ -734,7 +734,7 @@ def send_message(
734734 self ,
735735 message : Union [str , bytes ],
736736 opcode : int = None ,
737- fail_silently : bool = False
737+ fail_silently : bool = False ,
738738 ):
739739 """
740740 Send a message to the client.
@@ -746,7 +746,7 @@ def send_message(
746746 """
747747 if self .closed :
748748 if fail_silently :
749- return None
749+ return
750750 raise RuntimeError ("Websocket connection is closed, cannot send message" )
751751
752752 determined_opcode = opcode or (
@@ -762,7 +762,7 @@ def send_message(
762762 self ._send_bytes (self ._request .connection , frame )
763763 except BrokenPipeError as error :
764764 if fail_silently :
765- return None
765+ return
766766 raise error
767767
768768 def _send (self ) -> None :
@@ -775,6 +775,6 @@ def close(self):
775775 **Always call this method when you are done sending events.**
776776 """
777777 if not self .closed :
778- self .send_message (b'' , Websocket .CLOSE , fail_silently = True )
778+ self .send_message (b"" , Websocket .CLOSE , fail_silently = True )
779779 self ._close_connection ()
780780 self .closed = True
0 commit comments