import os, sys, ftplib from getpass import getpass from mimetypes import guess_type
nonpassive = False # passive FTP by default remotesite = 'learning-python.com' # upload to this site remotedir = 'books' # from machine running on remoteuser = 'lutz' remotepass = getpass('Password for %s on %s: ' % (remoteuser, remotesite)) localdir = (len(sys.argv) > 1 and sys.argv[1]) or '.' cleanall = input('Clean remote directory first? ')[:1] in ['y', 'Y']
print('connecting...') connection = ftplib.FTP(remotesite) # connect to FTP site connection.login(remoteuser, remotepass) # log in as user/password connection.cwd(remotedir) # cd to directory to copy if nonpassive: # force active mode FTP connection.set_pasv(False) # most servers do passive
if cleanall: for remotename in connection.nlst(): # try to delete all remotes try: # first, to remove old files print('deleting remote', remotename) connection.delete(remotename) # skips . and .. if attempted except: print('cannot delete remote', remotename)
count = 0 # upload all local files localfiles = os.listdir(localdir) # listdir() strips dir path # any failure ends script for localname in localfiles: mimetype, encoding = guess_type(localname) # e.g., ('text/plain', 'gzip') mimetype = mimetype or '?/?' # may be (None, None) maintype = mimetype.split('/')[0] # .jpg ('image/jpeg', None')
localpath = os.path.join(localdir, localname)
print('uploading', localpath, 'to', localname, end=' ')
print('as', maintype, encoding or '')
if maintype == 'text' and encoding == None:
# use ascii mode xfer and bytes file
# need rb mode for ftplib's crlf logic
localfile = open(localpath, 'rb')
connection.storlines('STOR ' + localname, localfile)
else:
# use binary mode xfer and bytes file
localfile = open(localpath, 'rb')
connection.storbinary('STOR ' + localname, localfile)
localfile.close()
count += 1
connection.quit() print('Done:', count, 'files uploaded.')
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有