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.
This commit is contained in:
Jose Vasconcellos
2015-02-25 10:18:30 -05:00
parent 10bbe19c1a
commit f77498309d

View File

@@ -35,6 +35,12 @@ def parse_args():
"If set to 2, enable to trace websocket module") "If set to 2, enable to trace websocket module")
parser.add_argument("-n", "--nocert", action='store_true', parser.add_argument("-n", "--nocert", action='store_true',
help="Ignore invalid SSL cert") 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() return parser.parse_args()
@@ -67,13 +73,20 @@ def main():
if args.verbose > 1: if args.verbose > 1:
websocket.enableTrace(True) websocket.enableTrace(True)
opts = {} opts = {}
if (args.origin):
opts = { "origin": args.origin }
if (args.subprotocols):
opts = { "subprotocols": args.subprotocols }
if (args.nocert): if (args.nocert):
opts = { "cert_reqs": websocket.ssl.CERT_NONE, "check_hostname": False } opts = { "cert_reqs": websocket.ssl.CERT_NONE, "check_hostname": False }
ws = websocket.create_connection(args.url, sslopt=opts) ws = websocket.create_connection(args.url, sslopt=opts)
print("Press Ctrl+C to quit") print("Press Ctrl+C to quit")
def recv(): def recv():
try:
frame = ws.recv_frame() frame = ws.recv_frame()
except websocket.WebSocketException:
return (websocket.ABNF.OPCODE_CLOSE, None)
if not frame: if not frame:
raise websocket.WebSocketException("Not a valid frame %s" % frame) raise websocket.WebSocketException("Not a valid frame %s" % frame)
elif frame.opcode in OPCODE_DATA: elif frame.opcode in OPCODE_DATA:
@@ -82,7 +95,7 @@ def main():
ws.send_close() ws.send_close()
return (frame.opcode, None) return (frame.opcode, None)
elif frame.opcode == websocket.ABNF.OPCODE_PING: elif frame.opcode == websocket.ABNF.OPCODE_PING:
ws.pong("Hi!") ws.pong(frame.data)
return frame.opcode, frame.data return frame.opcode, frame.data
return frame.opcode, frame.data return frame.opcode, frame.data
@@ -100,10 +113,16 @@ def main():
if msg: if msg:
console.write(msg) console.write(msg)
if opcode == websocket.ABNF.OPCODE_CLOSE:
break
thread = threading.Thread(target=recv_ws) thread = threading.Thread(target=recv_ws)
thread.daemon = True thread.daemon = True
thread.start() thread.start()
if args.text:
ws.send(args.text)
while True: while True:
try: try:
message = console.raw_input("> ") message = console.raw_input("> ")