diff options
author | Sebastian Deiss <s.deiss@science-computing.de> | 2014-02-11 13:01:49 +0100 |
---|---|---|
committer | Sebastian Deiss <s.deiss@science-computing.de> | 2014-02-11 13:01:49 +0100 |
commit | e7f41de2f2dac5d03404f35edc5514f12e42c49f (patch) | |
tree | eae66247e741b2acdcc5ec10712af231992296a0 /demos/demo_sftp.py | |
parent | a08ac06083a40eb1455e5fa14cd644f7a2350f16 (diff) |
Merge branch scottkmaxwell:py3-support-without-py25 into
SebastianDeiss:gssapi-py3-support
Diffstat (limited to 'demos/demo_sftp.py')
-rwxr-xr-x | demos/demo_sftp.py | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 7c4aaba0..a34f2b19 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,26 @@ 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)' - sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') - data = open('demo_sftp.py', 'r').read() + print('(assuming demo_sftp_folder/ already exists)') + 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) - 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' + 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') # BETTER: use the get() and put() methods sftp.put('demo_sftp.py', 'demo_sftp_folder/demo_sftp.py') @@ -109,8 +114,8 @@ try: t.close() -except Exception, e: - print '*** Caught exception: %s: %s' % (e.__class__, e) +except Exception as e: + print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: t.close() |