10 from six
.moves
.urllib
.parse
import urlparse
21 encoding
= getattr(sys
.stdin
, "encoding", "")
25 return encoding
.lower()
28 OPCODE_DATA
= (websocket
.ABNF
.OPCODE_TEXT
, websocket
.ABNF
.OPCODE_BINARY
)
29 ENCODING
= get_encoding()
32 class VAction(argparse
.Action
):
34 def __call__(self
, parser
, args
, values
, option_string
=None):
40 values
= values
.count("v") + 1
41 setattr(args
, self
.dest
, values
)
45 parser
= argparse
.ArgumentParser(description
="WebSocket Simple Dump Tool")
46 parser
.add_argument("url", metavar
="ws_url",
47 help="websocket url. ex. ws://echo.websocket.org/")
48 parser
.add_argument("-p", "--proxy",
49 help="proxy url. ex. http://127.0.0.1:8080")
50 parser
.add_argument("-v", "--verbose", default
=0, nargs
='?', action
=VAction
,
52 help="set verbose mode. If set to 1, show opcode. "
53 "If set to 2, enable to trace websocket module")
54 parser
.add_argument("-n", "--nocert", action
='store_true',
55 help="Ignore invalid SSL cert")
56 parser
.add_argument("-r", "--raw", action
="store_true",
58 parser
.add_argument("-s", "--subprotocols", nargs
='*',
59 help="Set subprotocols")
60 parser
.add_argument("-o", "--origin",
62 parser
.add_argument("--eof-wait", default
=0, type=int,
63 help="wait time(second) after 'EOF' received.")
64 parser
.add_argument("-t", "--text",
65 help="Send initial text")
66 parser
.add_argument("--timings", action
="store_true",
67 help="Print timings in seconds")
68 parser
.add_argument("--headers",
69 help="Set custom headers. Use ',' as separator")
71 return parser
.parse_args()
76 def raw_input(self
, prompt
):
80 line
= raw_input(prompt
)
82 if ENCODING
and ENCODING
!= "utf-8" and not isinstance(line
, six
.text_type
):
83 line
= line
.decode(ENCODING
).encode("utf-8")
84 elif isinstance(line
, six
.text_type
):
85 line
= line
.encode("utf-8")
90 class InteractiveConsole(RawInput
, code
.InteractiveConsole
):
92 def write(self
, data
):
93 sys
.stdout
.write("\033[2K\033[E")
94 # sys.stdout.write("\n")
95 sys
.stdout
.write("\033[34m< " + data
+ "\033[39m")
96 sys
.stdout
.write("\n> ")
100 return self
.raw_input("> ")
103 class NonInteractive(RawInput
):
105 def write(self
, data
):
106 sys
.stdout
.write(data
)
107 sys
.stdout
.write("\n")
111 return self
.raw_input("")
115 start_time
= time
.time()
118 websocket
.enableTrace(True)
121 p
= urlparse(args
.proxy
)
122 options
["http_proxy_host"] = p
.hostname
123 options
["http_proxy_port"] = p
.port
125 options
["origin"] = args
.origin
126 if args
.subprotocols
:
127 options
["subprotocols"] = args
.subprotocols
130 opts
= {"cert_reqs": websocket
.ssl
.CERT_NONE
, "check_hostname": False}
132 options
['header'] = map(str.strip
, args
.headers
.split(','))
133 ws
= websocket
.create_connection(args
.url
, sslopt
=opts
, **options
)
135 console
= NonInteractive()
137 console
= InteractiveConsole()
138 print("Press Ctrl+C to quit")
142 frame
= ws
.recv_frame()
143 except websocket
.WebSocketException
:
144 return websocket
.ABNF
.OPCODE_CLOSE
, None
146 raise websocket
.WebSocketException("Not a valid frame %s" % frame
)
147 elif frame
.opcode
in OPCODE_DATA
:
148 return frame
.opcode
, frame
.data
149 elif frame
.opcode
== websocket
.ABNF
.OPCODE_CLOSE
:
151 return frame
.opcode
, None
152 elif frame
.opcode
== websocket
.ABNF
.OPCODE_PING
:
154 return frame
.opcode
, frame
.data
156 return frame
.opcode
, frame
.data
160 opcode
, data
= recv()
162 if six
.PY3
and opcode
== websocket
.ABNF
.OPCODE_TEXT
and isinstance(data
, bytes
):
163 data
= str(data
, "utf-8")
164 if not args
.verbose
and opcode
in OPCODE_DATA
:
167 msg
= "%s: %s" % (websocket
.ABNF
.OPCODE_MAP
.get(opcode
), data
)
171 console
.write(str(time
.time() - start_time
) + ": " + msg
)
175 if opcode
== websocket
.ABNF
.OPCODE_CLOSE
:
178 thread
= threading
.Thread(target
=recv_ws
)
187 message
= console
.read()
189 except KeyboardInterrupt:
192 time
.sleep(args
.eof_wait
)
196 if __name__
== "__main__":
199 except Exception as e
: