3 gY+@szdZddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z ddl Tddl TdgZGdddeZdS) 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) WebSocketgetdefaulttimeout)* WebSocketAppc @sReZdZdZdddZejfddZdd Zd d Z dddZ ddZ ddZ dS)rzc Higher level of APIs are provided. The interface is like JavaScript WebSocket object. NTcCsv||_|dk r|ng|_| |_||_||_||_||_||_||_||_ | |_ | |_ | |_ d|_ d|_d|_| |_dS)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: a boolean flag indicating whether the app's main loop should keep running, defaults to True 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. Nr)urlheadercookieon_open on_messageon_dataon_erroron_closeon_pingon_pongon_cont_message keep_running get_mask_keysock last_ping_tm last_pong_tm subprotocols)selfrr r r rrrrrrrr rr r?/Users/olari/OneDrive/sandbox/awsBlog2/lambda/websocket/_app.py__init__0s"+zWebSocketApp.__init__cCs&|j s|jj||dkr"tddS)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. rzConnection is already closed.N)rsend"WebSocketConnectionClosedException)rdataopcoderrrrmszWebSocketApp.sendcKsd|_|jr|jjf|dS)z- close websocket connection. FN)rrclose)rkwargsrrrr!yszWebSocketApp.closecCsfx`|j|s`tj|_|jry|jjWqtk r\}ztdj|PWYdd}~XqXqWdS)Nz send_ping routine terminated: {})waittimerrping Exceptionwarningformat)rintervaleventexrrr _send_pings  zWebSocketApp._send_pingrFc Cs| s|dkrd}|r*|r*||kr*td|dkr6g}|dkrBi}|jrPtdd} d} zHyt|j|||jrtdpvd| d|_|jjt|jj|j|j |j |||||j | | d |j |j |rtj}tj|j||fd } | jd| jx^|jjrTtj|jjfff|\}}}|js(P|r|jjd\}}|tjkrR|} Pn|tjkrp|j |j|jn|tjkrtj|_|j |j |jn|tj!kr|jr|j |j"||j#|j$|j |j|j|j$nF|j}t%j&r|tj'kr|j(d }|j |j"||j#d|j |j)||r|j*rtj|j*|kr|j*|j|krt+d qWWnFt,t-t.fk r}z"|j |j/|t0|t.rWYdd}~XnXWd| r| j1r|j2| j3d|_|jj4|j5| r| jnd}|j |j6f|d|_XdS) aH 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. rNz#Ensure ping_interval > ping_timeoutzsocket is already openedTF)sockoptssloptfire_cont_frameskip_utf8_validation) r r http_proxy_hosthttp_proxy_port http_no_proxyhttp_proxy_authrhostorigin)targetargszutf-8zping/pong timed out)7WebSocketExceptionrrrr settimeoutrconnectrr r r _callbackr threadingEventThreadr, setDaemonstart connectedselectrrecv_data_framer OPCODE_CLOSE OPCODE_PINGrr OPCODE_PONGr$rr OPCODE_CONTr r finsixPY3 OPCODE_TEXTdecoder rWebSocketTimeoutExceptionr&KeyboardInterrupt SystemExitr isinstanceisAlivesetjoinr!_get_close_argsr)rr-r.Z ping_intervalZ ping_timeoutr1r2r3r4r0r5r6threadZ close_framer*rweop_codeframerZ close_argsrrr run_forevers                zWebSocketApp.run_forevercCsddl}tjdkr6|j s0t|j|jjdkrXgSn"|j sTt|j|jjdkrXgS|rt|dkrdtj |ddtj |dd}|ddj d}||gSddgS) z this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments rNrzutf-8)r]r) inspectsys version_inforlen getargspecr8getfullargspecrJbyte2intrM)rrr`codereasonrrrrUs (zWebSocketApp._get_close_argscGsl|rhy||f|WnPtk rf}z4tdj||trVtj\}}}tj|WYdd}~XnXdS)Nzerror from callback {}: {})r&errorr(ZisEnabledForDebugraexc_info tracebackprint_tb)rcallbackr8rY_tbrrrr< szWebSocketApp._callback) NNNNNNNNTNNNN) NNrNNNNNFNN) __name__ __module__ __qualname____doc__rrrLrr!r,r\rUr<rrrrr*s& 7   f)rsrCrar=r$rkrJ_abnfr_corerr _exceptions_logging__all__objectrrrrrs