
    E                         S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSK7  SSK	7  SSK
7  SSK7  SSK7  SSK7  SSK7  SSK7  SS/r  " S S\5      rS\4S	 jrg)
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

    )print_functionN   )*	WebSocketcreate_connectionc                      \ rS rSrSr   S#S jrS rS rS rS r	S	 r
S
 rS r\" \\5      rS r\" \5      rS r\" \5      rS rS r\" \5      rS r\R0                  4S jrS rS rS$S jrS rS rS%S jrS%S jr S r!\"\#RH                  " S5      4S jr%\"\#RH                  " S5      S4S jr&S r'S r(S  r)S! r*S"r+g)&r   4   a}  
Low level WebSocket interface.
This class is based on
  The WebSocket protocol draft-hixie-thewebsocketprotocol-76
  http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76

We can connect to the websocket server and send/receive data.
The following example is an echo client.

>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.org")
>>> ws.send("Hello, Server")
>>> ws.recv()
'Hello, Server'
>>> ws.close()

get_mask_key: a callable to produce new mask keys, see the set_mask_key
  function's docstring for more details
sockopt: values for socket.setsockopt.
    sockopt must be tuple and each element is argument of sock.setsockopt.
sslopt: dict object for ssl socket option.
fire_cont_frame: fire recv event for each cont frame. default is False
enable_multithread: if set to True, lock send method.
skip_utf8_validation: skip utf8 validation.
Nc                 d   [        X#5      U l         SU l        SU l        SU l        Xl        [        U R                  U5      U l        [        XF5      U l        U(       a5  [        R                  " 5       U l        [        R                  " 5       U l        g[        5       U l        [        5       U l        g)z
Initialize WebSocket object.
NF)sock_opthandshake_responsesock	connectedget_mask_keyframe_buffer_recvcontinuous_frame
cont_frame	threadingLocklockreadlockNoLock)selfr   sockoptssloptfire_cont_frameenable_multithreadskip_utf8_validation_s           "lib/third_party/websocket/_core.py__init__WebSocket.__init__P   s     !1"&	((5IJ*3 !(DI%NN,DMDI"HDM    c              #   4   #     U R                  5       v   M  7f)zH
Allow iteration over websocket, implying sequential `recv` executions.
recvr   s    r    __iter__WebSocket.__iter__h   s      ))+ s   c                 "    U R                  5       $ Nr%   r'   s    r    __next__WebSocket.__next__o   s    yy{r#   c                 "    U R                  5       $ r+   )r,   r'   s    r    nextWebSocket.nextr   s    }}r#   c                 6    U R                   R                  5       $ r+   )r   filenor'   s    r    r2   WebSocket.filenou   s    yy!!r#   c                     Xl         g)a2  
set function to create musk key. You can customize mask key generator.
Mainly, this is for testing purpose.

func: callable object. the func takes 1 argument as integer.
      The argument means length of mask key.
      This func must return string(byte array),
      which length is argument specified.
N)r   )r   funcs     r    set_mask_keyWebSocket.set_mask_keyx   s
     !r#   c                 .    U R                   R                  $ )z$
Get the websocket timeout(second).
)r   timeoutr'   s    r    
gettimeoutWebSocket.gettimeout   s     }}$$$r#   c                 ~    XR                   l        U R                  (       a  U R                  R                  U5        gg)zC
Set the timeout to the websocket.

timeout: timeout time(second).
N)r   r9   r   
settimeout)r   r9   s     r    r=   WebSocket.settimeout   s,     !(99II  ) r#   c                 R    U R                   (       a  U R                   R                  $ g)z
get subprotocol
N)r   subprotocolr'   s    r    getsubprotocolWebSocket.getsubprotocol   s"     ""**666r#   c                 R    U R                   (       a  U R                   R                  $ g)z
get handshake status
N)r   statusr'   s    r    	getstatusWebSocket.getstatus   s"     ""**111r#   c                 R    U R                   (       a  U R                   R                  $ g)z
get handshake response header
N)r   headersr'   s    r    
getheadersWebSocket.getheaders   s"     ""**222r#   c                 J    [        U R                  [        R                  5      $ r+   )
isinstancer   ssl	SSLSocketr'   s    r    is_sslWebSocket.is_ssl   s    $))S]]33r#   c           
      0   UR                  SU R                  R                  5      U R                  l        [        XR                  [	        S0 UD6UR                  SS5      5      u  U l        n [        U R                  /UQ70 UD6U l        [        UR                  SS5      5       H  nU R                  R                  [        ;   d  M#  U R                  R                  S   nU R                  R                  5         [        XR                  [	        S0 UD6UR                  SS5      5      u  U l        n[        U R                  /UQ70 UD6U l        M     SU l        g!   U R                  (       a!  U R                  R                  5         SU l        e = f)	a  
Connect to url. url is websocket url scheme.
ie. ws://host:port/resource
You can customize using 'options'.
If you set "header" list object, you can set your own custom header.

>>> ws = WebSocket()
>>> ws.connect("ws://echo.websocket.org/",
        ...     header=["User-Agent: MyProgram",
        ...             "x-custom: header"])

timeout: socket timeout time. This value is integer.
         if you set None for this value,
         it means "use default_timeout value"

options: "header" -> custom http header list or dict.
         "cookie" -> cookie value.
         "origin" -> custom origin url.
         "suppress_origin" -> suppress outputting origin header.
         "host"   -> custom host header string.
         "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.
         "http_proxy_auth" - http proxy auth information.
                             tuple of username and password.
                             default is None
         "redirect_limit" -> number of redirects to follow.
         "subprotocols" - array of available sub protocols.
                          default is None.
         "socket" - pre-initialized stream socket.

r9   socketNredirect_limit   locationT )getr   r9   connect
proxy_infopopr   	handshaker   rangerD   SUPPORTED_REDIRECT_STATUSESrH   closer   )r   urloptionsaddrsattempts        r    rX   WebSocket.connect   sF   F !(It}}7L7L M"3z7LG7L#*;;x#>@	5	&/		&ME&MW&MD# -=q!AB**115PP1199*ECIIOO%(/]]JDYQXDY07Hd0K)M$DIu.7		.UE.UW.UD+ C "DN	yy		! 	s   /AE  BE   5Fc                 N    [         R                  X5      nU R                  U5      $ )z
Send the data as string.

payload: Payload must be utf-8 string or unicode,
          if the opcode is OPCODE_TEXT.
          Otherwise, it must be string(byte array)

opcode: operation code to send. Please see OPCODE_XXX.
)ABNFcreate_frame
send_frame)r   payloadopcodeframes       r    sendWebSocket.send   s#     !!'2u%%r#   c                 F   U R                   (       a  U R                   Ul         UR                  5       n[        U5      n[        S[	        U5      -   5        U R
                     U(       a  U R                  U5      nX$S nU(       a  M  SSS5        U$ ! , (       d  f       U$ = f)a  
Send the data frame.

frame: frame data created  by ABNF.create_frame

>>> ws = create_connection("ws://echo.websocket.org/")
>>> frame = ABNF.create_frame("Hello", ABNF.OPCODE_TEXT)
>>> ws.send_frame(frame)
>>> cont_frame = ABNF.create_frame("My name is ", ABNF.OPCODE_CONT, 0)
>>> ws.send_frame(frame)
>>> cont_frame = ABNF.create_frame("Foo Bar", ABNF.OPCODE_CONT, 1)
>>> ws.send_frame(frame)

zsend: N)r   formatlentracereprr   _send)r   rj   datalengthls        r    rg   WebSocket.send_frame   s     !%!2!2E||~Thd#$YYJJt$Bx $ 
  Y
 s   !$B
B c                 B    U R                  U[        R                  5      $ r+   )rk   re   OPCODE_BINARYr   rh   s     r    send_binaryWebSocket.send_binary  s    yy$"4"455r#    c                     [        U[        R                  5      (       a  UR                  S5      nU R	                  U[
        R                  5        g)z8
send ping data.

payload: data payload to send server.
utf-8N)rL   six	text_typeencoderk   re   OPCODE_PINGry   s     r    pingWebSocket.ping  7     gs}}--nnW-G		'4++,r#   c                     [        U[        R                  5      (       a  UR                  S5      nU R	                  U[
        R                  5        g)z8
send pong data.

payload: data payload to send server.
r~   N)rL   r   r   r   rk   re   OPCODE_PONGry   s     r    pongWebSocket.pong(  r   r#   c                 <   U R                      U R                  5       u  pSSS5        [        R                  (       a%  W[        R
                  :X  a  WR                  S5      $ W[        R
                  :X  d  U[        R                  :X  a  W$ g! , (       d  f       Ns= f)z[
Receive string data(byte array) from the server.

return value: string(byte array) value.
Nr~   r|   )r   	recv_datar   PY3re   OPCODE_TEXTdecoderx   )r   ri   rs   s      r    r&   WebSocket.recv2  sl     ]]>>+LF 77v!1!11;;w''t'''6T5G5G+GK ]s   B
Bc                 B    U R                  U5      u  p#X#R                  4$ )
Receive data with operation code.

control_frame: a boolean flag indicating whether to return control frame
data, defaults to False

return  value: tuple of operation code and string(byte array) value.
)recv_data_framers   )r   control_frameri   rj   s       r    r   WebSocket.recv_dataA  s#     ,,];zz!!r#   c                 x    U R                  5       nU(       d  [        SU-  5      eUR                  [        R                  [        R
                  [        R                  4;   ar  U R                  R                  U5        U R                  R                  U5        U R                  R                  U5      (       a  U R                  R                  U5      $ OUR                  [        R                  :X  a  U R                  5         UR                  U4$ UR                  [        R                  :X  aV  [        UR                   5      S:  a  U R#                  UR                   5        O[        S5      eU(       a  UR                  U4$ O3UR                  [        R$                  :X  a  U(       a  UR                  U4$ GM  )r   zNot a valid frame %s~   zPing message is too long)
recv_frameWebSocketProtocolExceptionri   re   r   rx   OPCODE_CONTr   validateaddis_fireextractOPCODE_CLOSE
send_closer   ro   rs   r   r   )r   r   rj   s      r    r   WebSocket.recv_data_frameM  sT    OO%E 1*U24 4$"2"2D4F4FHXHX!YY((/##E*??**511??22599 2 !2!22!||U**!1!11uzz?S(IIejj)424 4  <<.. !!1!11  <<..7 r#   c                 6    U R                   R                  5       $ )zF
receive data as frame from server.

return value: ABNF frame object.
)r   r   r'   s    r    r   WebSocket.recv_frames  s       ++--r#   c                     US:  d  U[         R                  :  a  [        S5      eSU l        U R	                  [
        R                  " SU5      U-   [         R                  5        g)z
send close data to the server.

status: status code to send. see STATUS_XXX.

reason: the reason to close. This must be string or bytes.
r   code is invalid rangeF!HN)re   	LENGTH_16
ValueErrorr   rk   structpackr   )r   rD   reasons      r    r   WebSocket.send_close{  sM     A:4>>1455		&++dF+f4d6G6GHr#   rT   c                    U R                   (       Ga  US:  d  U[        R                  :  a  [        S5      e SU l         U R	                  [
        R                  " SU5      U-   [        R                  5        U R                  R                  5       nU R                  R                  U5        [        R                  " 5       nUb  [        R                  " 5       U-
  U:  a   U R                  5       nUR                  [        R                  :w  a  MP  [        5       (       aH  [
        R                  " SUR                   SS 5      S   nU["        :w  a  [%        S['        U5      -   5         U R                  R                  U5        U R                  R)                  [*        R,                  5        U R)                  5         gg!    M[  = f!    N= f)z
Close Websocket object

status: status code to send. see STATUS_XXX.

reason: the reason to close. This must be string.

timeout: timeout until receive a close frame.
    If None, it will wait forever until receive a close frame.
r   r   Fr   N   zclose status: )r   re   r   r   rk   r   r   r   r   r:   r=   timer   ri   isEnabledForErrorunpackrs   STATUS_NORMALerrorrq   shutdownrR   	SHUT_RDWR)r   rD   r   r9   sock_timeout
start_timerj   recv_statuss           r    r^   WebSocket.close  sb    >>>zVt~~5 !899!&		&++dF3 !"&"3"35#yy335		$$W-!YY[
oz)AG)K
 $ 1 <<4+<+<<$,..*0--ejj1o*Nq*QK*m; %&6k9J&J K 		$$\2		""6#3#34 MMO9 *s2   B(G ".G  G AG  )AG  GG Gc                 z    U R                   (       a*  U R                  R                  [        R                  5        gg)zQ
Low-level asynchronous abort, wakes up other threads that are waiting in recv_*
N)r   r   r   rR   r   r'   s    r    abortWebSocket.abort  s(     >>IIv//0 r#   c                 x    U R                   (       a)  U R                   R                  5         SU l         SU l        gg)zclose socket, immediately.NF)r   r^   r   r'   s    r    r   WebSocket.shutdown  s*    99IIOODI"DN r#   c                 .    [        U R                  U5      $ r+   )rk   r   )r   rs   s     r    rr   WebSocket._send  s    DIIt$$r#   c                      [        U R                  U5      $ ! [         a;    U R                  (       a  U R                  R                  5         S U l        SU l        e f = f)NF)r&   r   "WebSocketConnectionClosedExceptionr^   r   )r   bufsizes     r    r   WebSocket._recv  sM    			7++1 	yy		!DI"DN	s
    AA)	r   r   r   r   r   r   r   r   r   )NNNFFF)r|   )F),__name__
__module____qualname____firstlineno____doc__r!   r(   r,   r/   r2   r6   r:   r=   propertyr9   rA   r@   rE   rD   rI   rO   rH   rX   re   r   rk   rg   rz   r   r   r&   r   r   r   r   r   br   r^   r   r   rr   r   __static_attributes__rV   r#   r    r   r   4   s   6 @D;@&+%0"
!%* z:.G >*K i F4 z"G5n $(#3#3 &86--
"$/L. !.ceeBi I )rA 'R1#%r#   c           	      6   UR                  S/ 5      nUR                  S0 5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nU" SXEUUUS.UD6n	U	R                  Ub  UO	[        5       5        U	R                  " U 40 UD6  U	$ )	aN  
connect to url and return websocket object.

Connect to url and return the WebSocket object.
Passing optional timeout parameter will set the timeout on the socket.
If no timeout is supplied,
the global default timeout setting returned by getdefauttimeout() is used.
You can customize using 'options'.
If you set "header" list object, you can set your own custom header.

>>> conn = create_connection("ws://echo.websocket.org/",
     ...     header=["User-Agent: MyProgram",
     ...             "x-custom: header"])


timeout: socket timeout time. This value is integer.
         if you set None for this value,
         it means "use default_timeout value"

class_: class to instantiate when creating the connection. It has to implement
        settimeout and connect. It's __init__ should be compatible with
        WebSocket.__init__, i.e. accept all of it's kwargs.
options: "header" -> custom http header list or dict.
         "cookie" -> cookie value.
         "origin" -> custom origin url.
         "suppress_origin" -> suppress outputting origin header.
         "host"   -> custom host header string.
         "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.
         "http_proxy_auth" - http proxy auth information.
                                tuple of username and password.
                                default is None
         "enable_multithread" -> enable lock for multithread.
         "redirect_limit" -> number of redirects to follow.
         "sockopt" -> socket options
         "sslopt" -> ssl option
         "subprotocols" - array of available sub protocols.
                          default is None.
         "skip_utf8_validation" - skip utf8 validation.
         "socket" - pre-initialized stream socket.
r   r   r   Fr   r   )r   r   r   r   r   rV   )rZ   r=   getdefaulttimeoutrX   )
r_   r9   class_r`   r   r   r   r   r   websocks
             r    r   r     s    V kk)R(G[[2&Fkk"3U;O %95A";;'=uE KW%4(:*>K CJKG '"5w;L;NOOOC#7#Nr#   )r   
__future__r   rR   r   r   r   r   _abnf_exceptions
_handshake_http_logging_socket_ssl_compat_utils__all__objectr   r   rV   r#   r    <module>r      sc   * &     
        +
,V Vr $(	 6r#   