diff --git a/demos/interactive.py b/demos/interactive.py index b7962b3..7138cd6 100644 --- a/demos/interactive.py +++ b/demos/interactive.py @@ -19,6 +19,7 @@ import socket import sys +from paramiko.py3compat import u # windows does not have termios... try: @@ -49,7 +50,7 @@ def posix_shell(chan): r, w, e = select.select([chan, sys.stdin], [], []) if chan in r: try: - x = chan.recv(1024) + x = u(chan.recv(1024)) if len(x) == 0: sys.stdout.write('\r\n*** EOF\r\n') break diff --git a/paramiko/channel.py b/paramiko/channel.py index 182cd68..d715bb8 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -1059,7 +1059,7 @@ class Channel (object): elif key == 'x11-req': single_connection = m.get_boolean() auth_proto = m.get_text() - auth_cookie = m.get_text() + auth_cookie = m.get_binary() screen_number = m.get_int() if server is None: ok = False diff --git a/paramiko/common.py b/paramiko/common.py index 2392f03..9161f30 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -129,7 +129,7 @@ if PY3: four_byte = b'\x04' max_byte = b'\xff' newline_byte = b'\n' - cr_byte = b'\r' + cr_byte = 13 else: zero_byte = '\x00' one_byte = '\x01' diff --git a/paramiko/file.py b/paramiko/file.py index aca97c6..625737f 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -235,7 +235,7 @@ class BufferedFile (object): self._rbuffer = '' self._pos += len(line) return line - line += new_data + line += b2s(new_data) self._realpos += len(new_data) # find the newline pos = line.find('\n') diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 376041d..4cead1c 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -321,7 +321,7 @@ class PKey (object): try: encryption_type, saltstr = headers['dek-info'].split(',') except: - raise SSHException('Can\'t parse DEK-info in private key file') + raise SSHException("Can't parse DEK-info in private key file") if encryption_type not in self._CIPHER_TABLE: raise SSHException('Unknown private key cipher "%s"' % encryption_type) # if no password was passed in, raise an exception pointing out that we need one @@ -373,10 +373,10 @@ class PKey (object): n = blocksize - len(data) % blocksize #data += rng.read(n) # that would make more sense ^, but it confuses openssh. - data += '\0' * n + data += zero_byte * n data = cipher.new(key, mode, salt).encrypt(data) f.write('Proc-Type: 4,ENCRYPTED\n') - f.write('DEK-Info: %s,%s\n' % (cipher_name, hexlify(salt).upper())) + f.write('DEK-Info: %s,%s\n' % (cipher_name, u(hexlify(salt)).upper())) f.write('\n') s = u(base64.encodestring(data)) # re-wrap to 64-char lines diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index c4a85ac..e4830da 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,6 +1,6 @@ import sys -__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_type', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] +__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_type', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] PY3 = sys.version_info[0] >= 3 @@ -47,6 +47,9 @@ if PY3: else: raise TypeError("Expected unicode or bytes, got %r" % s) + def b2s(s): + return s.decode() if isinstance(s, bytes) else s + import io StringIO = io.StringIO # NOQA BytesIO = io.BytesIO # NOQA @@ -97,6 +100,9 @@ else: else: raise TypeError("Expected unicode or bytes, got %r" % s) + def b2s(s): + return s + try: import cStringIO StringIO = cStringIO.StringIO # NOQA diff --git a/paramiko/transport.py b/paramiko/transport.py index c679a98..46e0fe8 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -805,7 +805,7 @@ class Transport (threading.Thread): """ if not self.active: raise SSHException('SSH session not active') - address = str(address) + address = address port = int(port) response = self.global_request('tcpip-forward', (address, port), wait=True) if response is None: diff --git a/tests/loop.py b/tests/loop.py index cfd7626..b2c73dd 100644 --- a/tests/loop.py +++ b/tests/loop.py @@ -21,7 +21,7 @@ """ import threading, socket -from paramiko.py3compat import * +from paramiko.common import * class LoopSocket (object): @@ -47,6 +47,7 @@ class LoopSocket (object): self.__lock.release() def send(self, data): + data = asbytes(data) if self.__mate is None: # EOF raise EOFError() diff --git a/tests/test_client.py b/tests/test_client.py index 0959ac9..a8d0463 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -29,6 +29,7 @@ import weakref from binascii import hexlify from tests.util import test_path import paramiko +from paramiko.py3compat import b class NullServer (paramiko.ServerInterface): @@ -44,7 +45,7 @@ class NullServer (paramiko.ServerInterface): return paramiko.AUTH_FAILED def check_auth_publickey(self, username, key): - if (key.get_name() == 'ssh-dss') and (hexlify(key.get_fingerprint()) == '4478f0b9a23cc5182009ff755bc1d26c'): + if (key.get_name() == 'ssh-dss') and (hexlify(key.get_fingerprint()) == b('4478f0b9a23cc5182009ff755bc1d26c')): return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_FAILED diff --git a/tests/test_transport.py b/tests/test_transport.py index ccd44e4..230f4a2 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -362,7 +362,7 @@ class TransportTest(ParamikoTest): self.assertEquals([], w) self.assertEquals([], e) - self.assertEquals('hello\n', chan.recv(6)) + self.assertEquals(b('hello\n'), chan.recv(6)) # and, should be dead again now r, w, e = select.select([chan], [], [], 0.1) @@ -381,7 +381,7 @@ class TransportTest(ParamikoTest): self.assertEquals([chan], r) self.assertEquals([], w) self.assertEquals([], e) - self.assertEquals('', chan.recv(16)) + self.assertEquals(bytes(), chan.recv(16)) # make sure the pipe is still open for now... p = chan._pipe @@ -463,7 +463,7 @@ class TransportTest(ParamikoTest): self.assertEquals(6093, requested[0][1]) x11_server.send('hello') - self.assertEquals('hello', x11_client.recv(5)) + self.assertEquals(b('hello'), x11_client.recv(5)) x11_server.close() x11_client.close() @@ -496,7 +496,7 @@ class TransportTest(ParamikoTest): cch = self.tc.accept() sch.send('hello') - self.assertEquals('hello', cch.recv(5)) + self.assertEquals(b('hello'), cch.recv(5)) sch.close() cch.close() ss.close() @@ -528,12 +528,12 @@ class TransportTest(ParamikoTest): cch.connect(self.server._tcpip_dest) ss, _ = greeting_server.accept() - ss.send('Hello!\n') + ss.send(b('Hello!\n')) ss.close() sch.send(cch.recv(8192)) sch.close() - self.assertEquals('Hello!\n', cs.recv(7)) + self.assertEquals(b('Hello!\n'), cs.recv(7)) cs.close() def test_G_stderr_select(self): @@ -564,7 +564,7 @@ class TransportTest(ParamikoTest): self.assertEquals([], w) self.assertEquals([], e) - self.assertEquals('hello\n', chan.recv_stderr(6)) + self.assertEquals(b('hello\n'), chan.recv_stderr(6)) # and, should be dead again now r, w, e = select.select([chan], [], [], 0.1)