diff options
| author | Jose Vasconcellos <jose@metologica.com> | 2015-02-25 10:18:30 -0500 |
|---|---|---|
| committer | Jose Vasconcellos <jose@metologica.com> | 2015-02-25 10:18:30 -0500 |
| commit | f77498309d4ca3ef47495de980599c4e9649e596 (patch) | |
| tree | 8d3c0d368437e55edee6f6151de62326b27926fa /bin | |
| parent | 10bbe19c1a4c493fbfcce3f02f850a954310edf2 (diff) | |
| download | websocket-client-f77498309d4ca3ef47495de980599c4e9649e596.tar.gz | |
Changes to wsdump.py. Add command line options and handle closed socket.
Additional comamnd line options:
* -s proto1 proto2 - set one or more subprotocols
* -o origin - set origin
* -t "initial text" - allow some text be set from command line
Handle receiving from a closed socket.
Reply to ping with a pong containing the ping packet data.
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/wsdump.py | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/bin/wsdump.py b/bin/wsdump.py index 51269a7..3f1d56e 100755 --- a/bin/wsdump.py +++ b/bin/wsdump.py @@ -35,6 +35,12 @@ def parse_args(): "If set to 2, enable to trace websocket module") parser.add_argument("-n", "--nocert", action='store_true', help="Ignore invalid SSL cert") + parser.add_argument("-s", "--subprotocols", nargs='*', + help="Set subprotocols") + parser.add_argument("-o", "--origin", + help="Set origin") + parser.add_argument("-t", "--text", + help="Send initial text") return parser.parse_args() @@ -67,13 +73,20 @@ def main(): if args.verbose > 1: websocket.enableTrace(True) opts = {} + if (args.origin): + opts = { "origin": args.origin } + if (args.subprotocols): + opts = { "subprotocols": args.subprotocols } if (args.nocert): opts = { "cert_reqs": websocket.ssl.CERT_NONE, "check_hostname": False } ws = websocket.create_connection(args.url, sslopt=opts) print("Press Ctrl+C to quit") def recv(): - frame = ws.recv_frame() + try: + frame = ws.recv_frame() + except websocket.WebSocketException: + return (websocket.ABNF.OPCODE_CLOSE, None) if not frame: raise websocket.WebSocketException("Not a valid frame %s" % frame) elif frame.opcode in OPCODE_DATA: @@ -82,7 +95,7 @@ def main(): ws.send_close() return (frame.opcode, None) elif frame.opcode == websocket.ABNF.OPCODE_PING: - ws.pong("Hi!") + ws.pong(frame.data) return frame.opcode, frame.data return frame.opcode, frame.data @@ -100,10 +113,16 @@ def main(): if msg: console.write(msg) + if opcode == websocket.ABNF.OPCODE_CLOSE: + break + thread = threading.Thread(target=recv_ws) thread.daemon = True thread.start() + if args.text: + ws.send(args.text) + while True: try: message = console.raw_input("> ") |
