1- '''
1+ """
22The client reads a line from stdin, sends it to the server, then prints the
33response. This is a poor implementation of a client. It is only intended to
44demonstrate how to use wsproto.
5- '''
5+ """
66
77import socket
88import sys
99
1010from wsproto import WSConnection
1111from wsproto .connection import ConnectionType
12- from wsproto .events import AcceptConnection , CloseConnection , Message , Ping , Pong , Request , TextMessage
13-
12+ from wsproto .events import (
13+ AcceptConnection ,
14+ CloseConnection ,
15+ Message ,
16+ Ping ,
17+ Pong ,
18+ Request ,
19+ TextMessage ,
20+ )
1421
1522RECEIVE_BYTES = 4096
1623
1724
1825def main ():
19- ''' Run the client. '''
26+ """ Run the client."""
2027 try :
2128 host = sys .argv [1 ]
2229 port = int (sys .argv [2 ])
2330 except (IndexError , ValueError ):
24- print (' Usage: {} <HOST> <PORT>' .format (sys .argv [0 ]))
31+ print (" Usage: {} <HOST> <PORT>" .format (sys .argv [0 ]))
2532 sys .exit (1 )
2633
2734 try :
2835 wsproto_demo (host , port )
2936 except KeyboardInterrupt :
30- print (' \n Received SIGINT: shutting down…' )
37+ print (" \n Received SIGINT: shutting down…" )
3138
3239
3340def wsproto_demo (host , port ):
34- '''
41+ """
3542 Demonstrate wsproto:
3643
3744 0) Open TCP connection
@@ -41,39 +48,39 @@ def wsproto_demo(host, port):
4148 4) Negotiate WebSocket closing handshake
4249
4350 :param stream: a socket stream
44- '''
51+ """
4552
4653 # 0) Open TCP connection
47- print (' Connecting to {}:{}' .format (host , port ))
54+ print (" Connecting to {}:{}" .format (host , port ))
4855 conn = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
4956 conn .connect ((host , port ))
5057
5158 # 1) Negotiate WebSocket opening handshake
52- print (' Opening WebSocket' )
59+ print (" Opening WebSocket" )
5360 ws = WSConnection (ConnectionType .CLIENT )
5461 # Because this is a client WebSocket, we need to initiate the connection
5562 # handshake by sending a Request event.
56- net_send (ws .send (Request (host = host , target = ' server' )), conn )
63+ net_send (ws .send (Request (host = host , target = " server" )), conn )
5764 net_recv (ws , conn )
5865 handle_events (ws )
5966
6067 # 2) Send a message and display response
6168 message = "wsproto is great"
62- print (' Sending message: {}' .format (message ))
69+ print (" Sending message: {}" .format (message ))
6370 net_send (ws .send (Message (data = message )), conn )
6471 net_recv (ws , conn )
6572 handle_events (ws )
6673
6774 # 3) Send ping and display pong
6875 payload = b"table tennis"
69- print (' Sending ping: {}' .format (payload ))
76+ print (" Sending ping: {}" .format (payload ))
7077 net_send (ws .send (Ping (payload = payload )), conn )
7178 net_recv (ws , conn )
7279 handle_events (ws )
7380
7481 # 4) Negotiate WebSocket closing handshake
75- print (' Closing WebSocket' )
76- net_send (ws .send (CloseConnection (code = 1000 , reason = ' sample reason' )), conn )
82+ print (" Closing WebSocket" )
83+ net_send (ws .send (CloseConnection (code = 1000 , reason = " sample reason" )), conn )
7784 # After sending the closing frame, we won't get any more events. The server
7885 # should send a reply and then close the connection, so we need to receive
7986 # twice:
@@ -83,35 +90,35 @@ def wsproto_demo(host, port):
8390
8491
8592def net_send (out_data , conn ):
86- ''' Write pending data from websocket to network. '''
87- print (' Sending {} bytes' .format (len (out_data )))
93+ """ Write pending data from websocket to network. """
94+ print (" Sending {} bytes" .format (len (out_data )))
8895 conn .send (out_data )
8996
9097
9198def net_recv (ws , conn ):
92- ''' Read pending data from network into websocket. '''
99+ """ Read pending data from network into websocket. """
93100 in_data = conn .recv (RECEIVE_BYTES )
94101 if not in_data :
95102 # A receive of zero bytes indicates the TCP socket has been closed. We
96103 # need to pass None to wsproto to update its internal state.
97- print (' Received 0 bytes (connection closed)' )
104+ print (" Received 0 bytes (connection closed)" )
98105 ws .receive_data (None )
99106 else :
100- print (' Received {} bytes' .format (len (in_data )))
107+ print (" Received {} bytes" .format (len (in_data )))
101108 ws .receive_data (in_data )
102109
103110
104111def handle_events (ws ):
105112 for event in ws .events ():
106113 if isinstance (event , AcceptConnection ):
107- print (' WebSocket negotiation complete' )
114+ print (" WebSocket negotiation complete" )
108115 elif isinstance (event , TextMessage ):
109- print (' Received message: {}' .format (event .data ))
116+ print (" Received message: {}" .format (event .data ))
110117 elif isinstance (event , Pong ):
111- print (' Received pong: {}' .format (event .payload ))
118+ print (" Received pong: {}" .format (event .payload ))
112119 else :
113- raise Exception (' Do not know how to handle event: ' + str (event ))
120+ raise Exception (" Do not know how to handle event: " + str (event ))
114121
115122
116- if __name__ == ' __main__' :
123+ if __name__ == " __main__" :
117124 main ()
0 commit comments