How does paramiko Channel.recv() exactly work?

前端 未结 1 561
执念已碎
执念已碎 2021-01-05 09:40

I\'m having a hard time understanding how the recv() function works.

http://docs.paramiko.org/en/1.13/api/channel.html#paramiko.channel.Channel.recv

相关标签:
1条回答
  • 2021-01-05 10:39

    Channel recv() corresponds to a socket.recv(), it does not have any specific structure or size, it just reads whatever data was sent from the remote server, not exceeding maxBytes.

    You commonly use recv() in a loop until you get a piece of data that you are waiting for:

    def _wait_for_data(self, options, verbose=False):
        chan = self.chan
        data = ""
        while True:
            x = chan.recv(1024)
            if len(x) == 0:
                self.log("*** Connection terminated\r")
                sys.exit(3)
            data += x
            if verbose:
                sys.stdout.write(x)
                sys.stdout.flush()
            for i in range(len(options)):
                if re.search(options[i], data):
                    return i
        return -1
    
    0 讨论(0)
提交回复
热议问题