1
- '''
1
+ """
2
2
The client reads a line from stdin, sends it to the server, then prints the
3
3
response. This is a poor implementation of a client. It is only intended to
4
4
demonstrate how to use wsproto.
5
- '''
5
+ """
6
6
7
7
import socket
8
8
import sys
9
9
10
10
from wsproto import WSConnection
11
11
from 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
+ )
14
21
15
22
RECEIVE_BYTES = 4096
16
23
17
24
18
25
def main ():
19
- ''' Run the client. '''
26
+ """ Run the client."""
20
27
try :
21
28
host = sys .argv [1 ]
22
29
port = int (sys .argv [2 ])
23
30
except (IndexError , ValueError ):
24
- print (' Usage: {} <HOST> <PORT>' .format (sys .argv [0 ]))
31
+ print (" Usage: {} <HOST> <PORT>" .format (sys .argv [0 ]))
25
32
sys .exit (1 )
26
33
27
34
try :
28
35
wsproto_demo (host , port )
29
36
except KeyboardInterrupt :
30
- print (' \n Received SIGINT: shutting down…' )
37
+ print (" \n Received SIGINT: shutting down…" )
31
38
32
39
33
40
def wsproto_demo (host , port ):
34
- '''
41
+ """
35
42
Demonstrate wsproto:
36
43
37
44
0) Open TCP connection
@@ -41,39 +48,39 @@ def wsproto_demo(host, port):
41
48
4) Negotiate WebSocket closing handshake
42
49
43
50
:param stream: a socket stream
44
- '''
51
+ """
45
52
46
53
# 0) Open TCP connection
47
- print (' Connecting to {}:{}' .format (host , port ))
54
+ print (" Connecting to {}:{}" .format (host , port ))
48
55
conn = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
49
56
conn .connect ((host , port ))
50
57
51
58
# 1) Negotiate WebSocket opening handshake
52
- print (' Opening WebSocket' )
59
+ print (" Opening WebSocket" )
53
60
ws = WSConnection (ConnectionType .CLIENT )
54
61
# Because this is a client WebSocket, we need to initiate the connection
55
62
# 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 )
57
64
net_recv (ws , conn )
58
65
handle_events (ws )
59
66
60
67
# 2) Send a message and display response
61
68
message = "wsproto is great"
62
- print (' Sending message: {}' .format (message ))
69
+ print (" Sending message: {}" .format (message ))
63
70
net_send (ws .send (Message (data = message )), conn )
64
71
net_recv (ws , conn )
65
72
handle_events (ws )
66
73
67
74
# 3) Send ping and display pong
68
75
payload = b"table tennis"
69
- print (' Sending ping: {}' .format (payload ))
76
+ print (" Sending ping: {}" .format (payload ))
70
77
net_send (ws .send (Ping (payload = payload )), conn )
71
78
net_recv (ws , conn )
72
79
handle_events (ws )
73
80
74
81
# 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 )
77
84
# After sending the closing frame, we won't get any more events. The server
78
85
# should send a reply and then close the connection, so we need to receive
79
86
# twice:
@@ -83,35 +90,35 @@ def wsproto_demo(host, port):
83
90
84
91
85
92
def 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 )))
88
95
conn .send (out_data )
89
96
90
97
91
98
def net_recv (ws , conn ):
92
- ''' Read pending data from network into websocket. '''
99
+ """ Read pending data from network into websocket. """
93
100
in_data = conn .recv (RECEIVE_BYTES )
94
101
if not in_data :
95
102
# A receive of zero bytes indicates the TCP socket has been closed. We
96
103
# 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)" )
98
105
ws .receive_data (None )
99
106
else :
100
- print (' Received {} bytes' .format (len (in_data )))
107
+ print (" Received {} bytes" .format (len (in_data )))
101
108
ws .receive_data (in_data )
102
109
103
110
104
111
def handle_events (ws ):
105
112
for event in ws .events ():
106
113
if isinstance (event , AcceptConnection ):
107
- print (' WebSocket negotiation complete' )
114
+ print (" WebSocket negotiation complete" )
108
115
elif isinstance (event , TextMessage ):
109
- print (' Received message: {}' .format (event .data ))
116
+ print (" Received message: {}" .format (event .data ))
110
117
elif isinstance (event , Pong ):
111
- print (' Received pong: {}' .format (event .payload ))
118
+ print (" Received pong: {}" .format (event .payload ))
112
119
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 ))
114
121
115
122
116
- if __name__ == ' __main__' :
123
+ if __name__ == " __main__" :
117
124
main ()
0 commit comments