
    2BfQ                         d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dlm	Z	m
Z
mZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d	d
lmZmZ  G d d      Z G d de      Z G d de      Zy)    N)time)urlsplit)ConnectionTypeWSConnection)	AcceptConnectionRejectConnectionCloseConnectionMessageRequestPingPongTextMessageBytesMessage)PerMessageDeflate)CloseReason)LocalProtocolError   )ConnectionErrorConnectionClosedc                   L    e Zd Z	 	 ddZd Zd Zd ZddZddZd Z	d	 Z
d
 Zy)AioBaseNc                 b   d | _         || _        || _        || _        || _        d| _        g | _        d | _        d| _        d| _	        |t        j                  k(  | _        t        j                  | _        d | _        d | _        d | _        t'        j(                         | _        d | _        d | _        y )NTr   F)subprotocolconnection_typereceive_bytesping_intervalmax_message_sizepong_receivedinput_bufferincoming_messageincoming_message_len	connectedr   SERVER	is_serverr   NO_STATUS_RCVDclose_reasonclose_messagersockwsockasyncioEventeventwstask)selfr   r   r   r   s        V/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/simple_websocket/aiows.py__init__zAioBase.__init__   s      .** 0! $$%!)^-B-BB'66!

]]_
	    c                    K   t        | j                        | _        | j                          d {    | j                  s
t               t        j                  | j                               | _	        y 7 CwN)
r   r   r-   	handshaker"   r   r*   create_task_taskr.   r/   s    r0   connectzAioBase.connect1   sU     t334nn~~!##''

5		 	s   .A6A4AA6c                    K   y wr4    r8   s    r0   r5   zAioBase.handshake9   s	     s   c                 b  K   | j                   s t        | j                  | j                        t	        |t
              r&| j                  j                  t        |            }n.| j                  j                  t        t        |                  }| j                  j                  |       yw)zSend data over the WebSocket connection.

        :param data: The data to send. If ``data`` is of type ``bytes``, then
                     a binary message is sent. Else, the message is sent in
                     text format.
        )dataN)r"   r   r&   r'   
isinstancebytesr-   sendr
   r   strr)   write)r/   r=   out_datas      r0   r@   zAioBase.send=   sv      ~~"4#4#4d6H6HIIdE"ww||G$67Hww||KSY$?@H

"s   B-B/c                   K   | j                   rw| j                  sk	 t        j                  | j                  j                         |       d{    | j                  j                          | j                   r| j                  sk	 | j                  j                  d      S 7 S# t        j                  $ r Y yw xY w# t        $ r Y nw xY w| j                   s t        | j                  | j                        yw)a}  Receive data over the WebSocket connection.

        :param timeout: Amount of time to wait for the data, in seconds. Set
                        to ``None`` (the default) to wait indefinitely. Set
                        to 0 to read without blocking.

        The data received is returned, as ``bytes`` or ``str``, depending on
        the type of the incoming message.
        timeoutNr   )r"   r   r*   wait_forr,   waitTimeoutErrorclearpop
IndexErrorr   r&   r'   )r/   rF   s     r0   receivezAioBase.receiveL   s      nnT%6%6&&tzz'8'JJJ JJ nnT%6%6	$$((++ K'' 
  		~~"4#4#4d6H6HII s^   C92B$ B"B$ 2C9B= !C9"B$ $B:7C99B::C9=	C	C9C		0C9c                 D  K   | j                   s t        | j                  | j                        | j                  j                  t        |xs t        j                  |            }	 | j                  j                  |       d| _         y# t        $ r
 Y d| _         yw xY ww)a@  Close the WebSocket connection.

        :param reason: A numeric status code indicating the reason of the
                       closure, as defined by the WebSocket specification. The
                       default is 1000 (normal closure).
        :param message: A text message to be sent to the other side.
        FN)r"   r   r&   r'   r-   r@   r	   r   NORMAL_CLOSUREr)   rB   BrokenPipeError)r/   reasonmessagerC   s       r0   closezAioBase.closec   s      ~~"4#4#4d6H6HII77<<0k00'!; <	JJX&   		s*   A$B 'B
 B 
	B	B BB c                      y r4   r;   )r/   requests     r0   choose_subprotocolzAioBase.choose_subprotocolu   s     r2   c                 \  K   d }| j                   rt               | j                   z   }| j                  r	 d}|rt               }d}||kD  rHd}	 t        j                  | j
                  j                  | j                        ||z
         d {   }|r| j                  s*| j                  t        j                  d       d {    nd| _	        | j                  j                  | j                  j!                  t#                            t%        ||      | j                   z   }| j
                  j                  | j                         d {   }t'        |      dk(  r
t)               	 | j                  j1                  |       | j3                          d {   | _        | j                  r| j                  j                          y 7 B# t        j                  $ r d}Y Xw xY w7 *7 # t(        t*        f$ r$ d| _        | j,                  j/                          Y qw xY w7 w)Nr2   TFrE   zPing/Pong timeoutrQ   rR   r   )r   r   r"   r*   rG   r(   readr   rI   r   rS   r   POLICY_VIOLATIONr)   rB   r-   r@   r   maxlenOSErrorConnectionResetErrorr,   setreceive_data_handle_events)r/   	next_pingin_datanow	timed_outs        r0   r7   zAioBase._task{   s    	!3!33Inn&C $I 3$)	-,3,<,< $

0B0B C(1C-9 '9G
 !#11"&**'2'C'C(; #- #= = = "-2*

((df)=>$'Y$7$:L:L$L	 $(JJOOD4F4F$GGGw<1$!)O % GG  )#'#6#6#88DNG nnH 	

7'9  '33 -(,I-
= H 12 !&

  9s   3H,G4 A G GG 2G4 G/G4 
H,AG4 'H,)(G4 G2G4 ./H,H*H,5H,G G,(G4 +G,,G4 2G4 40H'$H,&H''H,c           	        K   d}d}| j                   j                         D ]  }	 t        |t              rT| j	                  |      | _        || j                   j                  t        | j
                  t               g            z  }nt        |t              r| j                  r,|| j                   j                  |j                               z  }|j                  | _        |j                  | _        d| _        | j"                  j%                          d}nt        |t&              r.|| j                   j                  |j                               z  }nt        |t(              r	d| _        nt        |t,        t.        f      r| xj0                  t3        |j4                        z  c_        | j6                  rn| j0                  | j6                  kD  rU|| j                   j                  t        t8        j:                  d            z  }| j"                  j%                          d} n | j<                  |j4                  | _        nt        |t,              r~t        | j<                  t>              s6t?        | j<                  |j4                  z   jA                               | _        n| xj<                  |j4                  jA                         z  c_        nat        | j<                  t>              s(t?        | j<                  |j4                  z         | _        n| xj<                  |j4                  z  c_        |jB                  s#t        | j<                  tD        tF        f      r&| jH                  jK                  | j<                         nrt        |t,              r4| jH                  jK                  | j<                  jM                                n.| jH                  jK                  tG        | j<                               d | _        d| _        | j"                  j%                          n	  |r| jP                  jS                  |       |S # tN        $ r" d}| j"                  j%                          d}Y Qw xY ww)NTr2   )r   
extensionsFzMessage is too bigr   )*r-   eventsr>   r   rV   r   r@   r   r   r	   r$   responsecoder&   rQ   r'   r"   r,   r_   r   r   r   r   r   r!   r\   r=   r   r   MESSAGE_TOO_BIGr    	bytearrayencodemessage_finishedrA   r?   r   appenddecoder   r)   rB   )r/   
keep_goingrC   r,   s       r0   ra   zAioBase._handle_events   sa    
WW^^% G	#EF#eW-'+'>'>u'ED$-=$($4$4$5$7#8.: !; ;H  7~~ DGGLL1A$BB(-

D%).D&%*DNJJNN$!&Jt,U^^-= >>Ht,)-D&\'BC--UZZ@-,, 558M8MM DGGLL'779M2O %P P

(%*
,,4 16

-#E;7)$*?*?K4=!%!6!6!C K K M5OD1 !11UZZ5F5F5HH1)$*?*?K4= $ 5 5

 B5DD1 !11UZZ?1 11 !$"7"7#uF))001F1FG#E;7))00 1188:< ))00t7L7L1MN,0D)01D-JJNN$GG	#P JJX& & #

 "
#sC   #Q6H Q&Q6)DQQ6C"Q&"Q6'Q3/Q62Q33Q6N   NNr4   NN)__name__
__module____qualname__r1   r9   r5   r@   rM   rS   rV   r7   ra   r;   r2   r0   r   r      s8    ;?6:06#J.$)VMr2   r   c                   V     e Zd ZdZ	 	 d fd	Ze	 	 	 dd       Z fdZd Zd Z	 xZ
S )		AioServerzThis class implements a WebSocket server.

    Instead of creating an instance of this class directly, use the
    ``accept()`` class method to create individual instances of the server,
    each bound to a client request.
    c                     t         |   t        j                  |||       || _        i | _        |xs g | _        t        | j                  t              r| j                  g| _        d| _	        y )Nr   r   r   r   unknown)
superr1   r   r#   rU   headerssubprotocolsr>   rA   mode)r/   rU   r   r   r   r   	__class__s         r0   r1   zAioServer.__init__   sk    )>)>'4'4*: 	 	< (.Bd''-!%!2!2 3D	r2   c	                   K   |r|s|rt        d      |r|s|rt        d      |r,ddlm}	 |	j                  |d   |d   |d   |       d{   S  | |||d	||||
      }
|
j	                          d{    |
S 7 /7 w)a  Accept a WebSocket connection from a client.

        :param aiohttp: The request object from aiohttp. If this argument is
                        provided, ``asgi``, ``sock`` and ``headers`` must not
                        be set.
        :param asgi: A (scope, receive, send) tuple from an ASGI request. If
                     this argument is provided, ``aiohttp``, ``sock`` and
                     ``headers`` must not be set.
        :param sock: A connected socket to use. If this argument is provided,
                     ``aiohttp`` and ``asgi`` must not be set. The ``headers``
                     argument must be set with the incoming request headers.
        :param headers: A dictionary with the incoming request headers, when
                        ``sock`` is used.
        :param subprotocols: A list of supported subprotocols, or ``None`` (the
                             default) to disable subprotocol negotiation.
        :param receive_bytes: The size of the receive buffer, in bytes. The
                              default is 4096.
        :param ping_interval: Send ping packets to clients at the requested
                              interval in seconds. Set to ``None`` (the
                              default) to disable ping/pong logic. Enable to
                              prevent disconnections when the line is idle for
                              a certain amount of time, or to detect
                              unresponsive clients and disconnect them. A
                              recommended interval is 25 seconds.
        :param max_message_size: The maximum size allowed for a message, in
                                 bytes, or ``None`` for no limit. The default
                                 is ``None``.
        z1aiohttp argument cannot be used with asgi or sockz1asgi argument cannot be used with aiohttp or sockr   )WebSocketASGIr      r   N)aiohttpsockr~   )r   r   r   r   )
ValueErrorasgir   accept_accept)clsr   r   r   r~   r   r   r   r   r   r-   s              r0   r   zAioServer.accept
  s     @  $ % %W $ % %+&--d1gtAwQ;G . I I I WdwG*-,"24 jjl	I 	s$   ABA>(B7B 8B Bc                   K   | j                   d   r+| j                   d   }| j                   d   | _        d| _        nv| j                   d   r\| j                   d   j                  j	                  d      j                         }| j                   d   j                  | _        d| _        nt        d      t        j                  |       d {   \  | _	        | _
        t        | 1          d {    y 7 (7 w)Nr   r~   customr   socketzInvalid request)r   )rU   r~   r   	transportget_extra_infodupr   r*   open_connectionr(   r)   r}   r9   )r/   r   r   s     r0   r   zAioServer._accept<  s     <<<<'D<<	2DL DI\\)$<<	*44CC#% <<	2::DL!DI.//'.'>'>D'I!I
DJgo "Js$   C
C:C6"C:0C81C:8C:c                   K   d}| j                   j                         D ]  \  }}|| d| dj                         z  }  |dz  }| j                  j	                  |       | j                          d {   | _        y 7 
w)Ns   GET / HTTP/1.1
z: z
s   
)r~   itemsrm   r-   r`   ra   r"   )r/   rc   headervalues       r0   r5   zAioServer.handshakeM  s}     '!\\//1 	9MFE&E7$/6688G	97W%#22444s   A1B 3A>4B c                 J    |j                   D ]  }|| j                   v s|c S  y)a  Choose a subprotocol to use for the WebSocket connection.

        The default implementation selects the first protocol requested by the
        client that is accepted by the server. Subclasses can override this
        method to implement a different subprotocol negotiation algorithm.

        :param request: A ``Request`` object.

        The method should return the subprotocol to use, or ``None`` if no
        subprotocol is chosen.
        Nr   )r/   rU   r   s      r0   rV   zAioServer.choose_subprotocolU  s2     #// 	#Kd///""	# r2   rr   )NNNNNrs   NN)ru   rv   rw   __doc__r1   classmethodr   r   r5   rV   __classcell__r   s   @r0   ry   ry      sD     BF6: FJJN&*/ /b "5r2   ry   c                   `     e Zd ZdZ	 	 	 d fd	Ze	 	 	 	 dd       Z fdZd Zd	 fd	Z	 xZ
S )
	AioClientzThis class implements a WebSocket client.

    Instead of creating an instance of this class directly, use the
    ``connect()`` class method to create an instance that is connected to a
    server.
    c                    t         |   t        j                  |||       || _        || _        t        |      }|j                  dv | _        |j                  | _
        |j                  xs | j                  rdnd| _        |j                  | _        |j                  r"| xj                  d|j                  z   z  c_        |xs g | _        t        | j                  t               r| j                  g| _        g | _        t        |t$              r6|j'                         D ]"  \  }	}
| j"                  j)                  |	|
f       $ y t        |t*              r|| _        y y )Nr{   )httpswssi  P   ?)r}   r1   r   CLIENTurlssl_contextr   scheme	is_securehostnamehostportpathqueryr   r>   rA   extra_headeersdictr   ro   list)r/   r   r   r~   r   r   r   r   
parsed_urlkeyr   r   s              r0   r1   zAioClient.__init__n  s/    	)>)>'4'4*: 	 	< &c]
#**.>>''	OOFt~~2	OO	IIz////I(.Bd''-!%!2!2 3D gt$%mmo 9
U##**C<89&")D 'r2   c
           	      `   K    | |||||||      }
|
j                          d{    |
S 7 w)a  Returns a WebSocket client connection.

        :param url: The connection URL. Both ``ws://`` and ``wss://`` URLs are
                    accepted.
        :param subprotocols: The name of the subprotocol to use, or a list of
                             subprotocol names in order of preference. Set to
                             ``None`` (the default) to not use a subprotocol.
        :param headers: A dictionary or list of tuples with additional HTTP
                        headers to send with the connection request. Note that
                        custom headers are not supported by the WebSocket
                        protocol, so the use of this parameter is not
                        recommended.
        :param receive_bytes: The size of the receive buffer, in bytes. The
                              default is 4096.
        :param ping_interval: Send ping packets to the server at the requested
                              interval in seconds. Set to ``None`` (the
                              default) to disable ping/pong logic. Enable to
                              prevent disconnections when the line is idle for
                              a certain amount of time, or to detect an
                              unresponsive server and disconnect. A recommended
                              interval is 25 seconds. In general it is
                              preferred to enable ping/pong on the server, and
                              let the client respond with pong (which it does
                              regardless of this setting).
        :param max_message_size: The maximum size allowed for a message, in
                                 bytes, or ``None`` for no limit. The default
                                 is ``None``.
        :param ssl_context: An ``SSLContext`` instance, if a default SSL
                            context isn't sufficient.
        )r   r~   r   r   r   r   N)_connect)r   r   r   r~   r   r   r   r   thread_classevent_classr-   s              r0   r9   zAioClient.connect  s?     F <,M"2M kkm	 	s   #.,.c                 n  K   | j                   r?| j                  3t        j                  t        j                  j
                        | _        t        j                  | j                  | j                  | j                         d {   \  | _
        | _        t        | 5          d {    y 7 (7 w)N)purpose)ssl)r   r   r   create_default_contextPurposeSERVER_AUTHr*   r   r   r   r(   r)   r}   r9   )r/   r   s    r0   r   zAioClient._connect  s     >>'#&#=#=KK33$5 '.'>'>IItyyd&6&6(8 "8
DJgo"8s$   BB5B1	"B5+B3,B53B5c                   K   | j                   j                  t        | j                  | j                  | j
                  | j                              }| j                  j                  |       	 | j                  j                  | j                         d {   }| j                   j                  |       	 t        | j                   j                               }	 t!        |t"              rt%        |j&                        t!        |t(              st%        d      |j*                  | _        d| _        y 7 # t        $ r Y nw xY wحw)N)r   targetr   extra_headersTi  )r-   r@   r   r   r   r   r   r)   rB   r(   rY   r   r`   nextrh   StopIterationr>   r   r   status_coder   r   r"   )r/   rC   rc   r,   s       r0   r5   zAioClient.handshake  s    77<<TYYtyy595F5F6:6I6I!K L 	

" JJOOD,>,>??GGG  )TWW^^-. e-.!%"3"344E#34!#&& ,, @ !  s7   BED5E8#D7 AE7	E EEEc                 x   K   t         |   ||       d {    | j                  j                          y 7 w)NrX   )r}   rS   r)   )r/   rQ   rR   r   s      r0   rS   zAioClient.close  s2     gm67m;;;

 	<s   :8 :)NNrs   NNN)NNrs   NNNNNrt   )ru   rv   rw   r   r1   r   r9   r   r5   rS   r   r   s   @r0   r   r   g  sM     8<JN!*6 ;?8<9=59& &P , r2   r   )r*   r   r   urllib.parser   wsprotor   r   wsproto.eventsr   r   r	   r
   r   r   r   r   r   wsproto.extensionsr   wsproto.frame_protocolr   wsproto.utilitiesr   errorsr   r   r   ry   r   r;   r2   r0   <module>r      s[     
  ! 0
 
 
 1 . 0 5[ [|n nbl lr2   