【问题标题】:What is the actual return value type of Python Paramiko Channel.recv()?Python Paramiko Channel.recv() 的实际返回值类型是什么?
【发布时间】:2021-02-03 13:19:02
【问题描述】:

我有一个 2.7 版的旧 Python 脚本,它使用函数 Paramiko Channel.recv() 方法。我正在尝试将脚本迁移到 Python 版本 3(和 Paramiko 2.7.1),但文档说:

:return: 接收到的数据,作为str/bytes

由于从 Python 2 迁移到 Python 3 时的主要问题是不同的基本字符串,因此我需要更具体的返回类型规范。

函数的返回类型取决于什么?或者:我如何确定返回值总是相同的(例如字节串)?

一些代码-sn-p:

t = paramiko.Transport(socklike_obj)
t.start_client()
t.auth_password(usr, pw)
chan = t.open_session()
nbytes = 2**16-1  # 65535
out = chan.recv(nbytes)
while out != b'':  # or: out != ''
    if b'\n' in out or b'\r' in out:
        parts = out.splitlines()

【问题讨论】:

    标签: python python-3.x ssh python-2.x paramiko


    【解决方案1】:

    在 Python 3 中,该方法始终返回 bytes

    在 Python 2 中,该方法返回一个字符串。

    【讨论】:

    • 或者:它只返回 bytes(因为在 2.x 中,bytesstr 是同一回事)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2013-09-09
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    相关资源
    最近更新 更多