Vous êtes sur la page 1sur 3

11/3/2014

Super simple websockets client/server using Python. Compatible with the draft 76 challenge/response.

geoffb / simple_websocket_client.html
Last active a day ago

Super simple websockets client/server using Python. Compatible with the draft 76 challenge/response.

simple_websocket_client.html
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 < ! D O C T Y P Eh t m l > < h t m ll a n g = " e n " > < h e a d > < t i t l e > W e b S o c k e tC l i e n t < / t i t l e > < s t y l e > # o u t p u t{ b o r d e r :s o l i d1 p x# 0 0 0 ; } < / s t y l e > < / h e a d > < b o d y > < f o r mi d = " f o r m " > < i n p u tt y p e = " t e x t "i d = " m e s s a g e " > < b u t t o nt y p e = " s u b m i t " > S e n d < / b u t t o n > < / f o r m > < h r > < d i vi d = " o u t p u t " > < / d i v > < s c r i p t > v a ri n p u t B o x=d o c u m e n t . g e t E l e m e n t B y I d ( " m e s s a g e " ) ; v a ro u t p u t=d o c u m e n t . g e t E l e m e n t B y I d ( " o u t p u t " ) ; v a rf o r m=d o c u m e n t . g e t E l e m e n t B y I d ( " f o r m " ) ; t r y{ v a rh o s t=" w s : / / "+w i n d o w . l o c a t i o n . h o s t n a m e+" : 9 8 7 6 / s t u f f " ; c o n s o l e . l o g ( " H o s t : " ,h o s t ) ; v a rs=n e wW e b S o c k e t ( h o s t ) ; s . o n o p e n=f u n c t i o n( e ){ c o n s o l e . l o g ( " S o c k e to p e n e d . " ) ; } ; s . o n c l o s e=f u n c t i o n( e ){ c o n s o l e . l o g ( " S o c k e tc l o s e d . " ) ; } ; s . o n m e s s a g e=f u n c t i o n( e ){ c o n s o l e . l o g ( " S o c k e tm e s s a g e : " ,e . d a t a ) ; v a rp=d o c u m e n t . c r e a t e E l e m e n t ( " p " ) ; p . i n n e r H T M L=e . d a t a ; o u t p u t . a p p e n d C h i l d ( p ) ; } ; s . o n e r r o r=f u n c t i o n( e ){ c o n s o l e . l o g ( " S o c k e te r r o r : " ,e ) ; } ; }c a t c h( e x ){ c o n s o l e . l o g ( " S o c k e te x c e p t i o n : " ,e x ) ; } f o r m . a d d E v e n t L i s t e n e r ( " s u b m i t " ,f u n c t i o n( e ){ e . p r e v e n t D e f a u l t ( ) ; s . s e n d ( i n p u t B o x . v a l u e ) ; i n p u t B o x . v a l u e=" " ; } ,f a l s e )

HTML

https://gist.github.com/geoffb/616117

1/3

11/3/2014
6 3 6 4 6 5 6 6 6 7

Super simple websockets client/server using Python. Compatible with the draft 76 challenge/response.

< / s c r i p t > < / b o d y > < / h t m l >

simple_websocket_server.py
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 # ! / u s r / b i n / e n vp y t h o n i m p o r ts o c k e t ,s t r u c t ,h a s h l i b ,t h r e a d i n g ,c g i d e fd e c o d e _ k e y( k e y ) : n u m=" " s p a c e s=0 f o rci nk e y : i fc . i s d i g i t ( ) : n u m+ =c i fc . i s s p a c e ( ) : s p a c e s+ =1 r e t u r ni n t ( n u m )/s p a c e s d e fc r e a t e _ h a s h( k e y 1 ,k e y 2 ,c o d e ) : a=s t r u c t . p a c k ( " > L " ,d e c o d e _ k e y ( k e y 1 ) ) b=s t r u c t . p a c k ( " > L " ,d e c o d e _ k e y ( k e y 2 ) ) m d 5=h a s h l i b . m d 5 ( a+b+c o d e ) r e t u r nm d 5 . d i g e s t ( ) d e fr e c v _ d a t a( c l i e n t ,l e n g t h ) : d a t a=c l i e n t . r e c v ( l e n g t h ) i fn o td a t a :r e t u r nd a t a r e t u r nd a t a . d e c o d e ( ' u t f 8 ' ,' i g n o r e ' ) d e fs e n d _ d a t a( c l i e n t ,d a t a ) : m e s s a g e=" \ x 0 0 % s \ x F F "%d a t a . e n c o d e ( ' u t f 8 ' ) r e t u r nc l i e n t . s e n d ( m e s s a g e ) d e fp a r s e _ h e a d e r s( d a t a ) : h e a d e r s={ } l i n e s=d a t a . s p l i t l i n e s ( ) f o rli nl i n e s : p a r t s=l . s p l i t ( " :" ,1 ) i fl e n ( p a r t s )= =2 : h e a d e r s [ p a r t s [ 0 ] ]=p a r t s [ 1 ] h e a d e r s [ ' c o d e ' ]=l i n e s [ l e n ( l i n e s )-1 ] r e t u r nh e a d e r s d e fh a n d s h a k e( c l i e n t ) : p r i n t' H a n d s h a k i n g . . . ' d a t a=c l i e n t . r e c v ( 1 0 2 4 ) h e a d e r s=p a r s e _ h e a d e r s ( d a t a ) p r i n t' G o th e a d e r s : ' f o rk ,vi nh e a d e r s . i t e r i t e m s ( ) : p r i n tk ,' : ' ,v d i g e s t=c r e a t e _ h a s h ( h e a d e r s [ ' S e c W e b S o c k e t K e y 1 ' ] , h e a d e r s [ ' S e c W e b S o c k e t K e y 2 ' ] , h e a d e r s [ ' c o d e ' ] ) s h a k e=" H T T P / 1 . 11 0 1W e bS o c k e tP r o t o c o lH a n d s h a k e \ r \ n " s h a k e+ =" U p g r a d e :W e b S o c k e t \ r \ n " s h a k e+ =" C o n n e c t i o n :U p g r a d e \ r \ n " s h a k e+ =" S e c W e b S o c k e t O r i g i n :% s \ r \ n "%( h e a d e r s [ ' O r i g i n ' ] ) s h a k e+ =" S e c W e b S o c k e t L o c a t i o n :w s : / / % s / s t u f f \ r \ n "%( h e a d e r s [ ' H o s t ' ] ) s h a k e+ =" S e c W e b S o c k e t P r o t o c o l :s a m p l e \ r \ n \ r \ n " s h a k e+ =d i g e s t r e t u r nc l i e n t . s e n d ( s h a k e ) d e fh a n d l e( c l i e n t ,a d d r ) : h a n d s h a k e ( c l i e n t ) l o c k=t h r e a d i n g . L o c k ( ) w h i l e1 : d a t a=r e c v _ d a t a ( c l i e n t ,1 0 2 4 ) i fn o td a t a :b r e a k d a t a=c g i . e s c a p e ( d a t a ) l o c k . a c q u i r e ( ) [ s e n d _ d a t a ( c ,d a t a )f o rci nc l i e n t s ]

Python

https://gist.github.com/geoffb/616117

2/3

11/3/2014
6 9 7 0 7 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8 7 9 8 0 8 1 8 2 8 3 8 4 8 5 8 6 8 7 8 8 8 9

Super simple websockets client/server using Python. Compatible with the draft 76 challenge/response.
[ s e n d _ d a t a ( c ,d a t a )f o rci nc l i e n t s ] l o c k . r e l e a s e ( ) p r i n t' C l i e n tc l o s e d : ' ,a d d r l o c k . a c q u i r e ( ) c l i e n t s . r e m o v e ( c l i e n t ) l o c k . r e l e a s e ( ) c l i e n t . c l o s e ( ) d e fs t a r t _ s e r v e r( ) : s=s o c k e t . s o c k e t ( ) s . s e t s o c k o p t ( s o c k e t . S O L _ S O C K E T ,s o c k e t . S O _ R E U S E A D D R ,1 ) s . b i n d ( ( ' ' ,9 8 7 6 ) ) s . l i s t e n ( 5 ) w h i l e1 : c o n n ,a d d r=s . a c c e p t ( ) p r i n t' C o n n e c t i o nf r o m : ' ,a d d r c l i e n t s . a p p e n d ( c o n n ) t h r e a d i n g . T h r e a d ( t a r g e t=h a n d l e ,a r g s=( c o n n ,a d d r ) ) . s t a r t ( ) c l i e n t s=[ ] s t a r t _ s e r v e r ( )

ccheaton (/ccheaton) commented (/geoffb/616117/#comment-934220)

5 months ago (/geoffb/616117/#comment-934220)

This is helpful for me because I'm trying to learn websockets. A few comments at the top of each file also would help (ports, etc.).

brandonhsiao (/brandonhsiao) commented (/geoffb/616117/#comment-987345)

2 months ago (/geoffb/616117/#comment-987345)

What does that lock in handle(client, addr) do? Isn't the point of a lock to synchronize several threads? Is that a bug for each thread to have its own lock?

dawsonlp (/dawsonlp) commented (/geoffb/616117/#comment-997303)

a month ago (/geoffb/616117/#comment-997303)

Running Ubuntu/Chromium/31.0.1650.63, the headers that come from the browser when I load the webpage don't include Sec-WebSocket-Key1 and Sec-WebSocket-Key2, they only have Sec-WebSocket-Key, so the python code throws a KeyError exception in handshake(client) when it tries to build a digest. Is this because of a recent change in Chrome? Has the websocket protocol been updated since this code was published?

dawsonlp (/dawsonlp) commented (/geoffb/616117/#comment-997509)

a month ago (/geoffb/616117/#comment-997509)

Explanation - the draft 76 challenge/response that this implements has been replaced with RFC-6455. Modern browsers don't work with this code as is.

https://gist.github.com/geoffb/616117

3/3

Vous aimerez peut-être aussi