def onTermCommand(bot, command): command = BYTES2STR(command) if command.startswith('GET /'): http = True end = command.find('\r\n') if end == -1 or not command[:end - 3].endswith(' HTTP/'): argv = [] else: url = command[5:end - 9].rstrip('/') if url == 'favicon.ico': return b'' argv = [Unquote(x) for x in url.split('/')] else: http = False argv = command.strip().split(None, 3) if argv and argv[0] in cmdFuncs: try: result, err = cmdFuncs[argv[0]](bot, argv[1:], http) except Exception as e: result, err = None, '运行命令过程中出错:' + str(type(e)) + str(e) ERROR(err, exc_info=True) else: result, err = None, 'QQBot 命令格式错误' if http: rep = {'result': result, 'err': err} rep = STR2BYTES(JsonDumps(rep, ensure_ascii=False, indent=4)) rep = (b'HTTP/1.1 200 OK\r\n' + b'Connection: close\r\n' + b'Content-Length: ' + STR2BYTES(str(len(rep))) + b'\r\n' + b'Content-Type: text/plain;charset=utf-8\r\n\r\n' + rep) else: rep = STR2BYTES(str(err or result)) + b'\r\n' return rep
def execute(bot, command): if command.startswith('GET /'): end = command.find('\r\n') if end == -1 or not command[:end - 2].endswith(' HTTP/1'): return argv = [Unquote(x) for x in command[5:end - 9].split('/')] else: argv = command.strip().split(None, 4) if argv and argv[0] in cmdFuncs: return cmdFuncs[argv[0]](bot, argv[1:])