paramiko Channel.recv() 究竟是如何工作的? [英] How does paramiko Channel.recv() exactly work?

查看:129
本文介绍了paramiko Channel.recv() 究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解 recv() 函数的工作原理.

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

我知道每次调用该函数时都会收到一个数据块,但是有人可以详细说明此数据的结构或大小吗?假设我发送一个命令 date,我注意到:

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:

  • 第一次阅读得到:日期"
  • 第二次阅读:实际回复(2014 年 6 月 9 日星期一 12:04:17 CDT)
  • 第三次读取:提示

但这如何处理随机出现在终端上的调试消息?

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

只要实际响应小于最大字节数 (nbytes),前面的模式是否成立?

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

如果超过 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

推荐答案

Channel recv() 对应一个 socket.recv(),它没有任何特定的结构和大小,它只是读取远程发送的任何数据服务器,不超过 maxBytes.

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.

您通常在循环中使用 recv() 直到获得您正在等待的数据:

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

这篇关于paramiko Channel.recv() 究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆