How does paramiko Channel.recv() exactly work?

女生的网名这么多〃 提交于 2019-11-30 03:59:19

问题


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

I understand this is receiving a chunk a data each time you call the function, but can someone elaborate on the structure or size of this data? Lets say I send a command date, I notice:

  • 1st read gets: "date"
  • 2nd read gets: actual response (Mon Jun 9 12:04:17 CDT 2014)
  • 3rd read gets: prompt

But how does this handle debugging messages that appear randomly on the terminal?

Does the previous pattern hold true as long as the actual response is less than maximum bytes (nbytes)?

What happens if it exceeds nbytes?

As per request, I've added a snippet of the code below:

while reads<maxReads:
   resp = self.__chan.recv(maxBytes)
   print resp
   self.__buffer += resp
   if resp.endswith('$ ') or resp.endswith('# '):
      break
   reads += 1

回答1:


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


来源:https://stackoverflow.com/questions/24125182/how-does-paramiko-channel-recv-exactly-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!