@@ -45,6 +45,8 @@ 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("-r", "--raw", action="store_true",
|
||||||
|
help="raw output")
|
||||||
parser.add_argument("-s", "--subprotocols", nargs='*',
|
parser.add_argument("-s", "--subprotocols", nargs='*',
|
||||||
help="Set subprotocols")
|
help="Set subprotocols")
|
||||||
parser.add_argument("-o", "--origin",
|
parser.add_argument("-o", "--origin",
|
||||||
@@ -56,15 +58,7 @@ def parse_args():
|
|||||||
|
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
class RawInput():
|
||||||
class InteractiveConsole(code.InteractiveConsole):
|
|
||||||
def write(self, data):
|
|
||||||
sys.stdout.write("\033[2K\033[E")
|
|
||||||
# sys.stdout.write("\n")
|
|
||||||
sys.stdout.write("\033[34m" + data + "\033[39m")
|
|
||||||
sys.stdout.write("\n> ")
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
def raw_input(self, prompt):
|
def raw_input(self, prompt):
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
line = input(prompt)
|
line = input(prompt)
|
||||||
@@ -78,10 +72,28 @@ class InteractiveConsole(code.InteractiveConsole):
|
|||||||
|
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
class InteractiveConsole(RawInput, code.InteractiveConsole):
|
||||||
|
def write(self, data):
|
||||||
|
sys.stdout.write("\033[2K\033[E")
|
||||||
|
# sys.stdout.write("\n")
|
||||||
|
sys.stdout.write("\033[34m< " + data + "\033[39m")
|
||||||
|
sys.stdout.write("\n> ")
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
return self.raw_input("> ")
|
||||||
|
|
||||||
|
class NonInteractive(RawInput):
|
||||||
|
def write(self, data):
|
||||||
|
sys.stdout.write(data)
|
||||||
|
sys.stdout.write("\n")
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
return self.raw_input("")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
console = InteractiveConsole()
|
|
||||||
if args.verbose > 1:
|
if args.verbose > 1:
|
||||||
websocket.enableTrace(True)
|
websocket.enableTrace(True)
|
||||||
options = {}
|
options = {}
|
||||||
@@ -93,7 +105,11 @@ def main():
|
|||||||
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, **options)
|
ws = websocket.create_connection(args.url, sslopt=opts, **options)
|
||||||
print("Press Ctrl+C to quit")
|
if args.raw:
|
||||||
|
console = NonInteractive()
|
||||||
|
else:
|
||||||
|
console = InteractiveConsole()
|
||||||
|
print("Press Ctrl+C to quit")
|
||||||
|
|
||||||
def recv():
|
def recv():
|
||||||
try:
|
try:
|
||||||
@@ -121,11 +137,11 @@ def main():
|
|||||||
if six.PY3 and opcode == websocket.ABNF.OPCODE_TEXT and isinstance(data, bytes):
|
if six.PY3 and opcode == websocket.ABNF.OPCODE_TEXT and isinstance(data, bytes):
|
||||||
data = str(data, "utf-8")
|
data = str(data, "utf-8")
|
||||||
if not args.verbose and opcode in OPCODE_DATA:
|
if not args.verbose and opcode in OPCODE_DATA:
|
||||||
msg = "< %s" % data
|
msg = data
|
||||||
elif args.verbose:
|
elif args.verbose:
|
||||||
msg = "< %s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data)
|
msg = "%s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data)
|
||||||
|
|
||||||
if msg:
|
if msg is not None:
|
||||||
console.write(msg)
|
console.write(msg)
|
||||||
|
|
||||||
if opcode == websocket.ABNF.OPCODE_CLOSE:
|
if opcode == websocket.ABNF.OPCODE_CLOSE:
|
||||||
@@ -140,7 +156,7 @@ def main():
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
message = console.raw_input("> ")
|
message = console.read()
|
||||||
ws.send(message)
|
ws.send(message)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user