a
   NP»N=p¯Lã                   @   s¢   d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZ ddlT ddlmZ dgZG d	d
„ d
ƒZG dd„ dƒZG dd„ deƒZdS )aL  
websocket - WebSocket client library for Python

Copyright (C) 2010 Hiroki Ohtani(liris)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1335  USA

é    Né   )ÚABNF)Ú	WebSocketÚgetdefaulttimeout)Ú*)Ú_loggingÚWebSocketAppc                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
Dispatcherc                 C   s   || _ || _d S ©N©ÚappÚping_timeout©Úselfr   r   © r   ú2/usr/lib/python3.9/site-packages/websocket/_app.pyÚ__init__,   s    zDispatcher.__init__c                 C   s@   | j jr<t | j jjfdd| j¡\}}}|r4|ƒ s4q<|ƒ  q d S ©Nr   )r   Úkeep_runningÚselectÚsockr   )r   r   Úread_callbackÚcheck_callbackÚrÚwÚer   r   r   Úread0   s    ÿ
zDispatcher.readN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r	   +   s   r	   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSSLDispatcherc                 C   s   || _ || _d S r
   r   r   r   r   r   r   :   s    zSSLDispatcher.__init__c                 C   s(   | j jr$|  ¡ }|r|ƒ sq$|ƒ  q d S r
   )r   r   r   )r   r   r   r   r   r   r   r   r   >   s    zSSLDispatcher.readc                 C   s6   | j jj}| ¡ r|gS t |fdd| j¡\}}}|S r   )r   r   Úpendingr   r   )r   r   r   r   r   r   r   r   r   F   s
    
zSSLDispatcher.selectN)r   r   r   r   r   r   r   r   r   r   r    9   s   r    c                   @   sZ   e Zd ZdZddd„Zejfdd„Zdd	„ Zd
d„ Z	ddd„Z
dd„ Zdd„ Zdd„ ZdS )r   zc
    Higher level of APIs are provided.
    The interface is like JavaScript WebSocket object.
    NTc                 C   sv   || _ |dur|ng | _|| _|| _|| _|| _|| _|| _|| _|| _	|	| _
d| _|| _d| _d| _d| _|| _dS )a­  
        url: websocket url.
        header: custom header for websocket handshake.
        on_open: callable object which is called at opening websocket.
          this function has one argument. The argument is this class object.
        on_message: callable 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 string which we get from the server.
        on_error: callable 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: callable object which is called when closed the connection.
         this function has one argument. The argument is this class object.
        on_cont_message: callback object which is called when receive continued
         frame data.
         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: 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: this parameter is obsolete and ignored.
        get_mask_key: a callable to produce new mask keys,
          see the WebSocket.set_mask_key's docstring for more information
        subprotocols: array 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)r   r"   r#   r%   r&   r(   r)   r*   r+   r,   r   r-   r$   r0   r'   r   r   r   r   U   s"    *zWebSocketApp.__init__c                 C   s$   | j r| j  ||¡dkr tdƒ‚dS )zÔ
        send message.
        data: message to send. If you set opcode to OPCODE_TEXT,
              data must be utf-8 string or unicode.
        opcode: operation code of data. default is OPCODE_TEXT.
        r   zConnection is already closed.N)r   ÚsendZ"WebSocketConnectionClosedException)r   ÚdataÚopcoder   r   r   r1   ’   s    ÿzWebSocketApp.sendc                 K   s(   d| _ | jr$| jjf i |¤Ž d| _dS )z-
        close websocket connection.
        FN)r   r   Úclose)r   Úkwargsr   r   r   r4   ž   s    zWebSocketApp.closec              
   C   sp   |  |¡slt ¡ | _| jr z| j ¡  W q  tyh } z(t d |¡¡ W Y d }~qlW Y d }~q d }~0 0 q d S )Nz send_ping routine terminated: {})	ÚwaitÚtimer.   r   ZpingÚ	Exceptionr   ZwarningÚformat)r   ÚintervalÚeventÚexr   r   r   Ú
_send_ping§   s    

zWebSocketApp._send_pingr   Fc                    s²  ˆdurˆdkrd‰ˆr,|r,|ˆkr,t dƒ‚|s4g }|s<i }ˆjrJt dƒ‚d‰dˆ_dˆ_dˆ_d‡ ‡‡fdd„	‰zàtˆj||ˆjdu|	|rdndd	ˆ_ˆj t	ƒ ¡ ˆjj
ˆjˆjˆj||||ˆj|
|||d
 |sâˆ ˆ¡}ˆ ˆj¡ |r"t ¡ ‰ tjˆj|ˆ fd‰ˆ d¡ ˆ ¡  ‡‡fdd„}‡‡fdd„}| ˆjj||¡ W nZ tttfy¬ } z:ˆ ˆj|¡ t|tƒr†‚ ˆƒ  t|tƒ W  Y d}~S d}~0 0 dS )a@  
        run event loop for WebSocket framework.
        This loop is infinite loop and is alive during websocket is available.
        sockopt: values for socket.setsockopt.
            sockopt must be tuple
            and each element is argument of sock.setsockopt.
        sslopt: ssl socket optional dict.
        ping_interval: automatically send "ping" command
            every specified period(second)
            if set to 0, not send automatically.
        ping_timeout: timeout(second) if the pong message is not received.
        http_proxy_host: http proxy host name.
        http_proxy_port: http proxy port. If not set, set to 80.
        http_no_proxy: host names, which doesn't use proxy.
        skip_utf8_validation: skip utf8 validation.
        host: update host header.
        origin: update origin header.
        dispatcher: customize reading data from socket.
        suppress_origin: suppress outputting origin header.

        Returns
        -------
        False if caught KeyboardInterrupt
        True if other exception was raised during a loop
        Nr   z#Ensure ping_interval > ping_timeoutzsocket is already openedTc                    sd   ˆrˆ  ¡ rˆ  ¡  ˆ ¡  dˆ_ˆjr2ˆj ¡  ˆ | r@| jnd¡}ˆjˆj	g|¢R Ž  dˆ_dS )z²
            Tears down the connection.
            If close_frame is set, we will invoke the on_close handler with the
            statusCode and reason from there.
            FN)
ZisAliveÚsetÚjoinr   r   r4   Ú_get_close_argsr2   Ú	_callbackr)   )Zclose_frameZ
close_args)r;   r   Úthreadr   r   Úteardowná   s    
ÿz*WebSocketApp.run_forever.<locals>.teardownF)ÚsockoptÚssloptZfire_cont_frameÚskip_utf8_validationZenable_multithread)r#   r$   Úhttp_proxy_hostÚhttp_proxy_portÚhttp_no_proxyÚhttp_proxy_authr0   ÚhostÚoriginÚsuppress_originÚ
proxy_type)ÚtargetÚargsc                     sô   ˆ j sˆƒ S ˆ j d¡\} }| tjkr.ˆ|ƒS | tjkrJˆ  ˆ j|j¡ n¦| tj	krpt
 
¡ ˆ _ˆ  ˆ j|j¡ n€| tjkr®ˆ jr®ˆ  ˆ j|j|j|j¡ ˆ  ˆ j|j|j¡ nB|j}tjrÎ| tjkrÎ| d¡}ˆ  ˆ j||jd¡ ˆ  ˆ j|¡ dS )NTúutf-8)r   r   Zrecv_data_framer   ZOPCODE_CLOSEZOPCODE_PINGrA   r*   r2   ZOPCODE_PONGr7   r/   r+   ZOPCODE_CONTr,   r'   r3   ZfinÚsixZPY3ÚOPCODE_TEXTÚdecoder&   )Zop_codeÚframer2   )r   rC   r   r   r     s.    



ÿÿ
z&WebSocketApp.run_forever.<locals>.readc                     sT   ˆ rPt   ¡ ˆj ˆ k} ˆjˆj dk }ˆjˆj ˆ k}ˆjrP| rP|sH|rPtdƒ‚dS )Nr   zping/pong timed outT)r7   r.   r/   ZWebSocketTimeoutException)Zhas_timeout_expiredZ$has_pong_not_arrived_after_last_pingZhas_pong_arrived_too_late)r   r   r   r   Úcheck&  s    ÿþþz'WebSocketApp.run_forever.<locals>.check)N)ZWebSocketExceptionr   r   r.   r/   r   r-   r,   Z
settimeoutr   Zconnectr"   r#   r$   r0   Úcreate_dispatcherrA   r%   Ú	threadingÚEventÚThreadr=   Ú	setDaemonÚstartr   r8   ÚKeyboardInterruptÚ
SystemExitr(   Ú
isinstance)r   rD   rE   Zping_intervalr   rG   rH   rI   rJ   rF   rK   rL   Ú
dispatcherrM   rN   r   rV   r   r   )r;   r   r   rC   rB   r   Úrun_forever±   sd    !
üú

ÿ
zWebSocketApp.run_foreverc                 C   s&   |pd}| j  ¡ rt| |ƒS t| |ƒS )Né
   )r   Zis_sslr    r	   )r   r   Útimeoutr   r   r   rW   ;  s    

zWebSocketApp.create_dispatcherc                 C   s¦   t jdk r,| jr&tt | j¡jƒdkrLg S n | jrHtt | j¡jƒdkrLg S |ržt|ƒdkrždt 	|dd… ¡ t 	|dd… ¡ }|dd…  
d¡}||gS ddgS )	z† this functions extracts the code, reason from the close body
        if they exists, and if the self.on_close except three arguments )é   r   rd   é   é   r   r   NrQ   )ÚsysÚversion_infor)   ÚlenÚinspectZ
getargspecrP   ZgetfullargspecrR   Zbyte2intrT   )r   r2   ÚcodeÚreasonr   r   r   r@   B  s    
(zWebSocketApp._get_close_argsc              
   G   sˆ   |r„z(t  |¡r||Ž  n|| g|¢R Ž  W nV ty‚ } z>t d ||¡¡ t ¡ rnt ¡ \}}}t	 
|¡ W Y d }~n
d }~0 0 d S )Nzerror from callback {}: {})rj   Zismethodr8   r   Úerrorr9   ZisEnabledForDebugrg   Úexc_infoÚ	tracebackÚprint_tb)r   ÚcallbackrP   r   Ú_Útbr   r   r   rA   T  s    

zWebSocketApp._callback)NNNNNNNNTNNNN)NNr   NNNNNFNNNFN)r   r   r   Ú__doc__r   r   rS   r1   r4   r=   ra   rW   r@   rA   r   r   r   r   r   O   s0          ú
=	
       ú
 )rt   rj   r   rg   rX   r7   ro   rR   Z_abnfr   Z_corer   r   Ú_exceptionsÚ r   Ú__all__r	   r    Úobjectr   r   r   r   r   Ú<module>   s   