避免在套接字读取输出中输入 - Python

时间:2016-10-14 21:53:54

标签: python bash sockets stdout stdin

我有以下recv_size方法:

def recv_size(self, socket, n=8192, timeout=0.25):
    """

    :param socket:
    :param n:
    :param timeout:
    :return:
    """
    socket.setblocking(0)
    data = ''
    begin = time.time()
    while True:
        # if you got some data, then break after wait sec
        if data and time.time() - begin > timeout:
            break
        # if you got no data at all, wait a little longer
        elif time.time() - begin > timeout*2:
            break
        try:
            data = socket.recv(n)
            if data:
                sys.stdout.write(data)
                sys.stdout.flush()
                begin = time.time()
            else:
                time.sleep(0.1)
        except:
            pass

我通过socket <}向sendall()发送信息:

exec_id = client.exec_start(command)
while True:
    recv_size(socket)
    container_info = client.inspect_container(container)
    if exec_id['Id'] in container_info['ExecIDs']:
        input_value = sys.stdin.readline()
        socket.sendall(input_value)
    else:
        break

因此,当我输入命令行/bin/bash时,我就能够开始向远程终端发送信息。但是,我总是在返回消息中获得input_value

root@so1:# echo "hello"
echo "hello"
hello

我该如何避免这种情况?

0 个答案:

没有答案