HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/websocket/__pycache__/_app.cpython-310.pyc
o

���a�:�@s�dZ	ddlZddlZddlZddlZddlZddlmZddlm	Z	m
Z
ddlTddlm
Z
dgZGd	d
�d
�ZGdd�d�ZGd
d�d�ZdS)z

�N�)�ABNF)�	WebSocket�getdefaulttimeout)�*)�_logging�WebSocketAppc@s eZdZdZdd�Zdd�ZdS)�
Dispatcherz
    Dispatcher
    cC�||_||_dS�N��app�ping_timeout��selfr
r�r�0/usr/lib/python3/dist-packages/websocket/_app.py�__init__)�
zDispatcher.__init__cCs\|jjr,t��}|�|jjjtj�|�|j�}|r|�sdS|�|�	�|jjsdSdSr)
r
�keep_running�	selectors�DefaultSelector�register�sock�
EVENT_READ�selectr�close)rr�
read_callback�check_callback�sel�rrrr�read-s�zDispatcher.readN)�__name__�
__module__�__qualname__�__doc__rr!rrrrr	%sr	c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�
SSLDispatcherz
    SSLDispatcher
    cCr
rrrrrrr>rzSSLDispatcher.__init__cCs4|jjr|��}|r|�sdS|�|jjsdSdSr)r
rr)rrrrr rrrr!Bs�zSSLDispatcher.readcCs^|jjj}|��r|gSt��}|�|tj�|�|j�}|�	�t
|�dkr-|ddSdS)Nr)r
r�pendingrrrrrrr�len)rrrr rrrrJs
�zSSLDispatcher.selectN)r"r#r$r%rr!rrrrrr&:s
r&c@sxeZdZdZ							ddd�Zejfdd�Zdd	�Zd
d�Z				
					ddd�Z
dd�Zdd�Zdd�Z
dS)rz_
    Higher level of APIs are provided. The interface is like JavaScript WebSocket object.
    NTcCsv||_|dur	|ng|_||_||_||_||_||_||_||_||_	|	|_
d|_||_d|_
d|_d|_|
|_dS)a�	
        WebSocketApp initialization

        Parameters
        ----------
        url: str
            Websocket url.
        header: list or dict
            Custom header for websocket handshake.
        on_open: function
            Callback object which is called at opening websocket.
            on_open has one argument.
            The 1st argument is this class object.
        on_message: function
            Callback object which is called when received data.
            on_message has 2 arguments.
            The 1st argument is this class object.
            The 2nd argument is utf-8 data received from the server.
        on_error: function
            Callback object which is called when we get error.
            on_error has 2 arguments.
            The 1st argument is this class object.
            The 2nd argument is exception object.
        on_close: function
            Callback object which is called when connection is closed.
            on_close has 3 arguments.
            The 1st argument is this class object.
            The 2nd argument is close_status_code.
            The 3rd argument is close_msg.
        on_cont_message: function
            Callback object which is called when a continuation
            frame is received.
            on_cont_message has 3 arguments.
            The 1st argument is this class object.
            The 2nd argument is utf-8 string which we get from the server.
            The 3rd argument is continue flag. if 0, the data continue
            to next frame data
        on_data: function
            Callback object which is called when a message received.
            This is called before on_message or on_cont_message,
            and then on_message or on_cont_message is called.
            on_data has 4 argument.
            The 1st argument is this class object.
            The 2nd argument is utf-8 string which we get from the server.
            The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.
            The 4th argument is continue flag. If 0, the data continue
        keep_running: bool
            This parameter is obsolete and ignored.
        get_mask_key: function
            A callable function to get new mask keys, see the
            WebSocket.set_mask_key's docstring for more information.
        cookie: str
            Cookie value.
        subprotocols: list
            List of available sub protocols. Default is None.
        NFr)�url�header�cookie�on_open�
on_message�on_data�on_error�on_close�on_ping�on_pong�on_cont_messager�get_mask_keyr�last_ping_tm�last_pong_tm�subprotocols)rr)r*r,r-r/r0r1r2r3rr4r+r7r.rrrr^s"?
zWebSocketApp.__init__cCs$|jr|j�||�dkrtd��dS)a
        send message

        Parameters
        ----------
        data: str
            Message to send. If you set opcode to OPCODE_TEXT,
            data must be utf-8 string or unicode.
        opcode: int
            Operation code of data. Default is OPCODE_TEXT.
        rzConnection is already closed.N)r�send�"WebSocketConnectionClosedException)r�data�opcoderrrr8�s

��zWebSocketApp.sendcKs,d|_|jr|jjdi|��d|_dSdS)z-
        Close websocket connection.
        FNr)rrr)r�kwargsrrrr�s

�zWebSocketApp.closec
Cst|�|�s8t��|_|jr1z|j�|�Wnty0}zt�d�|��WYd}~dSd}~ww|�|�rdSdS)Nz send_ping routine terminated: {})	�wait�timer5r�ping�	Exceptionr�warning�format)r�interval�event�payload�exrrr�
_send_ping�s

���zWebSocketApp._send_pingr�Fcs��dur�dkrtd��|dur|dkrtd���r$|r$|�kr$td��|s(g}|s,i}�jr3td��d�d�_d�_d�_d���fdd	�	�zkt�j||�jdu|
dd
��_�j�t	���jj
�j�j�j
||||	�j||||d�|
s{����}
���j�|r�t���tj�j|�|fd��d�_�����fd
d�}��fdd�}|
��jj||�WdStttfy�}z���j|�t|t�rʂ��t|t�WYd}~Sd}~ww)a�
        Run event loop for WebSocket framework.

        This loop is an infinite loop and is alive while websocket is available.

        Parameters
        ----------
        sockopt: tuple
            Values for socket.setsockopt.
            sockopt must be tuple
            and each element is argument of sock.setsockopt.
        sslopt: dict
            Optional dict object for ssl socket option.
        ping_interval: int or float
            Automatically send "ping" command
            every specified period (in seconds).
            If set to 0, no ping is sent periodically.
        ping_timeout: int or float
            Timeout (in seconds) if the pong message is not received.
        ping_payload: str
            Payload message to send with each ping.
        http_proxy_host: str
            HTTP proxy host name.
        http_proxy_port: int or str
            HTTP proxy port. If not set, set to 80.
        http_no_proxy: list
            Whitelisted host names that don't use the proxy.
        skip_utf8_validation: bool
            skip utf8 validation.
        host: str
            update host header.
        origin: str
            update origin header.
        dispatcher: Dispatcher object
            customize reading data from socket.
        suppress_origin: bool
            suppress outputting origin header.

        Returns
        -------
        teardown: bool
            False if caught KeyboardInterrupt, True if other exception was raised during a loop
        NrzEnsure ping_timeout > 0zEnsure ping_interval >= 0z#Ensure ping_interval > ping_timeoutzsocket is already openedTcsb�r���r������d�_�jr�j����|r|nd�\}}d�_���j||�dS)a
            Tears down the connection.

            Parameters
            ----------
            close_frame: ABNF frame
                If close_frame is set, the on_close handler is invoked
                with the statusCode and reason from the provided frame.
            FN)	�is_alive�set�joinrrr�_get_close_args�	_callbackr0)�close_frame�close_status_code�close_reason)rDr�threadrr�teardowns

�z*WebSocketApp.run_forever.<locals>.teardown)�sockopt�sslopt�fire_cont_frame�skip_utf8_validation�enable_multithread)r*r+�http_proxy_host�http_proxy_port�
http_no_proxy�http_proxy_authr7�host�origin�suppress_origin�
proxy_type)�target�argscs��js��S�j�d�\}}|tjkr�|�S|tjkr&���j|j�dS|tj	kr:t
�
��_���j|j�dS|tj
krZ�jrZ���j|j|j|j����j|j|j�dS|j}|tjkrg|�d�}���j||jd����j|�dS)NT�utf-8)rr�recv_data_framer�OPCODE_CLOSE�OPCODE_PINGrMr1r:�OPCODE_PONGr>r6r2�OPCODE_CONTr3r.r;�fin�OPCODE_TEXT�decoder-)�op_code�framer:)rrRrrr!Ks4


�

���	�

z&WebSocketApp.run_forever.<locals>.readcsT�r(t���j�k}�j�jdk}�j�j�k}�jr(|r(|s$|r(td��dS)Nrzping/pong timed outT)r>r5r6�WebSocketTimeoutException)�has_timeout_expired�$has_pong_not_arrived_after_last_ping�has_pong_arrived_too_late)rrrr�checkes���z'WebSocketApp.run_forever.<locals>.checkr)�WebSocketExceptionrrr5r6rr4r3�
settimeoutr�connectr)r*r+r7�create_dispatcherrMr,�	threading�Event�ThreadrG�daemon�startr!r@�KeyboardInterrupt�
SystemExitr/�
isinstance)rrSrT�
ping_intervalr�ping_payloadrXrYrZr[rVr\r]�
dispatcherr^r_r!rq�er)rDrrrRrQr�run_forever�sl4��
�
��zWebSocketApp.run_forevercCs&|pd}|j��rt||�St||�S)N�
)r�is_sslr&r	)rr�timeoutrrrruzs


zWebSocketApp.create_dispatchercCsb|jr|s	ddgS|jr-t|j�dkr-d|jd|jd}|jdd��d�}||gSddgS)z�
        _get_close_args extracts the close code and reason from the close body
        if it exists (RFC6455 says WebSocket Connection Close Code is optional)
        N��rrrb)r0r:r(rj)rrNrO�reasonrrrrL�s
zWebSocketApp._get_close_argsc
Gsx|r:z||g|�R�WdSty9}z t�d�||��|jr.|�||�WYd}~dSWYd}~dSd}~wwdS)Nzerror from callback {}: {})r@r�errorrBr/)r�callbackrar�rrrrM�s����zWebSocketApp._callback)
NNNNNNNNTNNNN)NNrNrHNNNNFNNNFN)r"r#r$r%rrrir8rrGr�rurLrMrrrrrYs4
�R	

�')r%r�sysrvr>�	traceback�_abnfr�_corerr�_exceptionsrHr�__all__r	r&rrrrr�<module>s