How does paramiko Channel.recv () work?

I find it difficult to understand how the recv() function works.

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

I understand that every time you call this function, it gets a piece of data, but can anyone clarify the structure or size of this data? Say I send a date command, 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 to handle debugging messages that appear randomly on the terminal?

Is the previous template preserved if the actual response is less than the maximum byte ( nbytes )?

What happens if it exceeds nbytes ?

As requested, I added a code snippet below:

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

The recv () channel corresponds to socket.recv (), it does not have any specific structure or size, it simply reads all the data sent from the remote server, not exceeding maxBytes.

Usually you use recv () in a loop until you get the piece of data that you expect:

 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
source

Source: https://habr.com/ru/post/970581/


All Articles