summaryrefslogtreecommitdiff
path: root/paramiko/channel.py
diff options
context:
space:
mode:
authorJeff Forcier <jeff@bitprophet.org>2017-05-31 18:22:18 -0700
committerJeff Forcier <jeff@bitprophet.org>2017-05-31 18:22:18 -0700
commita856ae02cab32b55847474241ccf36664ad584af (patch)
treeea02103dc3b25a72ffad3109e350bff10acde687 /paramiko/channel.py
parenta80bbe0e2dd8768e93feba72f8eeacbe1378262f (diff)
parent383e102b54464f2be8fe5aaa3b1e9bd94ad0b788 (diff)
downloadparamiko-a856ae02cab32b55847474241ccf36664ad584af.tar.gz
Merge branch '2.0' into 2.1
Diffstat (limited to 'paramiko/channel.py')
-rw-r--r--paramiko/channel.py104
1 files changed, 66 insertions, 38 deletions
diff --git a/paramiko/channel.py b/paramiko/channel.py
index 89e9e5a9..32679f0e 100644
--- a/paramiko/channel.py
+++ b/paramiko/channel.py
@@ -28,10 +28,11 @@ import threading
from functools import wraps
from paramiko import util
-from paramiko.common import 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
+from paramiko.common import (
+ 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,
+)
from paramiko.message import Message
from paramiko.py3compat import bytes_types
from paramiko.ssh_exception import SSHException
@@ -51,10 +52,10 @@ def open_only(func):
@wraps(func)
def _check(self, *args, **kwds):
if (
- self.closed
- or self.eof_received
- or self.eof_sent
- or not self.active
+ self.closed or
+ self.eof_received or
+ self.eof_sent or
+ not self.active
):
raise SSHException('Channel is not open')
return func(self, *args, **kwds)
@@ -74,7 +75,7 @@ class Channel (ClosingContextManager):
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.
"""
@@ -157,7 +158,8 @@ class Channel (ClosingContextManager):
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 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
@@ -399,8 +401,14 @@ class Channel (ClosingContextManager):
self.transport._send_user_message(m)
@open_only
- def request_x11(self, screen_number=0, auth_protocol=None, auth_cookie=None,
- single_connection=False, handler=None):
+ def request_x11(
+ self,
+ screen_number=0,
+ auth_protocol=None,
+ auth_cookie=None,
+ single_connection=False,
+ handler=None
+ ):
"""
Request an x11 session on this channel. If the server allows it,
further x11 requests can be made from the server to the client,
@@ -416,7 +424,7 @@ class Channel (ClosingContextManager):
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
@@ -468,7 +476,8 @@ class Channel (ClosingContextManager):
:param function handler:
a required handler to use for incoming SSH Agent connections
- :return: True if we are ok, else False (at that time we always return ok)
+ :return: True if we are ok, else False
+ (at that time we always return ok)
:raises: SSHException in case of channel problem.
"""
@@ -549,16 +558,16 @@ class Channel (ClosingContextManager):
self._feed(data)
return old
- ### socket API
+ # ...socket API...
def settimeout(self, timeout):
"""
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.
+ 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)``.
@@ -652,8 +661,8 @@ class Channel (ClosingContextManager):
"""
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.
+ 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 `bytes`
@@ -803,7 +812,7 @@ class Channel (ClosingContextManager):
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.
@@ -827,7 +836,7 @@ class Channel (ClosingContextManager):
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
"""
while s:
@@ -945,7 +954,7 @@ class Channel (ClosingContextManager):
# attribute instead of a semipublic .closed attribute.
return self.closed
- ### calls from Transport
+ # ...calls from Transport
def _set_transport(self, transport):
self.transport = transport
@@ -954,7 +963,8 @@ class Channel (ClosingContextManager):
def _set_window(self, window_size, max_packet_size):
self.in_window_size = window_size
self.in_max_packet_size = max_packet_size
- # threshold of bytes we receive before we bother to send a window update
+ # threshold of bytes we receive before we bother to send
+ # a window update
self.in_window_threshold = window_size // 10
self.in_window_sofar = 0
self._log(DEBUG, 'Max packet in: %d bytes' % max_packet_size)
@@ -962,8 +972,9 @@ class Channel (ClosingContextManager):
def _set_remote_channel(self, chanid, window_size, max_packet_size):
self.remote_chanid = chanid
self.out_window_size = window_size
- self.out_max_packet_size = self.transport. \
- _sanitize_packet_size(max_packet_size)
+ self.out_max_packet_size = self.transport._sanitize_packet_size(
+ max_packet_size
+ )
self.active = 1
self._log(DEBUG, 'Max packet out: %d bytes' % self.out_max_packet_size)
@@ -995,7 +1006,10 @@ class Channel (ClosingContextManager):
code = m.get_int()
s = m.get_binary()
if code != 1:
- self._log(ERROR, 'unknown extended_data type %d; discarding' % code)
+ self._log(
+ ERROR,
+ 'unknown extended_data type %d; discarding' % code
+ )
return
if self.combine_stderr:
self._feed(s)
@@ -1035,8 +1049,15 @@ class Channel (ClosingContextManager):
if server is None:
ok = False
else:
- ok = server.check_channel_pty_request(self, term, width, height, pixelwidth,
- pixelheight, modes)
+ ok = server.check_channel_pty_request(
+ self,
+ term,
+ width,
+ height,
+ pixelwidth,
+ pixelheight,
+ modes
+ )
elif key == 'shell':
if server is None:
ok = False
@@ -1069,8 +1090,8 @@ class Channel (ClosingContextManager):
if server is None:
ok = False
else:
- ok = server.check_channel_window_change_request(self, width, height, pixelwidth,
- pixelheight)
+ ok = server.check_channel_window_change_request(
+ self, width, height, pixelwidth, pixelheight)
elif key == 'x11-req':
single_connection = m.get_boolean()
auth_proto = m.get_text()
@@ -1079,8 +1100,13 @@ class Channel (ClosingContextManager):
if server is None:
ok = False
else:
- ok = server.check_channel_x11_request(self, single_connection,
- auth_proto, auth_cookie, screen_number)
+ ok = server.check_channel_x11_request(
+ self,
+ single_connection,
+ auth_proto,
+ auth_cookie,
+ screen_number
+ )
elif key == 'auth-agent-req@openssh.com':
if server is None:
ok = False
@@ -1122,7 +1148,7 @@ class Channel (ClosingContextManager):
if m is not None:
self.transport._send_user_message(m)
- ### internals...
+ # ...internals...
def _send(self, s, m):
size = len(s)
@@ -1198,7 +1224,8 @@ class Channel (ClosingContextManager):
return m1, m2
def _unlink(self):
- # server connection could die before we become active: still signal the close!
+ # server connection could die before we become active:
+ # still signal the close!
if self.closed:
return
self.lock.acquire()
@@ -1241,7 +1268,8 @@ class Channel (ClosingContextManager):
# should we block?
if self.timeout == 0.0:
raise socket.timeout()
- # loop here in case we get woken up but a different thread has filled the buffer
+ # loop here in case we get woken up but a different thread has
+ # filled the buffer
timeout = self.timeout
while self.out_window_size == 0:
if self.closed or self.eof_sent: