Paramiko Transport settimeout

时间:2014-07-02 07:54:21

标签: python paramiko

我使用Paramiko进行端口转发,主要情况正常,但是当我测试我的错误情况时。我这样做的时间很长:

port = transport.request_port_forward(my_ip],0)
channel = transport.open_channel('direct-tcpip',(my_ip',22),
                                    ('127.0.0.1',port))

问题是transport.open_channel:设备(my_ip)无法访问,因此paramiko无法连接...我想为此操作设置超时但目前我知道如何仅为:< / p>

Client.connect()
Channel.settiemout()

但Client.connect仅适用于连接,我在transport.open_channel()之后获得了我的频道......

我在paramiko的文档(运输)中找到了这个:

  

初始化(袜子)

Create a new SSH session over an existing socket, or socket-like object. This only creates the Transport object; it doesn’t begin the SSH session yet. Use connect or start_client to begin a client session, or start_server to begin a server session.

If the object is not actually a socket, it must have the following methods:

    send(str): Writes from 1 to len(str) bytes, and returns an int representing the number of bytes written. Returns 0 or raises EOFError if the stream has been closed.
    recv(int): Reads from 1 to int bytes and returns them as a string. Returns 0 or raises EOFError if the stream has been closed.
    close(): Closes the socket.
    settimeout(n): Sets a (float) timeout on I/O operations.

但我不知道如何称呼这个&#34; settimeout(float)&#34;或设置它。我是否必须使用这4种方法创建自己的对象?

提前谢谢你,BR。

1 个答案:

答案 0 :(得分:0)

我找到了如何修复它...我不知道它是否是最好的方式但它有效......我在transport.py中添加了这个功能,它现在工作正常:

    def open_channel_with_timeout(self, kind,timeout,
                                dest_addr=None, src_addr=None):
    """
    Request a new channel to the server. `Channels <.Channel>` are
    socket-like objects used for the actual transfer of data across the
    session. You may only request a channel after negotiating encryption
    (using `connect` or `start_client`) and authenticating.

    :param str kind:
        the kind of channel requested (usually ``"session"``,
        ``"forwarded-tcpip"``, ``"direct-tcpip"``, or ``"x11"``)
    :param tuple dest_addr:
        the destination address (address + port tuple) of this port
        forwarding, if ``kind`` is ``"forwarded-tcpip"`` or
        ``"direct-tcpip"`` (ignored for other channel types)
    :param src_addr: the source address of this port forwarding, if
        ``kind`` is ``"forwarded-tcpip"``, ``"direct-tcpip"``, or ``"x11"``
    :param float timeout: the value of timeout before raising exception
    :return: a new `.Channel` on success

    :raises SSHException: if the request is rejected or the session ends
        prematurely
    """
    if not self.active:
        raise SSHException('SSH session not active')
    self.lock.acquire()
    try:
        chanid = self._next_channel()
        m = Message()
        m.add_byte(cMSG_CHANNEL_OPEN)
        m.add_string(kind)
        m.add_int(chanid)
        m.add_int(self.window_size)
        m.add_int(self.max_packet_size)
        if (kind == 'forwarded-tcpip') or (kind == 'direct-tcpip'):
            m.add_string(dest_addr[0])
            m.add_int(dest_addr[1])
            m.add_string(src_addr[0])
            m.add_int(src_addr[1])
        elif kind == 'x11':
            m.add_string(src_addr[0])
            m.add_int(src_addr[1])
        chan = Channel(chanid)
        self._channels.put(chanid, chan)
        self.channel_events[chanid] = event = threading.Event()
        self.channels_seen[chanid] = True
        chan._set_transport(self)
        chan._set_window(self.window_size, self.max_packet_size)
    finally:
        self.lock.release()
    self._send_user_message(m)
    egg_timer = 0.0
    while True:
        event.wait(0.1)
        egg_timer += 0.1
        if egg_timer > timeout:
            e = socket.error('Unable to open channel (Timeout).')
            raise e
        if not self.active:
            e = self.get_exception()
            if e is None:
                e = SSHException('Unable to open channel.')
            raise e
        if event.isSet():
            break
    chan = self._channels.get(chanid)
    if chan is not None:
        return chan
    e = self.get_exception()
    if e is None:
        e = SSHException('Unable to open channel.')
    raise e