def QTerm(): # python qterm.py [PORT] [COMMAND] if len(sys.argv) >= 2 and sys.argv[1].isdigit(): port = sys.argv[1] command = ' '.join(sys.argv[2:]).strip() else: port = DEFPORT command = ' '.join(sys.argv[1:]).strip() if command: resp = BYTES2STR(Query(HOST, port, SYSTEMSTR2BYTES(command))) if not resp: PRINT('无法连接 QQBot-Term 服务器') elif not resp.strip(): PRINT('QQBot 命令格式错误') else: PRINT(resp.strip())
def Query(host, port, req): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) resp = b'' try: sock.connect((host, int(port))) sock.sendall(req) while True: data = sock.recv(8096) if not data: return resp else: resp += data except socket.error: return resp finally: sock.close() if __name__ == '__main__': import sys from qqbot.common import SYSTEMSTR2BYTES data = ' '.join(sys.argv[1:]).strip() if data: host, port = '127.0.0.1', 8191 if data == '-s': MySocketServer(host, port).Run() else: print(Query(host, port, SYSTEMSTR2BYTES(data)))