aboutsummaryrefslogtreecommitdiffstats
path: root/mushi.py
blob: 1f8a659705b14b576318d1eb80a70727cf5b076d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3

import argparse
import socket
import code
import readline
import signal
import sys

def SigHandler_SIGINT(signum, frame):
    print()
    sys.exit(0)

def SigHandler_Timeout(signum, frame):
    print("mushi timed out.")
    raise Exception("time out")

class Argparser(object):
    def __init__(self):
        self.parser = argparse.ArgumentParser()
        self.parser.add_argument("--bool", action="store_true", help="bool", default=False)
        self.parser.add_argument("--dbg", action="store_true", help="debug", default=False)

    def parse(self, args):
        self.args, self.rest = self.parser.parse_known_args(args)

# write code here
def premain(argparser):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ("", 10111)
    sock.connect(server_address)
    msg = " ".join(argparser.rest)
    sock.sendall(bytes(msg, "utf-8"))
    output = sock.recv(4096)
    out2 = [c for c in output.decode("utf-8") if c != "\0"]
    print("".join(out2))
    sock.close()

def main(argv):
    argparser = Argparser()
    argparser.parse(argv)
    if argparser.args.dbg:
        try:
            premain(argparser)
        except:
            variables = globals().copy()
            variables.update(locals())
            shell = code.InteractiveConsole(variables)
            shell.interact(banner="DEBUG REPL")
    else:
        premain(argparser)

if __name__ == "__main__":
    main(sys.argv[1:])