diff options
Diffstat (limited to 'paramiko/transport.py')
-rw-r--r-- | paramiko/transport.py | 131 |
1 files changed, 76 insertions, 55 deletions
diff --git a/paramiko/transport.py b/paramiko/transport.py index 22348f87..f72eebaf 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -139,7 +139,7 @@ class Transport(threading.Thread, ClosingContextManager): _DECRYPT = object() _PROTO_ID = "2.0" - _CLIENT_ID = "paramiko_%s" % paramiko.__version__ + _CLIENT_ID = "paramiko_{}".format(paramiko.__version__) # These tuples of algorithm identifiers are in preference order; do not # reorder without reason! @@ -369,7 +369,7 @@ class Transport(threading.Thread, ClosingContextManager): break else: raise SSHException( - "Unable to connect to %s: %s" % (hostname, reason) + "Unable to connect to {}: {}".format(hostname, reason) ) # okay, normal socket-ish flow here... threading.Thread.__init__(self) @@ -456,17 +456,20 @@ class Transport(threading.Thread, ClosingContextManager): """ Returns a string representation of this object, for debugging. """ - out = "<paramiko.Transport at %s" % hex(long(id(self)) & xffffffff) + id_ = hex(long(id(self)) & xffffffff) + out = "<paramiko.Transport at {}".format(id_) if not self.active: out += " (unconnected)" else: if self.local_cipher != "": - out += " (cipher %s, %d bits)" % ( + out += " (cipher {}, {:d} bits)".format( self.local_cipher, self._cipher_info[self.local_cipher]["key-size"] * 8, ) if self.is_authenticated(): - out += " (active; %d open channel(s))" % len(self._channels) + out += " (active; {} open channel(s))".format( + len(self._channels) + ) elif self.initial_kex_done: out += " (connected; awaiting auth)" else: @@ -1106,7 +1109,7 @@ class Transport(threading.Thread, ClosingContextManager): m.add_boolean(wait) if data is not None: m.add(*data) - self._log(DEBUG, 'Sending global request "%s"' % kind) + self._log(DEBUG, 'Sending global request "{}"'.format(kind)) self._send_user_message(m) if not wait: return None @@ -1226,15 +1229,20 @@ class Transport(threading.Thread, ClosingContextManager): self._log(DEBUG, "Bad host key from server") self._log( DEBUG, - "Expected: %s: %s" - % (hostkey.get_name(), repr(hostkey.asbytes())), + "Expected: {}: {}".format( + hostkey.get_name(), repr(hostkey.asbytes()) + ), ) self._log( DEBUG, - "Got : %s: %s" % (key.get_name(), repr(key.asbytes())), + "Got : {}: {}".format( + key.get_name(), repr(key.asbytes()) + ), ) raise SSHException("Bad host key from server") - self._log(DEBUG, "Host key verified (%s)" % hostkey.get_name()) + self._log( + DEBUG, "Host key verified ({})".format(hostkey.get_name()) + ) if (pkey is not None) or (password is not None) or gss_auth or gss_kex: if gss_auth: @@ -1345,7 +1353,7 @@ class Transport(threading.Thread, ClosingContextManager): :param str username: the username to authenticate as :return: - `list` of auth types permissible for the next stage of + list of auth types permissible for the next stage of authentication (normally empty) :raises: @@ -1400,7 +1408,7 @@ class Transport(threading.Thread, ClosingContextManager): ``True`` if an attempt at an automated "interactive" password auth should be made if the server doesn't support normal password auth :return: - `list` of auth types permissible for the next stage of + list of auth types permissible for the next stage of authentication (normally empty) :raises: @@ -1473,7 +1481,7 @@ class Transport(threading.Thread, ClosingContextManager): an event to trigger when the authentication attempt is complete (whether it was successful or not) :return: - `list` of auth types permissible for the next stage of + list of auth types permissible for the next stage of authentication (normally empty) :raises: @@ -1531,7 +1539,7 @@ class Transport(threading.Thread, ClosingContextManager): :param callable handler: a handler for responding to server questions :param str submethods: a string list of desired submethods (optional) :return: - `list` of auth types permissible for the next stage of + list of auth types permissible for the next stage of authentication (normally empty). :raises: `.BadAuthenticationType` -- if public-key authentication isn't @@ -1583,7 +1591,6 @@ class Transport(threading.Thread, ClosingContextManager): :param bool gss_deleg_creds: Delegate credentials or not :return: list of auth types permissible for the next stage of authentication (normally empty) - :rtype: list :raises: `.BadAuthenticationType` -- if gssapi-with-mic isn't allowed by the server (and no event was passed in) :raises: @@ -1607,7 +1614,7 @@ class Transport(threading.Thread, ClosingContextManager): :param str username: The username to authenticate as. :returns: - a `list` of auth types permissible for the next stage of + a list of auth types permissible for the next stage of authentication (normally empty) :raises: `.BadAuthenticationType` -- if GSS-API Key Exchange was not performed (and no event was passed @@ -1805,7 +1812,9 @@ class Transport(threading.Thread, ClosingContextManager): raise SSHException("Unknown host key type") if not key.verify_ssh_sig(self.H, Message(sig)): raise SSHException( - "Signature verification (%s) failed." % self.host_key_type + "Signature verification ({}) failed.".format( + self.host_key_type + ) ) # noqa self.host_key = key @@ -1819,9 +1828,8 @@ class Transport(threading.Thread, ClosingContextManager): # Fallback to SHA1 for kex engines that fail to specify a hex # algorithm, or for e.g. transport tests that don't run kexinit. hash_algo = getattr(self.kex_engine, "hash_algo", None) - hash_select_msg = "kex engine %s specified hash_algo %r" % ( - self.kex_engine.__class__.__name__, - hash_algo, + hash_select_msg = "kex engine {} specified hash_algo {!r}".format( + self.kex_engine.__class__.__name__, hash_algo ) if hash_algo is None: hash_algo = sha1 @@ -1945,14 +1953,15 @@ class Transport(threading.Thread, ClosingContextManager): _active_threads.append(self) tid = hex(long(id(self)) & xffffffff) if self.server_mode: - self._log(DEBUG, "starting thread (server mode): %s" % tid) + self._log(DEBUG, "starting thread (server mode): {}".format(tid)) else: - self._log(DEBUG, "starting thread (client mode): %s" % tid) + self._log(DEBUG, "starting thread (client mode): {}".format(tid)) try: try: self.packetizer.write_all(b(self.local_version + "\r\n")) self._log( - DEBUG, "Local version/idstring: %s" % self.local_version + DEBUG, + "Local version/idstring: {}".format(self.local_version), ) # noqa self._check_banner() # The above is actually very much part of the handshake, but @@ -1984,8 +1993,9 @@ class Transport(threading.Thread, ClosingContextManager): if len(self._expected_packet) > 0: if ptype not in self._expected_packet: raise SSHException( - "Expecting packet from %r, got %d" - % (self._expected_packet, ptype) + "Expecting packet from {!r}, got {:d}".format( + self._expected_packet, ptype + ) ) # noqa self._expected_packet = tuple() if (ptype >= 30) and (ptype <= 41): @@ -2006,15 +2016,17 @@ class Transport(threading.Thread, ClosingContextManager): elif chanid in self.channels_seen: self._log( DEBUG, - "Ignoring message for dead channel %d" - % chanid, - ) # noqa + "Ignoring message for dead channel {:d}".format( # noqa + chanid + ), + ) else: self._log( ERROR, - "Channel request for unknown channel %d" - % chanid, - ) # noqa + "Channel request for unknown channel {:d}".format( # noqa + chanid + ), + ) break elif ( self.auth_handler is not None @@ -2030,7 +2042,7 @@ class Transport(threading.Thread, ClosingContextManager): # itself literally MSG_UNIMPLEMENTED, in which case, we # just shut up to avoid causing a useless loop). name = MSG_NAMES[ptype] - warning = "Oops, unhandled type {0} ({1!r})".format( + warning = "Oops, unhandled type {} ({!r})".format( ptype, name ) self._log(WARNING, warning) @@ -2050,7 +2062,7 @@ class Transport(threading.Thread, ClosingContextManager): except socket.error as e: if type(e.args) is tuple: if e.args: - emsg = "%s (%d)" % (e.args[1], e.args[0]) + emsg = "{} ({:d})".format(e.args[1], e.args[0]) else: # empty tuple, e.g. socket.timeout emsg = str(e) or repr(e) else: @@ -2091,11 +2103,11 @@ class Transport(threading.Thread, ClosingContextManager): # Log useful, non-duplicative line re: an agreed-upon algorithm. # Old code implied algorithms could be asymmetrical (different for # inbound vs outbound) so we preserve that possibility. - msg = "{0} agreed: ".format(which) + msg = "{} agreed: ".format(which) if local == remote: msg += local else: - msg += "local={0}, remote={1}".format(local, remote) + msg += "local={}, remote={}".format(local, remote) self._log(DEBUG, msg) # protocol stages @@ -2137,7 +2149,7 @@ class Transport(threading.Thread, ClosingContextManager): raise SSHException('Indecipherable protocol version "' + buf + '"') # save this server version string for later self.remote_version = buf - self._log(DEBUG, "Remote version/idstring: %s" % buf) + self._log(DEBUG, "Remote version/idstring: {}".format(buf)) # pull off any attached comment # NOTE: comment used to be stored in a variable and then...never used. # since 2003. ca 877cd974b8182d26fa76d566072917ea67b64e67 @@ -2151,9 +2163,9 @@ class Transport(threading.Thread, ClosingContextManager): version = segs[1] client = segs[2] if version != "1.99" and version != "2.0": - msg = "Incompatible version ({0} instead of 2.0)" + msg = "Incompatible version ({} instead of 2.0)" raise SSHException(msg.format(version)) - msg = "Connected (version {0}, client {1})".format(version, client) + msg = "Connected (version {}, client {})".format(version, client) self._log(INFO, msg) def _send_kex_init(self): @@ -2270,7 +2282,7 @@ class Transport(threading.Thread, ClosingContextManager): "Incompatible ssh peer (no acceptable kex algorithm)" ) # noqa self.kex_engine = self._kex_info[agreed_kex[0]](self) - self._log(DEBUG, "Kex agreed: %s" % agreed_kex[0]) + self._log(DEBUG, "Kex agreed: {}".format(agreed_kex[0])) if self.server_mode: available_server_keys = list( @@ -2387,7 +2399,8 @@ class Transport(threading.Thread, ClosingContextManager): len(agreed_local_compression) == 0 or len(agreed_remote_compression) == 0 ): - msg = "Incompatible ssh server (no acceptable compression) {0!r} {1!r} {2!r}" # noqa + msg = "Incompatible ssh server (no acceptable compression)" + msg += " {!r} {!r} {!r}" raise SSHException( msg.format( agreed_local_compression, @@ -2529,15 +2542,16 @@ class Transport(threading.Thread, ClosingContextManager): def _parse_disconnect(self, m): code = m.get_int() desc = m.get_text() - self._log(INFO, "Disconnect (code %d): %s" % (code, desc)) + self._log(INFO, "Disconnect (code {:d}): {}".format(code, desc)) def _parse_global_request(self, m): kind = m.get_text() - self._log(DEBUG, 'Received global request "%s"' % kind) + self._log(DEBUG, 'Received global request "{}"'.format(kind)) want_reply = m.get_boolean() if not self.server_mode: self._log( - DEBUG, 'Rejecting "%s" global request from server.' % kind + DEBUG, + 'Rejecting "{}" global request from server.'.format(kind), ) ok = False elif kind == "tcpip-forward": @@ -2592,7 +2606,7 @@ class Transport(threading.Thread, ClosingContextManager): chan._set_remote_channel( server_chanid, server_window_size, server_max_packet_size ) - self._log(DEBUG, "Secsh channel %d opened." % chanid) + self._log(DEBUG, "Secsh channel {:d} opened.".format(chanid)) if chanid in self.channel_events: self.channel_events[chanid].set() del self.channel_events[chanid] @@ -2608,8 +2622,9 @@ class Transport(threading.Thread, ClosingContextManager): reason_text = CONNECTION_FAILED_CODE.get(reason, "(unknown code)") self._log( ERROR, - "Secsh channel %d open FAILED: %s: %s" - % (chanid, reason_str, reason_text), + "Secsh channel {:d} open FAILED: {}: {}".format( + chanid, reason_str, reason_text + ), ) self.lock.acquire() try: @@ -2644,8 +2659,9 @@ class Transport(threading.Thread, ClosingContextManager): origin_port = m.get_int() self._log( DEBUG, - "Incoming x11 connection from %s:%d" - % (origin_addr, origin_port), + "Incoming x11 connection from {}:{:d}".format( + origin_addr, origin_port + ), ) self.lock.acquire() try: @@ -2659,8 +2675,9 @@ class Transport(threading.Thread, ClosingContextManager): origin_port = m.get_int() self._log( DEBUG, - "Incoming tcp forwarded connection from %s:%d" - % (origin_addr, origin_port), + "Incoming tcp forwarded connection from {}:{:d}".format( + origin_addr, origin_port + ), ) self.lock.acquire() try: @@ -2669,7 +2686,8 @@ class Transport(threading.Thread, ClosingContextManager): self.lock.release() elif not self.server_mode: self._log( - DEBUG, 'Rejecting "%s" channel request from server.' % kind + DEBUG, + 'Rejecting "{}" channel request from server.'.format(kind), ) reject = True reason = OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED @@ -2696,7 +2714,8 @@ class Transport(threading.Thread, ClosingContextManager): ) if reason != OPEN_SUCCEEDED: self._log( - DEBUG, 'Rejecting "%s" channel request from client.' % kind + DEBUG, + 'Rejecting "{}" channel request from client.'.format(kind), ) reject = True if reject: @@ -2730,7 +2749,9 @@ class Transport(threading.Thread, ClosingContextManager): m.add_int(self.default_window_size) m.add_int(self.default_max_packet_size) self._send_message(m) - self._log(DEBUG, "Secsh channel %d (%s) opened.", my_chanid, kind) + self._log( + DEBUG, "Secsh channel {:d} ({}) opened.".format(my_chanid, kind) + ) if kind == "auth-agent@openssh.com": self._forward_agent_handler(chan) elif kind == "x11": @@ -2747,7 +2768,7 @@ class Transport(threading.Thread, ClosingContextManager): m.get_boolean() # always_display msg = m.get_string() m.get_string() # language - self._log(DEBUG, "Debug msg: {0}".format(util.safe_string(msg))) + self._log(DEBUG, "Debug msg: {}".format(util.safe_string(msg))) def _get_subsystem_handler(self, name): try: @@ -2803,7 +2824,7 @@ class SecurityOptions(object): """ Returns a string representation of this object, for debugging. """ - return "<paramiko.SecurityOptions for %s>" % repr(self._transport) + return "<paramiko.SecurityOptions for {!r}>".format(self._transport) def _set(self, name, orig, x): if type(x) is list: |