From f73d5f73e512ffe2a7a4eccce262dc4acd5c4307 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:05:47 -0700 Subject: Fix print statements --- demos/demo.py | 35 ++++++++++++++++++----------------- demos/interactive.py | 2 +- 2 files changed, 19 insertions(+), 18 deletions(-) (limited to 'demos') diff --git a/demos/demo.py b/demos/demo.py index aa4bdaa5..cbd7730e 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -45,13 +45,13 @@ def agent_auth(transport, username): return for key in agent_keys: - print 'Trying ssh-agent key %s' % hexlify(key.get_fingerprint()), + print('Trying ssh-agent key %s' % hexlify(key.get_fingerprint())) try: transport.auth_publickey(username, key) - print '... success!' + print('... success!') return except paramiko.SSHException: - print '... nope.' + print('... nope.') def manual_auth(username, hostname): @@ -98,7 +98,7 @@ if len(sys.argv) > 1: else: hostname = raw_input('Hostname: ') if len(hostname) == 0: - print '*** Hostname required.' + print('*** Hostname required.') sys.exit(1) port = 22 if hostname.find(':') >= 0: @@ -111,6 +111,7 @@ try: sock.connect((hostname, port)) except Exception, e: print '*** Connect failed: ' + str(e) + print('*** Connect failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -119,7 +120,7 @@ try: try: t.start_client() except paramiko.SSHException: - print '*** SSH negotiation failed.' + print('*** SSH negotiation failed.') sys.exit(1) try: @@ -128,20 +129,20 @@ try: try: keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) except IOError: - print '*** Unable to open host keys file' + print('*** Unable to open host keys file') keys = {} # check server's host key -- this is important. key = t.get_remote_server_key() - if not keys.has_key(hostname): - print '*** WARNING: Unknown host key!' - elif not keys[hostname].has_key(key.get_name()): - print '*** WARNING: Unknown host key!' + if hostname not in keys: + print('*** WARNING: Unknown host key!') + elif key.get_name() not in keys[hostname]: + print('*** WARNING: Unknown host key!') elif keys[hostname][key.get_name()] != key: - print '*** WARNING: Host key has changed!!!' + print('*** WARNING: Host key has changed!!!') sys.exit(1) else: - print '*** Host key OK.' + print('*** Host key OK.') # get username if username == '': @@ -154,21 +155,21 @@ try: if not t.is_authenticated(): manual_auth(username, hostname) if not t.is_authenticated(): - print '*** Authentication failed. :(' + print('*** Authentication failed. :(') t.close() sys.exit(1) chan = t.open_session() chan.get_pty() chan.invoke_shell() - print '*** Here we go!' - print + print('*** Here we go!\n') interactive.interactive_shell(chan) chan.close() t.close() -except Exception, e: - print '*** Caught exception: ' + str(e.__class__) + ': ' + str(e) +except Exception: + e = sys.exc_info()[1] + print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: t.close() diff --git a/demos/interactive.py b/demos/interactive.py index f3be74d2..b7962b35 100644 --- a/demos/interactive.py +++ b/demos/interactive.py @@ -51,7 +51,7 @@ def posix_shell(chan): try: x = chan.recv(1024) if len(x) == 0: - print '\r\n*** EOF\r\n', + sys.stdout.write('\r\n*** EOF\r\n') break sys.stdout.write(x) sys.stdout.flush() -- cgit v1.2.1 From 66cfa97cce92b1d60383d178887b18dddb999fc1 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:19:30 -0700 Subject: Fix imports --- demos/demo.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'demos') diff --git a/demos/demo.py b/demos/demo.py index cbd7730e..3890eda7 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -30,7 +30,10 @@ import time import traceback import paramiko -import interactive +try: + import interactive +except ImportError: + from . import interactive def agent_auth(transport, username): -- cgit v1.2.1 From 7cdbbf4bdc864bfbbab019787857a9a06d14d7e8 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 17:15:25 -0700 Subject: Fix input --- demos/demo.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'demos') diff --git a/demos/demo.py b/demos/demo.py index 3890eda7..744b5c31 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -59,13 +59,13 @@ def agent_auth(transport, username): def manual_auth(username, hostname): default_auth = 'p' - auth = raw_input('Auth by (p)assword, (r)sa key, or (d)ss key? [%s] ' % default_auth) + auth = input('Auth by (p)assword, (r)sa key, or (d)ss key? [%s] ' % default_auth) if len(auth) == 0: auth = default_auth if auth == 'r': default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') - path = raw_input('RSA key [%s]: ' % default_path) + path = input('RSA key [%s]: ' % default_path) if len(path) == 0: path = default_path try: @@ -76,7 +76,7 @@ def manual_auth(username, hostname): t.auth_publickey(username, key) elif auth == 'd': default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_dsa') - path = raw_input('DSS key [%s]: ' % default_path) + path = input('DSS key [%s]: ' % default_path) if len(path) == 0: path = default_path try: @@ -99,7 +99,7 @@ if len(sys.argv) > 1: if hostname.find('@') >= 0: username, hostname = hostname.split('@') else: - hostname = raw_input('Hostname: ') + hostname = input('Hostname: ') if len(hostname) == 0: print('*** Hostname required.') sys.exit(1) @@ -112,8 +112,8 @@ if hostname.find(':') >= 0: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, port)) -except Exception, e: - print '*** Connect failed: ' + str(e) +except Exception: + e = sys.exc_info()[1] print('*** Connect failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -150,7 +150,7 @@ try: # get username if username == '': default_username = getpass.getuser() - username = raw_input('Username [%s]: ' % default_username) + username = input('Username [%s]: ' % default_username) if len(username) == 0: username = default_username -- cgit v1.2.1 From 7a45d3c70f6f308835ab66e3899e247e0efc17e7 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 15:25:45 -0700 Subject: More type conversion --- demos/interactive.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'demos') diff --git a/demos/interactive.py b/demos/interactive.py index b7962b35..7138cd6c 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 -- cgit v1.2.1 From 8bda3ab2bb5c5cae5baa301148545f6621b4a4c1 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 15:25:57 -0700 Subject: Fix demo_keygen --- demos/demo_keygen.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'demos') diff --git a/demos/demo_keygen.py b/demos/demo_keygen.py index bdd7388d..88facd46 100755 --- a/demos/demo_keygen.py +++ b/demos/demo_keygen.py @@ -19,7 +19,6 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. from __future__ import with_statement -import string import sys from binascii import hexlify @@ -28,6 +27,7 @@ from optparse import OptionParser from paramiko import DSSKey from paramiko import RSAKey from paramiko.ssh_exception import SSHException +from paramiko.py3compat import u usage=""" %prog [-v] [-b bits] -t type [-N new_passphrase] [-f output_keyfile]""" @@ -47,16 +47,16 @@ key_dispatch_table = { def progress(arg=None): if not arg: - print '0%\x08\x08\x08', + sys.stdout.write('0%\x08\x08\x08 ') sys.stdout.flush() elif arg[0] == 'p': - print '25%\x08\x08\x08\x08', + sys.stdout.write('25%\x08\x08\x08\x08 ') sys.stdout.flush() elif arg[0] == 'h': - print '50%\x08\x08\x08\x08', + sys.stdout.write('50%\x08\x08\x08\x08 ') sys.stdout.flush() elif arg[0] == 'x': - print '75%\x08\x08\x08\x08', + sys.stdout.write('75%\x08\x08\x08\x08 ') sys.stdout.flush() if __name__ == '__main__': @@ -92,8 +92,8 @@ if __name__ == '__main__': parser.print_help() sys.exit(0) - for o in default_values.keys(): - globals()[o] = getattr(options, o, default_values[string.lower(o)]) + for o in list(default_values.keys()): + globals()[o] = getattr(options, o, default_values[o.lower()]) if options.newphrase: phrase = getattr(options, 'newphrase') @@ -106,7 +106,7 @@ if __name__ == '__main__': if ktype == 'dsa' and bits > 1024: raise SSHException("DSA Keys must be 1024 bits") - if not key_dispatch_table.has_key(ktype): + if ktype not in key_dispatch_table: raise SSHException("Unknown %s algorithm to generate keys pair" % ktype) # generating private key @@ -121,7 +121,7 @@ if __name__ == '__main__': f.write(" %s" % comment) if options.verbose: - print "done." + print("done.") - hash = hexlify(pub.get_fingerprint()) - print "Fingerprint: %d %s %s.pub (%s)" % (bits, ":".join([ hash[i:2+i] for i in range(0, len(hash), 2)]), filename, string.upper(ktype)) + hash = u(hexlify(pub.get_fingerprint())) + print("Fingerprint: %d %s %s.pub (%s)" % (bits, ":".join([ hash[i:2+i] for i in range(0, len(hash), 2)]), filename, ktype.upper())) -- cgit v1.2.1 From bc683ac3653e2d66fe18c020374ec5b7c723ed53 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 15:35:35 -0700 Subject: Fix demo_server --- demos/demo_server.py | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'demos') diff --git a/demos/demo_server.py b/demos/demo_server.py index 915b0c67..deb21387 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -27,6 +27,7 @@ import threading import traceback import paramiko +from paramiko.py3compat import b, u # setup logging @@ -35,16 +36,16 @@ paramiko.util.log_to_file('demo_server.log') host_key = paramiko.RSAKey(filename='test_rsa.key') #host_key = paramiko.DSSKey(filename='test_dss.key') -print 'Read key: ' + hexlify(host_key.get_fingerprint()) +print('Read key: ' + u(hexlify(host_key.get_fingerprint()))) class Server (paramiko.ServerInterface): # 'data' is the output of base64.encodestring(str(key)) # (using the "user_rsa_key" files) - data = 'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + \ - 'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \ - 'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \ - 'UWT10hcuO4Ks8=' + data = b('AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + \ + 'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \ + 'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \ + 'UWT10hcuO4Ks8=') good_pub_key = paramiko.RSAKey(data=base64.decodestring(data)) def __init__(self): @@ -61,7 +62,7 @@ class Server (paramiko.ServerInterface): return paramiko.AUTH_FAILED def check_auth_publickey(self, username, key): - print 'Auth attempt with key: ' + hexlify(key.get_fingerprint()) + print('Auth attempt with key: ' + u(hexlify(key.get_fingerprint()))) if (username == 'robey') and (key == self.good_pub_key): return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_FAILED @@ -83,47 +84,49 @@ try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', 2200)) -except Exception, e: - print '*** Bind failed: ' + str(e) +except Exception: + e = sys.exc_info()[1] + print('*** Bind failed: ' + str(e)) traceback.print_exc() sys.exit(1) try: sock.listen(100) - print 'Listening for connection ...' + print('Listening for connection ...') client, addr = sock.accept() -except Exception, e: - print '*** Listen/accept failed: ' + str(e) +except Exception: + e = sys.exc_info()[1] + print('*** Listen/accept failed: ' + str(e)) traceback.print_exc() sys.exit(1) -print 'Got a connection!' +print('Got a connection!') try: t = paramiko.Transport(client) try: t.load_server_moduli() except: - print '(Failed to load moduli -- gex will be unsupported.)' + print('(Failed to load moduli -- gex will be unsupported.)') raise t.add_server_key(host_key) server = Server() try: t.start_server(server=server) - except paramiko.SSHException, x: - print '*** SSH negotiation failed.' + except paramiko.SSHException: + print('*** SSH negotiation failed.') sys.exit(1) # wait for auth chan = t.accept(20) if chan is None: - print '*** No channel.' + print('*** No channel.') sys.exit(1) - print 'Authenticated!' + print('Authenticated!') server.event.wait(10) if not server.event.isSet(): - print '*** Client never asked for a shell.' + print('*** Client never asked for a shell.') sys.exit(1) chan.send('\r\n\r\nWelcome to my dorky little BBS!\r\n\r\n') @@ -135,8 +138,9 @@ try: chan.send('\r\nI don\'t like you, ' + username + '.\r\n') chan.close() -except Exception, e: - print '*** Caught exception: ' + str(e.__class__) + ': ' + str(e) +except Exception: + e = sys.exc_info()[1] + print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: t.close() -- cgit v1.2.1 From 951e8cfd3a4d892cdaed82ce6b62803dcc001209 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 16:05:30 -0700 Subject: Fix demos --- demos/demo.py | 1 + demos/demo_sftp.py | 26 ++++++++++++++------------ demos/demo_simple.py | 24 ++++++++++++++---------- demos/forward.py | 18 ++++++++++++------ demos/rforward.py | 12 +++++++----- 5 files changed, 48 insertions(+), 33 deletions(-) (limited to 'demos') diff --git a/demos/demo.py b/demos/demo.py index 744b5c31..2f245baf 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -28,6 +28,7 @@ import socket import sys import time import traceback +from paramiko.py3compat import input import paramiko try: diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 7c4aaba0..2dba1722 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -28,6 +28,7 @@ import sys import traceback import paramiko +from paramiko.py3compat import input # setup logging @@ -40,9 +41,9 @@ if len(sys.argv) > 1: if hostname.find('@') >= 0: username, hostname = hostname.split('@') else: - hostname = raw_input('Hostname: ') + hostname = input('Hostname: ') if len(hostname) == 0: - print '*** Hostname required.' + print('*** Hostname required.') sys.exit(1) port = 22 if hostname.find(':') >= 0: @@ -53,7 +54,7 @@ if hostname.find(':') >= 0: # get username if username == '': default_username = getpass.getuser() - username = raw_input('Username [%s]: ' % default_username) + username = input('Username [%s]: ' % default_username) if len(username) == 0: username = default_username password = getpass.getpass('Password for %s@%s: ' % (username, hostname)) @@ -69,13 +70,13 @@ except IOError: # try ~/ssh/ too, because windows can't have a folder named ~/.ssh/ host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) except IOError: - print '*** Unable to open host keys file' + print('*** Unable to open host keys file') host_keys = {} -if host_keys.has_key(hostname): +if hostname in host_keys: hostkeytype = host_keys[hostname].keys()[0] hostkey = host_keys[hostname][hostkeytype] - print 'Using host key of type %s' % hostkeytype + print('Using host key of type %s' % hostkeytype) # now, connect and use paramiko Transport to negotiate SSH2 across the connection @@ -86,22 +87,22 @@ try: # dirlist on remote host dirlist = sftp.listdir('.') - print "Dirlist:", dirlist + print("Dirlist: %s" % dirlist) # copy this demo onto the server try: sftp.mkdir("demo_sftp_folder") except IOError: - print '(assuming demo_sftp_folder/ already exists)' + print('(assuming demo_sftp_folder/ already exists)') sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') data = open('demo_sftp.py', 'r').read() sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data) - print 'created demo_sftp_folder/ on the server' + print('created demo_sftp_folder/ on the server') # copy the README back here data = sftp.open('demo_sftp_folder/README', 'r').read() open('README_demo_sftp', 'w').write(data) - print 'copied README back here' + print('copied README back here') # BETTER: use the get() and put() methods sftp.put('demo_sftp.py', 'demo_sftp_folder/demo_sftp.py') @@ -109,8 +110,9 @@ try: t.close() -except Exception, e: - print '*** Caught exception: %s: %s' % (e.__class__, e) +except Exception: + e = sys.exc_info()[1] + print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: t.close() diff --git a/demos/demo_simple.py b/demos/demo_simple.py index 50f344a7..fb71145d 100755 --- a/demos/demo_simple.py +++ b/demos/demo_simple.py @@ -25,9 +25,13 @@ import os import socket import sys import traceback +from paramiko.py3compat import input import paramiko -import interactive +try: + import interactive +except ImportError: + from . import interactive # setup logging @@ -40,9 +44,9 @@ if len(sys.argv) > 1: if hostname.find('@') >= 0: username, hostname = hostname.split('@') else: - hostname = raw_input('Hostname: ') + hostname = input('Hostname: ') if len(hostname) == 0: - print '*** Hostname required.' + print('*** Hostname required.') sys.exit(1) port = 22 if hostname.find(':') >= 0: @@ -53,7 +57,7 @@ if hostname.find(':') >= 0: # get username if username == '': default_username = getpass.getuser() - username = raw_input('Username [%s]: ' % default_username) + username = input('Username [%s]: ' % default_username) if len(username) == 0: username = default_username password = getpass.getpass('Password for %s@%s: ' % (username, hostname)) @@ -64,18 +68,18 @@ try: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) - print '*** Connecting...' + print('*** Connecting...') client.connect(hostname, port, username, password) chan = client.invoke_shell() - print repr(client.get_transport()) - print '*** Here we go!' - print + print(repr(client.get_transport())) + print('*** Here we go!\n') interactive.interactive_shell(chan) chan.close() client.close() -except Exception, e: - print '*** Caught exception: %s: %s' % (e.__class__, e) +except Exception: + e = sys.exc_info()[1] + print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: client.close() diff --git a/demos/forward.py b/demos/forward.py index 5048c775..f1a0c047 100644 --- a/demos/forward.py +++ b/demos/forward.py @@ -30,7 +30,11 @@ import getpass import os import socket import select -import SocketServer +try: + import SocketServer +except ImportError: + import socketserver as SocketServer + import sys from optparse import OptionParser @@ -54,7 +58,8 @@ class Handler (SocketServer.BaseRequestHandler): chan = self.ssh_transport.open_channel('direct-tcpip', (self.chain_host, self.chain_port), self.request.getpeername()) - except Exception, e: + except Exception: + e = sys.exc_info()[1] verbose('Incoming request to %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) @@ -98,7 +103,7 @@ def forward_tunnel(local_port, remote_host, remote_port, transport): def verbose(s): if g_verbose: - print s + print(s) HELP = """\ @@ -165,8 +170,9 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception, e: - print '*** Failed to connect to %s:%d: %r' % (server[0], server[1], e) + except Exception: + e = sys.exc_info()[1] + print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) verbose('Now forwarding port %d to %s:%d ...' % (options.port, remote[0], remote[1])) @@ -174,7 +180,7 @@ def main(): try: forward_tunnel(options.port, remote[0], remote[1], client.get_transport()) except KeyboardInterrupt: - print 'C-c: Port forwarding stopped.' + print('C-c: Port forwarding stopped.') sys.exit(0) diff --git a/demos/rforward.py b/demos/rforward.py index 4a5d2e43..fcffbcb1 100755 --- a/demos/rforward.py +++ b/demos/rforward.py @@ -46,7 +46,8 @@ def handler(chan, host, port): sock = socket.socket() try: sock.connect((host, port)) - except Exception, e: + except Exception: + e = sys.exc_info()[1] verbose('Forwarding request to %s:%d failed: %r' % (host, port, e)) return @@ -82,7 +83,7 @@ def reverse_forward_tunnel(server_port, remote_host, remote_port, transport): def verbose(s): if g_verbose: - print s + print(s) HELP = """\ @@ -150,8 +151,9 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception, e: - print '*** Failed to connect to %s:%d: %r' % (server[0], server[1], e) + except Exception: + e = sys.exc_info()[1] + print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) verbose('Now forwarding remote port %d to %s:%d ...' % (options.port, remote[0], remote[1])) @@ -159,7 +161,7 @@ def main(): try: reverse_forward_tunnel(options.port, remote[0], remote[1], client.get_transport()) except KeyboardInterrupt: - print 'C-c: Port forwarding stopped.' + print('C-c: Port forwarding stopped.') sys.exit(0) -- cgit v1.2.1 From 7444a999931cddc1e61bb35270468aa45da2687e Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Sat, 2 Nov 2013 20:18:18 -0700 Subject: Fix some deprecation and resource warnings --- demos/demo_server.py | 4 ++-- demos/demo_sftp.py | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'demos') diff --git a/demos/demo_server.py b/demos/demo_server.py index deb21387..5a41a714 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -27,7 +27,7 @@ import threading import traceback import paramiko -from paramiko.py3compat import b, u +from paramiko.py3compat import b, u, decodebytes # setup logging @@ -46,7 +46,7 @@ class Server (paramiko.ServerInterface): 'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \ 'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \ 'UWT10hcuO4Ks8=') - good_pub_key = paramiko.RSAKey(data=base64.decodestring(data)) + good_pub_key = paramiko.RSAKey(data=decodebytes(data)) def __init__(self): self.event = threading.Event() diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 2dba1722..d7f28084 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -20,6 +20,8 @@ # based on code provided by raymond mosteller (thanks!) +from __future__ import with_statement + import base64 import getpass import os @@ -95,13 +97,15 @@ try: except IOError: print('(assuming demo_sftp_folder/ already exists)') sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') - data = open('demo_sftp.py', 'r').read() + with open('demo_sftp.py', 'r') as f: + data = f.read() sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data) print('created demo_sftp_folder/ on the server') # copy the README back here data = sftp.open('demo_sftp_folder/README', 'r').read() - open('README_demo_sftp', 'w').write(data) + with open('README_demo_sftp', 'w') as f: + f.write(data) print('copied README back here') # BETTER: use the get() and put() methods -- cgit v1.2.1 From 25dd096da065b1bc2f35c1a62d8a7055b022818b Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 08:06:35 -0800 Subject: Change all exceptions to modern format (not Py2.5 compatible) --- demos/demo.py | 6 ++---- demos/demo_server.py | 9 +++------ demos/demo_sftp.py | 3 +-- demos/demo_simple.py | 3 +-- demos/forward.py | 6 ++---- demos/rforward.py | 6 ++---- 6 files changed, 11 insertions(+), 22 deletions(-) (limited to 'demos') diff --git a/demos/demo.py b/demos/demo.py index 2f245baf..fff61784 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -113,8 +113,7 @@ if hostname.find(':') >= 0: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, port)) -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Connect failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -171,8 +170,7 @@ try: chan.close() t.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: diff --git a/demos/demo_server.py b/demos/demo_server.py index 5a41a714..34a9bd5e 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -84,8 +84,7 @@ try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', 2200)) -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Bind failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -94,8 +93,7 @@ try: sock.listen(100) print('Listening for connection ...') client, addr = sock.accept() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Listen/accept failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -138,8 +136,7 @@ try: chan.send('\r\nI don\'t like you, ' + username + '.\r\n') chan.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index d7f28084..1cccffc2 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -114,8 +114,7 @@ try: t.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: diff --git a/demos/demo_simple.py b/demos/demo_simple.py index fb71145d..ae631e43 100755 --- a/demos/demo_simple.py +++ b/demos/demo_simple.py @@ -77,8 +77,7 @@ try: chan.close() client.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: diff --git a/demos/forward.py b/demos/forward.py index f1a0c047..96e1700d 100644 --- a/demos/forward.py +++ b/demos/forward.py @@ -58,8 +58,7 @@ class Handler (SocketServer.BaseRequestHandler): chan = self.ssh_transport.open_channel('direct-tcpip', (self.chain_host, self.chain_port), self.request.getpeername()) - except Exception: - e = sys.exc_info()[1] + except Exception as e: verbose('Incoming request to %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) @@ -170,8 +169,7 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception: - e = sys.exc_info()[1] + except Exception as e: print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) diff --git a/demos/rforward.py b/demos/rforward.py index fcffbcb1..ae70670c 100755 --- a/demos/rforward.py +++ b/demos/rforward.py @@ -46,8 +46,7 @@ def handler(chan, host, port): sock = socket.socket() try: sock.connect((host, port)) - except Exception: - e = sys.exc_info()[1] + except Exception as e: verbose('Forwarding request to %s:%d failed: %r' % (host, port, e)) return @@ -151,8 +150,7 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception: - e = sys.exc_info()[1] + except Exception as e: print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) -- cgit v1.2.1 From 2da5f1fb4565d1e6dc72672f640dac44b122d235 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 08:56:53 -0800 Subject: Use 'with' for opening most file and SFTPFIle objects --- demos/demo_sftp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'demos') diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 1cccffc2..9476f76c 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -96,7 +96,8 @@ try: sftp.mkdir("demo_sftp_folder") except IOError: print('(assuming demo_sftp_folder/ already exists)') - sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') + with sftp.open('demo_sftp_folder/README', 'w') as f: + f.write('This was created by demo_sftp.py.\n') with open('demo_sftp.py', 'r') as f: data = f.read() sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data) -- cgit v1.2.1 From 106f9ea444ee9ed8246be128f232217b6c4977b8 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 08:56:53 -0800 Subject: Use 'with' for opening most file and SFTPFIle objects --- demos/demo_sftp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'demos') diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 9476f76c..c04d6e15 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -104,7 +104,8 @@ try: print('created demo_sftp_folder/ on the server') # copy the README back here - data = sftp.open('demo_sftp_folder/README', 'r').read() + with sftp.open('demo_sftp_folder/README', 'r') as f: + data = f.read() with open('README_demo_sftp', 'w') as f: f.write(data) print('copied README back here') -- cgit v1.2.1 From 981f768a62402a5aaa9f9f0ddfb4e14faa0d4442 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 09:37:33 -0800 Subject: Remove `from __future__ import with_statement` --- demos/demo_keygen.py | 1 - demos/demo_sftp.py | 2 -- 2 files changed, 3 deletions(-) (limited to 'demos') diff --git a/demos/demo_keygen.py b/demos/demo_keygen.py index 88facd46..860ee4e9 100755 --- a/demos/demo_keygen.py +++ b/demos/demo_keygen.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Lesser General Public License # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -from __future__ import with_statement import sys diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index c04d6e15..a34f2b19 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -20,8 +20,6 @@ # based on code provided by raymond mosteller (thanks!) -from __future__ import with_statement - import base64 import getpass import os -- cgit v1.2.1 From 6d75c75e643c3a109075e86e72b08221c76088cc Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 10:09:08 -0800 Subject: Remove byte conversions and unhexlify calls that we only needed for Py2.5 support and use the `b` byte string marker instead --- demos/demo_server.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'demos') diff --git a/demos/demo_server.py b/demos/demo_server.py index 34a9bd5e..bb35258b 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -42,10 +42,10 @@ print('Read key: ' + u(hexlify(host_key.get_fingerprint()))) class Server (paramiko.ServerInterface): # 'data' is the output of base64.encodestring(str(key)) # (using the "user_rsa_key" files) - data = b('AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + \ - 'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \ - 'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \ - 'UWT10hcuO4Ks8=') + data = (b'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + b'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + b'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + b'UWT10hcuO4Ks8=') good_pub_key = paramiko.RSAKey(data=decodebytes(data)) def __init__(self): -- cgit v1.2.1