U ¨Ãq`´Àã@sdZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZddlmZddlmZddlmZdd lmZdd lmZmZdd lm Z dd l!m"Z"d d„Z#Gdd„de"ƒZ$Gdd„deƒZ%Gdd„de%ƒZ&Gdd„de%ƒZ'dS)z" Abstraction for an SSH2 channel. éN©Úwraps)Úutil) ÚcMSG_CHANNEL_REQUESTÚcMSG_CHANNEL_WINDOW_ADJUSTÚcMSG_CHANNEL_DATAÚcMSG_CHANNEL_EXTENDED_DATAÚDEBUGÚERRORÚcMSG_CHANNEL_SUCCESSÚcMSG_CHANNEL_FAILUREÚcMSG_CHANNEL_EOFÚcMSG_CHANNEL_CLOSE)ÚMessage)Ú bytes_types)Ú SSHException)Ú BufferedFile)Ú BufferedPipeÚ PipeTimeout)Úpipe)ÚClosingContextManagercstˆƒ‡fdd„ƒ}|S)z¸ Decorator for `.Channel` methods which performs an openness check. :raises: `.SSHException` -- If the wrapped method is called on an unopened `.Channel`. cs0|js|js|js|js tdƒ‚ˆ|f|ž|ŽS)NzChannel is not open)ÚclosedÚ eof_receivedÚeof_sentÚactiver)ÚselfÚargsÚkwds©Úfunc©ú7/tmp/pip-target-nv4zd3e_/lib/python/paramiko/channel.pyÚ_check?sÿþýüzopen_only.._checkr)rr"r rr!Ú open_only6s  r#c@s6eZdZdZdd„Zdd„Zdd„Zed†d d „ƒZedd„ƒZ edd„ƒZ edd„ƒZ ed‡dd„ƒZ edd„ƒZ edd„ƒZdd„Zdd„Zdd„Zedˆd"d#„ƒZed$d%„ƒZd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Zd:d;„Zdd?„Z d@dA„Z!dBdC„Z"dDdE„Z#dFdG„Z$dHdI„Z%dJdK„Z&dLdM„Z'dNdO„Z(dPdQ„Z)dRdS„Z*dTdU„Z+dVdW„Z,dXdY„Z-e.dZd[„ƒZ/d\d]„Z0d^d_„Z1d`da„Z2dbdc„Z3ddde„Z4dfdg„Z5dhdi„Z6djdk„Z7dldm„Z8dndo„Z9dpdq„Z:drds„Z;dtdu„Zdzd{„Z?d|d}„Z@d~d„ZAd€d„ZBd‚dƒ„ZCd„d…„ZDd S)‰ÚChannela A secure tunnel across an SSH `.Transport`. A Channel is meant to behave like a socket, and has an API that should be indistinguishable from the Python socket API. Because SSH2 has a windowing kind of flow control, if you stop reading data from a Channel and its buffer fills up, the server will be unable to send you any more data until you read some of it. (This won't affect other channels on the same transport -- all channels on a single transport are flow-controlled independently.) Similarly, if the server isn't reading data you send, calls to `send` may block, unless you set a timeout. This is exactly like a normal network socket, so it shouldn't be too surprising. Instances of this class may be used as context managers. cCsÎ||_d|_d|_d|_d|_d|_tƒ|_tƒ|_d|_ d|_ d|_ t   ¡|_t  |j¡|_d|_d|_d|_d|_d|_d|_t  ¡|_t|ƒ|_t d¡|_d|_t  ¡|_d|_ d|_!d|_"d|_#dS)am Create a new channel. The channel is not associated with any particular session or `.Transport` until the Transport attaches it. Normally you would only call this method from the constructor of a subclass of `.Channel`. :param int chanid: the ID of this channel, as passed by an existing `.Transport`. rNFzparamiko.transportéÿÿÿÿ)$ÚchanidÚ remote_chanidÚ transportrrrrÚ in_bufferÚin_stderr_bufferÚtimeoutrÚ ultra_debugÚ threadingÚLockÚlockÚ ConditionÚ out_buffer_cvÚin_window_sizeÚout_window_sizeÚin_max_packet_sizeÚout_max_packet_sizeÚin_window_thresholdÚin_window_sofarÚEventÚ status_eventÚstrÚ_namerÚ get_loggerÚloggerÚ_pipeÚeventÚ event_readyÚcombine_stderrÚ exit_statusZ origin_addr)rr&r r r!Ú__init__^s8      zChannel.__init__cCsz | ¡Wn YnXdS©N)Úclose©rr r r!Ú__del__Šs zChannel.__del__cCsŽd |j¡}|jr|d7}nT|jrp|jr0|d7}|jr>|d7}|d |j¡7}t|jƒdkrp|d t|jƒ¡7}|dt |j ƒ7}|d 7}|S) zO Return a string representation of this object, for debugging. z ú>) Úformatr&rrrrr3Úlenr)Úreprr()rÚoutr r r!Ú__repr__s  zChannel.__repr__Úvt100éPércCsŽtƒ}| t¡| |j¡| d¡| d¡| |¡| |¡| |¡| |¡| |¡| tƒ¡| ¡|j   |¡|  ¡dS)ak Request a pseudo-terminal from the server. This is usually used right after creating a client channel, to ask the server to provide some basic terminal semantics for a shell invoked with `invoke_shell`. It isn't necessary (or desirable) to call this method if you're going to execute a single command with `exec_command`. :param str term: the terminal type to emulate (for example, ``'vt100'``) :param int width: width (in characters) of the terminal screen :param int height: height (in characters) of the terminal screen :param int width_pixels: width (in pixels) of the terminal screen :param int height_pixels: height (in pixels) of the terminal screen :raises: `.SSHException` -- if the request was rejected or the channel was closed úpty-reqTN) rÚadd_byterÚadd_intr'Ú add_stringÚ add_booleanÚbytesÚ_event_pendingr(Ú_send_user_messageÚ_wait_for_event)rÚtermÚwidthÚheightÚ width_pixelsÚ height_pixelsÚmr r r!Úget_pty£s           zChannel.get_ptycCsPtƒ}| t¡| |j¡| d¡| d¡| ¡|j  |¡|  ¡dS)a™ Request an interactive shell session on this channel. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the shell. Normally you would call `get_pty` before this, in which case the shell will operate through the pty, and the channel will be connected to the stdin and stdout of the pty. When the shell exits, the channel will be closed and can't be reused. You must open a new channel if you wish to open another shell. :raises: `.SSHException` -- if the request was rejected or the channel was closed ÚshellTN© rrRrrSr'rTrUrWr(rXrY©rr_r r r!Ú invoke_shellÍs     zChannel.invoke_shellcCsZtƒ}| t¡| |j¡| d¡| d¡| |¡| ¡|j  |¡|  ¡dS)a' Execute a command on the server. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the command being executed. When the command finishes executing, the channel will be closed and can't be reused. You must open a new channel if you wish to execute another command. :param str command: a shell command to execute. :raises: `.SSHException` -- if the request was rejected or the channel was closed ÚexecTNrb)rÚcommandr_r r r!Ú exec_commandès      zChannel.exec_commandcCsZtƒ}| t¡| |j¡| d¡| d¡| |¡| ¡|j  |¡|  ¡dS)aÕ Request a subsystem on the server (for example, ``sftp``). If the server allows it, the channel will then be directly connected to the requested subsystem. When the subsystem finishes, the channel will be closed and can't be reused. :param str subsystem: name of the subsystem being requested. :raises: `.SSHException` -- if the request was rejected or the channel was closed Ú subsystemTNrb)rrhr_r r r!Úinvoke_subsystems      zChannel.invoke_subsystemcCshtƒ}| t¡| |j¡| d¡| d¡| |¡| |¡| |¡| |¡|j |¡dS)aP Resize the pseudo-terminal. This can be used to change the width and height of the terminal emulation created in a previous `get_pty` call. :param int width: new width (in characters) of the terminal screen :param int height: new height (in characters) of the terminal screen :param int width_pixels: new width (in pixels) of the terminal screen :param int height_pixels: new height (in pixels) of the terminal screen :raises: `.SSHException` -- if the request was rejected or the channel was closed ú window-changeFN© rrRrrSr'rTrUr(rX)rr[r\r]r^r_r r r!Ú resize_ptys        zChannel.resize_ptyc Cs^| ¡D]P\}}z| ||¡Wqtk rV}zd}t| |¡|ƒ‚W5d}~XYqXqdS)a† Updates this channel's remote shell environment. .. note:: This operation is additive - i.e. the current environment is not reset before the given environment variables are set. .. warning:: Servers may silently reject some environment variables; see the warning in `set_environment_variable` for details. :param dict environment: a dictionary containing the name and respective values to set :raises: `.SSHException` -- if any of the environment variables was rejected by the server or the channel was closed z(Failed to set environment variable "{}".N)ÚitemsÚset_environment_variablerrI)rÚ environmentÚnameÚvalueÚeÚerrr r r!Úupdate_environment7s zChannel.update_environmentcCsTtƒ}| t¡| |j¡| d¡| d¡| |¡| |¡|j |¡dS)a` Set the value of an environment variable. .. warning:: The server may reject this request depending on its ``AcceptEnv`` setting; such rejections will fail silently (which is common client practice for this particular request type). Make sure you understand your server's configuration before using! :param str name: name of the environment variable :param str value: value of the environment variable :raises: `.SSHException` -- if the request was rejected or the channel was closed ÚenvFNrk)rrprqr_r r r!rnQs      z Channel.set_environment_variablecCs|jp|j ¡S)a¹ Return true if the remote process has exited and returned an exit status. You may use this to poll the process status if you don't want to block in `recv_exit_status`. Note that the server may not return an exit status in some cases (like bad servers). :return: ``True`` if `recv_exit_status` will return immediately, else ``False``. .. versionadded:: 1.7.3 )rr9Úis_setrFr r r!Úexit_status_readyls zChannel.exit_status_readycCs|j ¡|j ¡st‚|jS)a3 Return the exit status from the process on the server. This is mostly useful for retrieving the results of an `exec_command`. If the command hasn't finished yet, this method will wait until it does, or until the channel is closed. If no exit status is provided by the server, -1 is returned. .. warning:: In some situations, receiving remote output larger than the current `.Transport` or session's ``window_size`` (e.g. that set by the ``default_window_size`` kwarg for `.Transport.__init__`) will cause `.recv_exit_status` to hang indefinitely if it is called prior to a sufficiently large `.Channel.recv` (or if there are no threads calling `.Channel.recv` in the background). In these cases, ensuring that `.recv_exit_status` is called *after* `.Channel.recv` (or, again, using threads) can avoid the hang. :return: the exit code (as an `int`) of the process on the server. .. versionadded:: 1.2 )r9ÚwaitrvÚAssertionErrorrBrFr r r!Úrecv_exit_status{s zChannel.recv_exit_statuscCsJtƒ}| t¡| |j¡| d¡| d¡| |¡|j |¡dS)aS Send the exit status of an executed command to the client. (This really only makes sense in server mode.) Many clients expect to get some sort of status code back from an executed command after it completes. :param int status: the exit code of the process .. versionadded:: 1.2 ú exit-statusFNrk)rÚstatusr_r r r!Úsend_exit_status–s      zChannel.send_exit_statusNFcCs¨|dkr d}|dkr$t t d¡¡}tƒ}| t¡| |j¡|  d¡|  d¡|  |¡|  |¡|  |¡| |¡|  ¡|j   |¡| ¡|j  |¡|S)aM Request an x11 session on this channel. If the server allows it, further x11 requests can be made from the server to the client, when an x11 application is run in a shell session. From :rfc:`4254`:: It is RECOMMENDED that the 'x11 authentication cookie' that is sent be a fake, random cookie, and that the cookie be checked and replaced by the real cookie when a connection request is received. If you omit the auth_cookie, a new secure random 128-bit value will be generated, used, and returned. You will need to use this value to verify incoming x11 requests and replace them with the actual local x11 cookie (which requires some knowledge of the x11 protocol). If a handler is passed in, the handler is called from another thread whenever a new x11 connection arrives. The default handler queues up incoming x11 connections, which may be retrieved using `.Transport.accept`. The handler's calling signature is:: handler(channel: Channel, (address: str, port: int)) :param int screen_number: the x11 screen number (0, 10, etc.) :param str auth_protocol: the name of the X11 authentication method used; if none is given, ``"MIT-MAGIC-COOKIE-1"`` is used :param str auth_cookie: hexadecimal string containing the x11 auth cookie; if none is given, a secure random 128-bit value is generated :param bool single_connection: if True, only a single x11 connection will be forwarded (by default, any number of x11 connections can arrive over this session) :param handler: an optional callable handler to use for incoming X11 connections :return: the auth_cookie used NzMIT-MAGIC-COOKIE-1éúx11-reqT)ÚbinasciiÚhexlifyÚosÚurandomrrRrrSr'rTrUrWr(rXrYZ_set_x11_handler)rÚ screen_numberZ auth_protocolÚ auth_cookieÚsingle_connectionÚhandlerr_r r r!Ú request_x11«s$/          zChannel.request_x11cCsLtƒ}| t¡| |j¡| d¡| d¡|j |¡|j  |¡dS)a Request for a forward SSH Agent on this channel. This is only valid for an ssh-agent from OpenSSH !!! :param handler: a required callable handler to use for incoming SSH Agent connections :return: True if we are ok, else False (at that time we always return ok) :raises: SSHException in case of channel problem. úauth-agent-req@openssh.comFT) rrRrrSr'rTrUr(rXZ_set_forward_agent_handler)rr‡r_r r r!Úrequest_forward_agentîs      zChannel.request_forward_agentcCs|jS)zG Return the `.Transport` associated with this channel. )r(rFr r r!Ú get_transportszChannel.get_transportcCs ||_dS)zë Set a name for this channel. Currently it's only used to set the name of the channel in logfile entries. The name can be fetched with the `get_name` method. :param str name: new channel name N©r;)rrpr r r!Úset_name szChannel.set_namecCs|jS)zU Get the name of this channel that was previously set by `set_name`. rŒrFr r r!Úget_nameszChannel.get_namecCs|jS)a3 Return the `int` ID # for this channel. The channel ID is unique across a `.Transport` and usually a small number. It's also the number passed to `.ServerInterface.check_channel_request` when determining whether to accept a channel request in server mode. )r&rFr r r!Úget_ids zChannel.get_idcCsZtƒ}|j ¡z"|j}||_|r0|s0|j ¡}W5|j ¡Xt|ƒdkrV| |¡|S)a Set whether stderr should be combined into stdout on this channel. The default is ``False``, but in some cases it may be convenient to have both streams combined. If this is ``False``, and `exec_command` is called (or ``invoke_shell`` with no pty), output to stderr will not show up through the `recv` and `recv_ready` calls. You will have to use `recv_stderr` and `recv_stderr_ready` to get stderr output. If this is ``True``, data will never show up via `recv_stderr` or `recv_stderr_ready`. :param bool combine: ``True`` if stderr output should be combined into stdout on this channel. :return: the previous setting (a `bool`). .. versionadded:: 1.1 r) rVr/ÚacquireÚreleaserAr*ÚemptyrJÚ_feed)rÚcombineÚdataÚoldr r r!Úset_combine_stderr's    zChannel.set_combine_stderrcCs ||_dS)añ Set a timeout on blocking read/write operations. The ``timeout`` argument can be a nonnegative float expressing seconds, or ``None``. If a float is given, subsequent channel read/write operations will raise a timeout exception if the timeout period value has elapsed before the operation has completed. Setting a timeout of ``None`` disables timeouts on socket operations. ``chan.settimeout(0.0)`` is equivalent to ``chan.setblocking(0)``; ``chan.settimeout(None)`` is equivalent to ``chan.setblocking(1)``. :param float timeout: seconds to wait for a pending read/write operation before raising ``socket.timeout``, or ``None`` for no timeout. N©r+)rr+r r r!Ú settimeoutLszChannel.settimeoutcCs|jS)zÑ Returns the timeout in seconds (as a float) associated with socket operations, or ``None`` if no timeout is set. This reflects the last call to `setblocking` or `settimeout`. r˜rFr r r!Ú gettimeout^szChannel.gettimeoutcCs|r| d¡n | d¡dS)aZ Set blocking or non-blocking mode of the channel: if ``blocking`` is 0, the channel is set to non-blocking mode; otherwise it's set to blocking mode. Initially all channels are in blocking mode. In non-blocking mode, if a `recv` call doesn't find any data, or if a `send` call can't immediately dispose of the data, an error exception is raised. In blocking mode, the calls block until they can proceed. An EOF condition is considered "immediate data" for `recv`, so if the channel is closed in the read direction, it will never block. ``chan.setblocking(0)`` is equivalent to ``chan.settimeout(0)``; ``chan.setblocking(1)`` is equivalent to ``chan.settimeout(None)``. :param int blocking: 0 to set non-blocking mode; non-0 to set blocking mode. Nç)r™)rÚblockingr r r!Ú setblockingfs zChannel.setblockingcCs |j ¡S)a Return the address of the remote side of this Channel, if possible. This simply wraps `.Transport.getpeername`, used to provide enough of a socket-like interface to allow asyncore to work. (asyncore likes to call ``'getpeername'``.) )r(Ú getpeernamerFr r r!rž}szChannel.getpeernamecCst|j ¡z:|jdk r&|j ¡d|_|jr2|jr:W¢dS| ¡}W5|j ¡X|D]}|dk rV|j  |¡qVdS)a+ Close the channel. All future read/write operations on the channel will fail. The remote end will receive no more data (after queued data is flushed). Channels are automatically closed when their `.Transport` is closed or when they are garbage collected. N) r/rr‘r>rErrÚ_close_internalr(rX)rÚmsgsr_r r r!rE‡s      z Channel.closecCs |j ¡S)av Returns true if data is buffered and ready to be read from this channel. A ``False`` result does not mean that the channel has closed; it means you may need to wait before more data arrives. :return: ``True`` if a `recv` call on this channel would immediately return at least one byte; ``False`` otherwise. )r)Ú read_readyrFr r r!Ú recv_ready¡s zChannel.recv_readycCs~z|j ||j¡}Wntk r0t ¡‚YnX| t|ƒ¡}|dkrztƒ}| t ¡|  |j ¡|  |¡|j   |¡|S)aû Receive data from the channel. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by ``nbytes``. If a string of length zero is returned, the channel stream has closed. :param int nbytes: maximum number of bytes to read. :return: received data, as a ``str``/``bytes``. :raises socket.timeout: if no data is ready before the timeout set by `settimeout`. r)r)Úreadr+rÚsocketÚ_check_add_windowrJrrRrrSr'r(rX©rÚnbytesrLZackr_r r r!Úrecv­s     z Channel.recvcCs |j ¡S)a« Returns true if data is buffered and ready to be read from this channel's stderr stream. Only channels using `exec_command` or `invoke_shell` without a pty will ever have data on the stderr stream. :return: ``True`` if a `recv_stderr` call on this channel would immediately return at least one byte; ``False`` otherwise. .. versionadded:: 1.1 )r*r¡rFr r r!Úrecv_stderr_readyÊs zChannel.recv_stderr_readycCs~z|j ||j¡}Wntk r0t ¡‚YnX| t|ƒ¡}|dkrztƒ}| t ¡|  |j ¡|  |¡|j   |¡|S)aš Receive data from the channel's stderr stream. Only channels using `exec_command` or `invoke_shell` without a pty will ever have data on the stderr stream. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by ``nbytes``. If a string of length zero is returned, the channel stream has closed. :param int nbytes: maximum number of bytes to read. :return: received data as a `str` :raises socket.timeout: if no data is ready before the timeout set by `settimeout`. .. versionadded:: 1.1 r)r*r£r+rr¤r¥rJrrRrrSr'r(rXr¦r r r!Ú recv_stderrÙs    zChannel.recv_stderrcCs>|j ¡z"|js|jr W¢dS|jdkW¢S|j ¡XdS)a# Returns true if data can be written to this channel without blocking. This means the channel is either closed (so any write attempt would return immediately) or there is at least one byte of space in the outbound buffer. If there is at least one byte of space in the outbound buffer, a `send` call will succeed immediately and return the number of bytes actually written. :return: ``True`` if a `send` call on this channel would immediately succeed or fail TrN)r/rr‘rrr3rFr r r!Ú send_readyús  zChannel.send_readycCs(tƒ}| t¡| |j¡| ||¡S)a Send data to the channel. Returns the number of bytes sent, or 0 if the channel stream is closed. Applications are responsible for checking that all data has been sent: if only some of the data was transmitted, the application needs to attempt delivery of the remaining data. :param str s: data to send :return: number of bytes actually sent, as an `int` :raises socket.timeout: if no data could be sent before the timeout set by `settimeout`. )rrRrrSr'Ú_send©rÚsr_r r r!Úsends  z Channel.sendcCs2tƒ}| t¡| |j¡| d¡| ||¡S)aº Send data to the channel on the "stderr" stream. This is normally only used by servers to send output from shell commands -- clients won't use this. Returns the number of bytes sent, or 0 if the channel stream is closed. Applications are responsible for checking that all data has been sent: if only some of the data was transmitted, the application needs to attempt delivery of the remaining data. :param str s: data to send. :return: number of bytes actually sent, as an `int`. :raises socket.timeout: if no data could be sent before the timeout set by `settimeout`. .. versionadded:: 1.1 é)rrRrrSr'r¬r­r r r!Ú send_stderr#s    zChannel.send_stderrcCs |r| |¡}||d…}qdS)aß Send data to the channel, without allowing partial results. Unlike `send`, this method continues to send data from the given string until either all data has been sent or an error occurs. Nothing is returned. :param str s: data to send. :raises socket.timeout: if sending stalled for longer than the timeout set by `settimeout`. :raises socket.error: if an error occurred before the entire string was sent. .. note:: If the channel is closed while only part of the data has been sent, there is no way to determine how much data (if any) was sent. This is irritating, but identically follows Python's API. N)r¯©rr®Úsentr r r!Úsendall;s zChannel.sendallcCs |r| |¡}||d…}qdS)aE Send data to the channel's "stderr" stream, without allowing partial results. Unlike `send_stderr`, this method continues to send data from the given string until all data has been sent or an error occurs. Nothing is returned. :param str s: data to send to the client as "stderr" output. :raises socket.timeout: if sending stalled for longer than the timeout set by `settimeout`. :raises socket.error: if an error occurred before the entire string was sent. .. versionadded:: 1.1 N)r±r²r r r!Úsendall_stderrRs zChannel.sendall_stderrcGst|gt|ƒŽS)a( Return a file-like object associated with this channel. The optional ``mode`` and ``bufsize`` arguments are interpreted the same way as by the built-in ``file()`` function in Python. :return: `.ChannelFile` object which can be used for Python file I/O. )Ú ChannelFileÚlist©rÚparamsr r r!ÚmakefilegszChannel.makefilecGst|gt|ƒŽS)a} Return a file-like object associated with this channel's stderr stream. Only channels using `exec_command` or `invoke_shell` without a pty will ever have data on the stderr stream. The optional ``mode`` and ``bufsize`` arguments are interpreted the same way as by the built-in ``file()`` function in Python. For a client, it only makes sense to open this file for reading. For a server, it only makes sense to open this file for writing. :returns: `.ChannelStderrFile` object which can be used for Python file I/O. .. versionadded:: 1.1 )ÚChannelStderrFiler·r¸r r r!Úmakefile_stderrqszChannel.makefile_stderrcGst|gt|ƒŽS)a Return a file-like object associated with this channel's stdin stream. The optional ``mode`` and ``bufsize`` arguments are interpreted the same way as by the built-in ``file()`` function in Python. For a client, it only makes sense to open this file for writing. For a server, it only makes sense to open this file for reading. :returns: `.ChannelStdinFile` object which can be used for Python file I/O. .. versionadded:: 2.6 )ÚChannelStdinFiler·r¸r r r!Úmakefile_stdinƒszChannel.makefile_stdincCst|j ¡zX|jdk r$|j ¡W¢BSt ¡|_t |j¡\}}|j  |¡|j   |¡|j ¡W¢S|j ¡XdS)a» Returns an OS-level file descriptor which can be used for polling, but but not for reading or writing. This is primarily to allow Python's ``select`` module to work. The first time ``fileno`` is called on a channel, a pipe is created to simulate real OS-level file descriptor (FD) behavior. Because of this, two OS-level FDs are created, which will use up FDs faster than normal. (You won't notice this effect unless you have hundreds of channels open at the same time.) :return: an OS-level file descriptor (`int`) .. warning:: This method causes channel reads to be slightly less efficient. N) r/rr‘r>ÚfilenorZ make_pipeZ make_or_piper)Z set_eventr*)rÚp1Úp2r r r!r¿”s     zChannel.filenocCsb|dks|dkrd|_|dks&|dkr^|j ¡z | ¡}W5|j ¡X|dk r^|j |¡dS)a¨ Shut down one or both halves of the connection. If ``how`` is 0, further receives are disallowed. If ``how`` is 1, further sends are disallowed. If ``how`` is 2, further sends and receives are disallowed. This closes the stream in one or both directions. :param int how: 0 (stop receiving), 1 (stop sending), or 2 (stop receiving and sending). rér°N)rr/rr‘Ú _send_eofr(rX)rÚhowr_r r r!Úshutdown²s    zChannel.shutdowncCs| d¡dS)a| Shutdown the receiving side of this socket, closing the stream in the incoming direction. After this call, future reads on this channel will fail instantly. This is a convenience method, equivalent to ``shutdown(0)``, for people who don't make it a habit to memorize unix constants from the 1970s. .. versionadded:: 1.2 rN©rÅrFr r r!Ú shutdown_readÉs zChannel.shutdown_readcCs| d¡dS)a{ Shutdown the sending side of this socket, closing the stream in the outgoing direction. After this call, future writes on this channel will fail instantly. This is a convenience method, equivalent to ``shutdown(1)``, for people who don't make it a habit to memorize unix constants from the 1970s. .. versionadded:: 1.2 r°NrÆrFr r r!Úshutdown_writeÕs zChannel.shutdown_writecCs|jSrD)rrFr r r!Ú_closedászChannel._closedcCs||_t |j ¡¡|_dSrD)r(rr<Zget_log_channelr=)rr(r r r!Ú_set_transportészChannel._set_transportcCs2||_||_|d|_d|_| td |¡¡dS)Né rzMax packet in: {} bytes)r2r4r6r7Ú_logr rI)rÚ window_sizeÚmax_packet_sizer r r!Ú _set_windowís  zChannel._set_windowcCs8||_||_|j |¡|_d|_| td |j¡¡dS)Nr°zMax packet out: {} bytes) r'r3r(Z_sanitize_packet_sizer5rrÌr rI)rr&rÍrÎr r r!Ú_set_remote_channelösÿ ÿzChannel._set_remote_channelcCs(| td |j¡¡d|_|j ¡dS)NzSesch channel {} request okT)rÌr rIr&r@r?Úsetrcr r r!Ú_request_successs zChannel._request_successcCsF|j ¡z | ¡}W5|j ¡X|D]}|dk r(|j |¡q(dSrD)r/rr‘rŸr(rX©rr_r r r r!Ú_request_faileds   zChannel._request_failedcCs(t|tƒr|}n| ¡}|j |¡dSrD)Ú isinstancerÚ get_binaryr)Úfeed)rr_r®r r r!r“s z Channel._feedcCsP| ¡}| ¡}|dkr.| td |¡¡dS|jr@| |¡n |j |¡dS)Nr°z)unknown extended_data type {}; discarding) Úget_intrÖrÌr rIrAr“r*r×)rr_Úcoder®r r r!Ú_feed_extendedsÿ zChannel._feed_extendedcCsX| ¡}|j ¡z4|jr,| td |¡¡|j|7_|j   ¡W5|j ¡XdS)Nz window up {}) rØr/rr‘r,rÌr rIr3r1Ú notifyAll)rr_r§r r r!Ú_window_adjust&s zChannel._window_adjustc CsŒ| ¡}| ¡}|jj}d}|dkr@| ¡|_|j ¡d}n|dkrPd}nø|dkr°| ¡}| ¡}| ¡}| ¡} | ¡} | ¡} |dkr–d}n|  ||||| | | ¡}n˜|dkrÔ|dkrÆd}n |  |¡}nt|dkr| ¡} | ¡} |dkrüd}n|  || | ¡}n:|dkr@| ¡}|dkr0d}n |  ||¡}n|d krp| ¡} |dkrbd}n |  || ¡}nØ|d kr¾| ¡}| ¡}| ¡} | ¡} |dkrªd}n| |||| | ¡}nŠ|d kr | ¡}| ¡}| ¡}| ¡}|dkrød}n| |||||¡}n<|d kr2|dkr&d}n | |¡}n| td  |¡¡d}|rˆtƒ}|rf| t¡n | t¡| |j¡|j |¡dS)NFr{Tzxon-xoffrQrarurerhrjrr‰zUnhandled channel request "{}")Zget_textZ get_booleanr(Z server_objectrØrBr9rÑZ get_stringZcheck_channel_pty_requestZcheck_channel_shell_requestZcheck_channel_env_requestZcheck_channel_exec_requestZcheck_channel_subsystem_requestZ#check_channel_window_change_requestrÖZcheck_channel_x11_requestZ#check_channel_forward_agent_requestrÌr rIrrRr r rSr'rX)rr_ÚkeyZ want_replyÚserverÚokrZr[r\Z pixelwidthZ pixelheightÚmodesrprqÚcmdr†Z auth_protor…r„r r r!Ú_handle_request1s²  ÿ       ÿ  û      zChannel._handle_requestcCsh|j ¡z8|js@d|_|j ¡|j ¡|jdk r@|j ¡W5|j ¡X|  t d  |j ¡¡dS)NTzEOF received ({})) r/rr‘rr)rEr*r>Ú set_foreverrÌr rIr;rcr r r!Ú _handle_eofs     zChannel._handle_eofcCsT|j ¡z| ¡}|j |j¡W5|j ¡X|D]}|dk r6|j |¡q6dSrD)r/rr‘rŸr(Ú_unlink_channelr&rXrÓr r r!Ú _handle_closešs  zChannel._handle_closecCspt|ƒ}|j ¡z@|jr$t d¡‚| |¡}|dkr>W¢dS| |d|…¡W5|j ¡X|j   |¡|S)NzSocket is closedr) rJr/rr‘rr¤ÚerrorÚ_wait_for_send_windowrTr(rX)rr®r_Úsizer r r!r¬§s     z Channel._sendcGs&|jj|d|jd|f|žŽdS)Nz[chan z] )r=Úlogr;)rÚlevelÚmsgrr r r!rÌ»sz Channel._logcCs|j ¡d|_dS)NF)r?Úclearr@rFr r r!rW¾s zChannel._event_pendingcCsD|j ¡|j ¡st‚|jr"dS|j ¡}|dkrrãrFr r r!Ú _set_closedÌs      zChannel._set_closedcCsD|jr dStƒ}| t¡| |j¡d|_| td |j ¡¡|S)NTz EOF sent ({})) rrrRr rSr'rÌr rIr;rcr r r!rÃØs  zChannel._send_eofcCsD|jr |jrdS| ¡}tƒ}| t¡| |j¡| ¡||fS)N)NN) rrrÃrrRrrSr'rî)rÚm1Úm2r r r!rŸãs   zChannel._close_internalcCs@|jr dS|j ¡z| ¡|j |j¡W5|j ¡XdSrD)rr/rr‘rîr(rår&rFr r r!Ú_unlinkðs zChannel._unlinkcCsž|j ¡z‚|js|js|js&W¢ldS|jr>| td  |¡¡|j |7_ |j |j kr`W¢2dS|jrz| td  |j ¡¡|j }d|_ |W¢S|j ¡XdS)Nrz addwindow {}zaddwindow send {}) r/rr‘rrrr,rÌr rIr7r6)rÚnrLr r r!r¥üs$   ÿzChannel._check_add_windowcCsî|js |jrdS|jdkrŠ|jdkr,t ¡‚|j}|jdkrŠ|jsH|jrLdSt ¡}|j |¡|dk r2|t ¡|8}|dkr2t ¡‚q2|js–|jršdS|j|krª|j}|jd|krÂ|jd}|j|8_|j rê|  t d  |j¡¡|S)aD (You are already holding the lock.) Wait for the send window to open up, and allocate up to ``size`` bytes for transmission. If no space opens up before the timeout, a timeout exception is raised. Returns the number of bytes available to send (may be less than requested). rr›Né@zwindow down to {}) rrr3r+r¤Útimer1rxr5r,rÌr rI)rrér+Zthenr r r!rès2          zChannel._wait_for_send_window)rNrOrPrr)rOrPrr)rNNFN)EÚ__name__Ú __module__Ú __qualname__Ú__doc__rCrGrMr#r`rdrgrirlrtrnrwrzr}rˆrŠr‹rrŽrr—r™ršrržrEr¢r¨r©rªr«r¯r±r´rµrºr¼r¾r¿rÅrÇrÈÚpropertyrÉrÊrÏrÐrÒrÔr“rÚrÜrârärær¬rÌrWrYrîrÃrŸrñr¥rèr r r r!r$Ms¨,ú )      ú B   %  !         \       r$c@s2eZdZdZd dd„Zdd„Zdd „Zd d „Zd S)r¶aà A file-like wrapper around `.Channel`. A ChannelFile is created by calling `Channel.makefile`. .. warning:: To correctly emulate the file object created from a socket's `makefile ` method, a `.Channel` and its `.ChannelFile` should be able to be closed or garbage-collected independently. Currently, closing the `ChannelFile` does nothing but flush the buffer. Úrr%cCs ||_t |¡| ||¡dSrD)ÚchannelrrCZ _set_mode)rrûÚmodeÚbufsizer r r!rCEs zChannelFile.__init__cCsdt|jƒdS)zP Returns a string representation of this object, for debugging. zs:  0      t