
    iY                         S SK r S SKrS SKrS SKJr  S SKJr  S SKJrJr  S SK	J
r
JrJrJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S	S
KJrJr   " S S5      r " S S\5      r " S S\5      rg)    N)time)urlsplit)ConnectionTypeWSConnection)	AcceptConnectionRejectConnectionCloseConnectionMessageRequestPingPongTextMessageBytesMessage)PerMessageDeflate)CloseReason)LocalProtocolError   )ConnectionErrorConnectionClosedc                   V    \ rS rSr   SS jrS rS rSS jrSS jrS r	S	 r
S
 rSrg)Base   Nc	                    S U l         Xl        X0l        X@l        XPl        SU l        / U l        S U l        SU l        SU l	        U[        R                  :H  U l        [        R                  U l        S U l        Uc  SS Kn	U	R$                  nUc  SS Kn	U	R&                  nUc  [(        R*                  nXl        U" 5       U l        [1        U5      U l        U R5                  5         U R                  (       d
  [7        5       eU" U R8                  S9U l        U R:                  R<                  R?                  SS5      U R:                  l        U R:                  RA                  5         g )NTr   F)targetz	(_thread)z(simple_websocket.Base._thread))!subprotocolsock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	threadingThreadEvent	selectorsDefaultSelectorselector_classeventr   ws	handshaker   _threadthreadnamereplacestart)
selfr   connection_typer   r   r   thread_classevent_classr/   r*   s
             P/home/admin/cozy_coffee/venv/lib/python3.13/site-packages/simple_websocket/ws.py__init__Base.__init__   s%     	** 0! $$%!)^-B-BB'66!$++L#//K!&66N, ]
/~~!##"$,,7;;++33:<    c                     g N )r8   s    r<   r2   Base.handshakeB   s    r?   c                 b   U R                   (       d   [        U R                  U R                  5      e[	        U[
        5      (       a#  U R                  R                  [        US95      nO+U R                  R                  [        [        U5      S95      nU R                  R                  U5        g)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bytesr1   sendr
   r   strr   )r8   rE   out_datas      r<   rH   	Base.sendF   ss     ~~"4#4#4d6H6HIIdE""ww||G$67Hww||KSY$?@H		x r?   c                    U R                   (       an  U R                  (       d]  U R                  R                  US9(       d  gU R                  R	                  5         U R                   (       a  U R                  (       d  M]   U R                  R                  S5      $ ! [         a     Of = fU R                   (       d   [        U R                  U R                  5      eg)aM  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!   r0   waitclearpop
IndexErrorr   r(   r)   )r8   rM   s     r<   receiveBase.receiveU   s     nnT%6%6::??7?3JJ nnT%6%6%6	$$((++ 		~~"4#4#4d6H6HII s   B 
B)(B)c                 D   U R                   (       d   [        U R                  U R                  5      eU R                  R                  [        U=(       d    [        R                  U5      5      n U R                  R                  U5        SU l         g! [         a     Nf = f)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)   r1   rH   r	   r   NORMAL_CLOSUREr   BrokenPipeError)r8   reasonmessagerJ   s       r<   close
Base.closej   s     ~~"4#4#4d6H6HII77<<0k00'!; <	IINN8$   		s   /B 
BBc                     g rA   rB   )r8   requests     r<   choose_subprotocolBase.choose_subprotocol|   s     r?   c                    S nU R                   (       aS  [        5       U R                   -   nU R                  5       n UR                  U R                  [
        R                  S5        U R                  (       GaY   U(       a  [        5       nWU::  d  UR                  X#-
  5      (       d  U R                  (       d9  U R                  [        R                  SS9  U R                  R                  5         OSU l
        U R                  R!                  U R"                  R!                  [%        5       5      5        ['        X25      U R                   -   nM  U R                  R)                  U R*                  5      n[-        U5      S:X  a
  [/        5       eU R"                  R1                  U5        U R3                  5       U l        U R                  (       a  GMY  U(       a  UR                  5       OS   U R                  R                  5         g ! [         a    SU l         GNf = f! [.        [4        [6        4 a%    SU l        U R                  R                  5          M  f = f)NTFzPing/Pong timeoutrW   rX   r   )r   r   r/   registerr   r-   
EVENT_READ
ValueErrorr$   selectr    rY   r   POLICY_VIOLATIONr0   setrH   r1   r   maxrecvr   lenOSErrorreceive_data_handle_eventsConnectionResetErrorr   )r8   sel	next_pingnowin_datas        r<   r3   Base._thread   s   !3!33I%%'C'TYY	(<(<dC nnn&C C'szz)//J/J#11 JJk.J.J/B ' D JJNN,!-2*		tww||DF';<$'$7$:L:L$L	 ))..););<w<1$!)O$$W-!%!4!4!6' nnn2 				;  '!&'. 1&( !&

 	s2   +H :A8H 3AH A.H HH6IIc           	      |	   SnSnU R                   R                  5        GHF  n [        U[        5      (       aP  U R	                  U5      U l        X R                   R                  [        U R
                  [        5       /S95      -  nMj  [        U[        5      (       a  U R                  (       a+  X R                   R                  UR                  5       5      -  nUR                  U l        UR                  U l        SU l        U R"                  R%                  5         SnGM  [        U[&        5      (       a.  X R                   R                  UR                  5       5      -  nGMF  [        U[(        5      (       a
  SU l        GMe  [        U[,        [.        45      (       Ga  U =R0                  [3        UR4                  5      -  sl        U R6                  (       an  U R0                  U R6                  :  aT  X R                   R                  [        [8        R:                  S5      5      -  nU R"                  R%                  5         Sn  GO#U R<                  c  UR4                  U l        O[        U[,        5      (       a  [        U R<                  [>        5      (       d6  [?        U R<                  UR4                  -   RA                  5       5      U l        OU =R<                  UR4                  RA                  5       -  sl        Of[        U R<                  [>        5      (       d(  [?        U R<                  UR4                  -   5      U l        OU =R<                  UR4                  -  sl        URB                  (       d  GMY  [        U R<                  [D        [F        45      (       a&  U RH                  RK                  U R<                  5        Ow[        U[,        5      (       a4  U RH                  RK                  U R<                  RM                  5       5        O.U RH                  RK                  [G        U R<                  5      5        S U l        SU l        U R"                  R%                  5         GMF  GMI     U(       a  U RP                  R                  U5        U$ ! [N         a#    SnU R"                  R%                  5         Sn GM  f = f)NTr?   )r   
extensionsFzMessage is too bigr   ))r1   eventsrF   r   r]   r   rH   r   r   r	   r&   responsecoder(   rW   r)   r$   r0   rf   r   r   r    r   r   r#   ri   rE   r   r   MESSAGE_TOO_BIGr"   	bytearrayencodemessage_finishedrI   rG   r!   appenddecoder   r   )r8   
keep_goingrJ   r0   s       r<   rl   Base._handle_events   sj   
WW^^%EF#eW--'+'>'>u'ED$-=$($4$4$5$7#8.: !; ;H  77~~ GGLL1A$BB(-

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

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

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

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

 "
#s;   A#R	BR"A R%RC RD.R8C*R(R;:R;)r)   r(   r$   r0   r"   r#   r!   r&   r   r   r    r   r/   r   r   r4   r1   NN   NNNNNrA   NN)__name__
__module____qualname____firstlineno__r=   r2   rH   rR   rY   r]   r3   rl   __static_attributes__rB   r?   r<   r   r      s7    FJ6:EI&P!J*$$LMr?   r   c                   \   ^  \ rS rSrSr   SU 4S jjr\   SS j5       rS rS r	Sr
U =r$ )	Server   zThis 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	                 *  > Xl         U=(       d    / U l        [        U R                  [        5      (       a  U R                  /U l        SU l        S n	SU;   a  UR                  S5      n	SU l        OSU;   a  UR                  S5      n	SU l        OSU;   a'  UR                  S5      R                  5       n	SU l        OUR                  SS	5      R                  S
5      (       ar  US   n
[        U
S5      (       d  [        U
S5      (       a  U
R                  n
[        U
S5      (       a.  U
R                  R                  n	 U	R                  5       n	S
U l        U	c  [        S5      e[        TU ]A  U	["        R$                  UUUXgUS9  g ! [         a     NCf = f)Nunknownzwerkzeug.socketwerkzeugzgunicorn.socketgunicornzeventlet.inputeventletSERVER_SOFTWARE geventz
wsgi.inputrawrfilez+Cannot obtain socket from WSGI environment.)r9   r   r   r   r:   r;   r/   )environsubprotocolsrF   rI   modeget
get_socket
startswithhasattrr   r   _sockdupNotImplementedErrorRuntimeErrorsuperr=   r   r%   )r8   r   r   r   r   r   r:   r;   r/   r   
wsgi_input	__class__s              r<   r=   Server.__init__   s    (.Bd''--!%!2!2 3D	';;01D"DI');;01D"DI(;;/0;;=D"DI[[*B/::  !.J:u--'*g2N2N'--
z5))!~~++88:D %	<LMM~/D/D'4'4*:&2(6 	 	8 + s   >F 
FFc	                     U " XX4UXgUS9$ )aC  Accept a WebSocket connection from a client.

:param environ: A WSGI ``environ`` dictionary with the request details.
                Among other things, this class expects to find the
                low-level network socket for the connection somewhere
                in this dictionary. Since the WSGI specification does
                not cover where or how to store this socket, each web
                server does this in its own different way. Werkzeug,
                Gunicorn, Eventlet and Gevent are the only web servers
                that are currently supported.
: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``.
:param thread_class: The ``Thread`` class to use when creating
                     background threads. The default is the
                     ``threading.Thread`` class from the Python
                     standard library.
:param event_class: The ``Event`` class to use when creating event
                    objects. The default is the `threading.Event``
                    class from the Python standard library.
:param selector_class: The ``Selector`` class to use when creating
                       selectors. The default is the
                       ``selectors.DefaultSelector`` class from the
                       Python standard library.
)r   r   r   r   r:   r;   r/   rB   )	clsr   r   r   r   r   r:   r;   r/   s	            r<   acceptServer.accept*  s     P 7!.$4 ,"0	2 	2r?   c                    SnU R                   R                  5        Hv  u  p#UR                  S5      (       d  M  SR                  USS  R	                  S5       Vs/ s H  oDR                  5       PM     sn5      nX SU S3R                  5       -  nMx     US-  nU R                  R                  U5        U R                  5       U l
        g s  snf )	Ns   GET / HTTP/1.1
HTTP_-   _z: z
s   
)r   itemsr   joinsplit
capitalizerz   r1   rk   rl   r$   )r8   rq   keyvaluepheaders         r<   r2   Server.handshakeX  s    ',,,,.JC~~g&&3qr7==;M"N;Ma<<>;M"NOXRwd3::<< / 	7W%,,.	 #Os   C
c                 P    UR                    H  nX R                   ;   d  M  Us  $    g)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.
N)r   )r8   r\   r   s      r<   r]   Server.choose_subprotocolb  s+     #//K///"" 0 r?   )r$   r   r   r   )Nr   NNNNN)r   r   r   r   __doc__r=   classmethodr   r2   r]   r   __classcell__r   s   @r<   r   r      sH     BFIM26)8V >BGK04+2 +2Z/ r?   r   c                   f   ^  \ rS rSrSr   SU 4S jjr\   SS j5       rS rS	U 4S jjr	Sr
U =r$ )
Clientit  zThis 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
           
        > [        U5      n
U
R                  S;   nU
R                  U l        U
R                  =(       d    U(       a  SOSU l        U
R
                  U l        U
R                  (       a"  U =R
                  SU
R                  -   -  sl        U=(       d    / U l        [        U R                  [        5      (       a  U R                  /U l        / U l
        [        U[        5      (       a6  UR                  5        H!  u  pU R                  R                  X45        M#     O[        U[        5      (       a  X0l
        [        R                   " U R                  U R                  [        R"                  S9n[%        U5      S:X  a
  ['        5       e[        R                  " US   S   US   S   US   S   5      nU(       aH  Uc,  [(        R*                  " [(        R,                  R.                  S	9nUR1                  XR                  S
9nUR3                  US   S   5        [4        TU ]m  U[8        R:                  UUUXS9  g )N)httpswssi  P   ?)typer   r      )purpose)server_hostname   )r9   r   r   r   r:   r;   )r   schemehostnamehostportpathqueryr   rF   rI   extra_headeersdictr   r|   listsocketgetaddrinfoSOCK_STREAMri   r   sslcreate_default_contextPurposeSERVER_AUTHwrap_socketconnectr   r=   r   CLIENT)r8   urlr   headersr   r   r   ssl_contextr:   r;   
parsed_url	is_securer   r   connection_argsr   r   s                   r<   r=   Client.__init__{  s    c]
%%)99	''	OOAyb	OO	IIz////I(.Bd''--!%!2!2 3D gt$$%mmo
##**C<8 .&&") ,,TYY		282D2DF1$!##}}_Q/2OA4Fq4I,Q/24"!88KK335**4*KD_Q'*+~/D/D'4'4*:&2	 	 	Mr?   c
                     U " XUXEXgXS9	$ )aG  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.
:param thread_class: The ``Thread`` class to use when creating
                     background threads. The default is the
                     ``threading.Thread`` class from the Python
                     standard library.
:param event_class: The ``Event`` class to use when creating event
                    objects. The default is the `threading.Event``
                    class from the Python standard library.
)r   r   r   r   r   r   r:   r;   rB   )
r   r   r   r   r   r   r   r   r:   r;   s
             r<   r   Client.connect  s!    R 37!.$4 ,G 	Gr?   c           	         U R                   R                  [        U R                  U R                  U R
                  U R                  S95      nU R                  R                  U5         U R                  R                  U R                  5      nU R                   R                  U5         [        U R                   R                  5       5      n [        U[        5      (       a  [!        UR"                  5      e[        U[$        5      (       d  [!        S5      eUR&                  U l        SU l        g ! [         a     Of = fM  )N)r   r   r   extra_headersTi  )r1   rH   r   r   r   r   r   r   rh   r   rk   nextru   StopIterationrF   r   r   status_coder   r   r$   )r8   rJ   rq   r0   s       r<   r2   Client.handshake  s    77<<TYYtyy595F5F6:6I6I!K L 			x iinnT%7%78GGG  )TWW^^-. e-..!%"3"344E#344!#&& ,, !  s   +#D2 2
D?>D?c                 T   > [         TU ]  XS9  U R                  R                  5         g )Nr`   )r   rY   r   )r8   rW   rX   r   s      r<   rY   Client.close  s    V5		r?   )r$   r   r   r   r   r   r   r   r   )r   r   r   r   r   r=   r   r   r2   rY   r   r   r   s   @r<   r   r   t  sI     8<JNBF%MN 59IMAE+G +GZ, r?   r   )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   r   r   rB   r?   r<   <module>r      s^      
  ! 0
 
 
 1 . 0 5\ \~yT yxtT tr?   